This is a migrated thread and some comments may be shown as answers.

Import/Export issue

1 Answer 76 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
MI Pro
Top achievements
Rank 1
MI Pro asked on 28 Mar 2011, 01:51 PM
Hi,

I am trying to import a simple Html table and then export this to Docx:

var htmlDoc = new RadDocument();
htmlDoc.Measure(RadDocument.MAX_DOCUMENT_SIZE);
htmlDoc.Arrange(new RectangleF(PointF.Empty, htmlDoc.DesiredSize));

var htmltable = "<html><body><table><tr><td>cell</td></tr></table></body></html>";
var import = new HtmlFormatProvider();
var export = new DocxFormatProvider();

using (var stream = new MemoryStream()){
                var writer = new StreamWriter(stream);
                writer.Write(htmltable);
                writer.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                htmlDoc = import.Import(stream);
}

var bytes = export.Export(htmlDoc);

On the last line I always get en exception saying :
"System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection."

Any ideas?

1 Answer, 1 is accepted

Sort by
0
Accepted
Iva Toteva
Telerik team
answered on 29 Mar 2011, 10:01 AM
Hello Bjorn Gronli,

Actually you don't need to measure and arrange the document before the import, but after the import.
Here is the revised code:

               RadDocument htmlDoc;           
           var htmltable = "<html><body><table><tr><td>cell</td></tr></table></body></html>";
           var importer = new HtmlFormatProvider();
           var exporter = new DocxFormatProvider();
 
           using (var stream = new MemoryStream())
           {
               var writer = new StreamWriter(stream);
               writer.Write(htmltable);
               writer.Flush();
               stream.Seek(0, SeekOrigin.Begin);
               htmlDoc = importer.Import(stream);
           }
           htmlDoc.Measure(RadDocument.MAX_DOCUMENT_SIZE);
           htmlDoc.Arrange(new RectangleF(PointF.Empty, htmlDoc.DesiredSize));
           var bytes = exporter.Export(htmlDoc);

Best wishes,
Iva
the Telerik team
Tags
RichTextBox
Asked by
MI Pro
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question
or