Free Support Forum - aspose.com

Generate HTML of first fiew pages

I am generating some MailMerge documents using Aspose.Words. Some of them are almost 100 pages in length. Is there a way to export the first couple pages to HTML for previewing purposes? I have played with the Aspose.Words.Viewer, but the Bitmaps are often rendered weird.

Hi Nick,

Yes, it is possible to output only a part of the document to HTML. However, you cannot specify exactly a certain number of pages. But I think this is okay since HTML in the browser does not fixed pages.

The best way is to go copy a certain number of nodes from the first section of the document into a new document and save that small document as HTML. Here is sample code, (I'm quickly writing it right here, not in Visual Studio so it might have some syntax errors):

// This small document we will save as HTML.
Document dstDoc = new Document();

Body srcBody = srcDoc.FirstSection.Body;
Body dstBody = dstDoc.FirstSection.Body;

int i = 0;

for (Node srcNode = srcBody.FirstChild; node != null; node = node.NextSibling)
{
dstBody.AppendChild(dstBody.ImportNode(srcNode, true));

i++;

// Lets say we want to output this number of nodes to the preview HTML. These will basically bebody-level paragraphs or tables.
int const PreviewNodesCount = 100;

if (i >= PreviewNodesCount)
break;

}

dstDoc.Save(myFileName, SaveFormat.FormatHtml);