Document with more than one pages render to one HTML-File

Basically GroupDocs.Viewer created for each Word or PDF-Document that contains more than one page, one html file.

How can I create one html file with serveral pages (1:1 to Dokument) ?

For example, I have a doc-document that contains 10 pages. That document schould be render to ONE html file that contains 10 pages.

Hi there,


Thanks for using GroupDocs.Viewer API.

You can do this manually by appending html content of all pages in a single html file. Below is a sample code snippet to achieve this functionality.


string outputHtmlContent = “”;

foreach (PageHtml page in pages)
{
outputHtmlContent += page.HtmlContent;
}

File.AppendAllText(“D:\output.html”, outputHtmlContent);


In case of any confusion, please feel free to ask.

Warm Regards

thx for the answer, its work.


My next question is, how i can limit the number of pages for rendering.

For example, I have document with 20 or more pages and I want to render only first 10 pages of that document.
Hi there,

With the example code I've used, this approach creates an output which contains many HTML and HEAD tags - the document still renders in a browser, but is there a way to stop the multiple tags?

I'm just getting pages out of local filestream via htmlHandler.GetPages(fs), and this also creates several @font-face declarations in each page - do those need to be accounted for? I'd rather not extract them to supporting files like the ASP.Net MVC Front End showcase project does.

Thank you,

Mike Kingscott
Hi there,

Yes, you can achieve this functionality by providing range of the document pages to convert in HtmlOptions or ImageOptions. You can do this using following code

HtmlOptions options = new HtmlOptions();
options.PageNumbersToConvert = Enumerable.Range(1, 10).ToList();
List pages = htmlHandler.GetPages(guid, options);

Try above mentioned code and share your feedback.

Have a nice weekend ahead.

Hi Mike,


Thanks for using GroupDocs.Viewer for .NET.

GroupDocs.Viewer for .NET does not provide any builtin method to escape from multiple tags in case of merging HTML pages into single page. However, you can use your own logic to extract the child elements of all head tags, trim/remove head tags from HTML content of all pages and then add a single head tag in final html page (containing body of all HTML pages) that will contain the child elements extracted at first step. You can play with HTML content of each page using following code:

foreach (PageHtml page in pages)
{
string outputHtmlContent = page.HtmlContent;
// you can use outputHtmlContent to perform your required operations on HTML content
}


Secondly, when you use embedded resources then all the resources (including font faces) get embedded in HTML pages. However, when you do not use embedded resources then the API creates a separate directory named “resources” containing all the resources in it and adds reference of these resources in HTML page. Following code snippet shows how to set embedded resources.

//Instantiate the HtmlOptions object
HtmlOptions options = new HtmlOptions();

//to get html representations of pages with embedded resources
options.IsResourcesEmbedded = false;

List pages = htmlHandler.GetPages(guid, options);


If anything is not yet clear, please feel free to tell us.

Warm Regards

hi,


thx for answer.

If I use the options with Range(1,10) this is works only for file which contains more than 10 pages. If the file has less than 10 pages, i get an exception.

I resolve this problem in wich I use the function:
HtmlOptions options = new HtmlOptions()
options.CountPagesToConvert = 10;

Hello Usman,


Thank you for the swift reply. I figured I’d have to play about with the HTML as your example shows, and thank you for the clarification on the resources.

I do have another question, but I need to write some sample code first.

Thanks again,

Mike K.

Hi there,


Its good to know that you have resolved your issue.

You can also get complete information about a document, containing number of pages, before rendering that document. This will help you to escape from any exception. For more details, please see Working with Document Information.

Warm Regards

Hi Mike,


Sure, you are always welcome to post your questions here. We shall be looking forward to hear from you.

Warm Regards