Welcome Guest Search | Active Topics | Sign In | Register

Performance problem when using page number Options
egronbboj
Posted: Thursday, January 5, 2012 8:29:06 AM
Rank: Member
Groups: Member

Joined: 1/2/2012
Posts: 10
EO.PDF is blazingly fast when generating a 500+ page PDF - 26.5s on my computer! This was until I wanted to see the page number and number of pages in the footer of each page, like this: "Page m / n".

To achieve this I used the following code:
Code: Visual Basic.NET
EO.Pdf.HtmlToPdf.Options.FooterHtmlFormat = _
"<div><div style='float: left; color: gray;'>...</div>" & _
"<div style='float: right; color: gray;'>Page {page_number} / {total_pages}</div></div>"


Now the time to generate the exact same PDF is increased by a factor of 11 (5m21s)...

Is there any alternative to "{page_number}" and "{total_pages}"
eo_support
Posted: Thursday, January 5, 2012 8:56:15 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Yes. You can add the page number using "PDF Creator" interface after you finishes converting. The basic idea is like this:

Code: C#
//Convert your main content
HtmlToPdf.ConvertHtml(html, doc);

//Add header/footer
foreach (PdfPage page in doc.Pages)
{
    //Now use PDF Creator interface, not HTML to PDF interface to generate
    //page number on each page
    .....
}


The PDF Creator interface is a different set of interface than HTML to PDF. It's less powerful, more difficult to use (more code), but much faster, but works very well for simple output such as page header and footers. You can take a look of the sample and documentation to see how it works. If you run into any questions, feel free to let us know and we will help you out.

Thanks!

egronbboj
Posted: Friday, January 6, 2012 9:21:48 AM
Rank: Member
Groups: Member

Joined: 1/2/2012
Posts: 10
Great, by adding the page numbers this way, the PDF generation is fast again! For anyone interested in the final solution, I added the left side part of the footer the same way as before - with HtmlToPDF:
Code: Visual Basic.NET
HtmlToPdf.Options.FooterHtmlFormat = _
String.Format("<div style='float: left; color: gray;'>...</div>")

This reserves space on the bottom of each page, and was necessary since I wanted the position of the page numbers to be fixed. To achieve fixed positioning you have to take the string out of the document flow - much as absolute positioning in HTML:
Code: Visual Basic.NET
Dim pdf As New PdfDocument()
HtmlToPdf.ConvertHtml(htmlString.ToString(), pdf)

For Each page As PdfPage In pdf.Pages
    Dim render As New AcmRender(page, 0, New AcmPageLayout(New AcmPadding(0)))
    Dim text As New AcmText(String.Format("Page{0} / {1}", page.Index + 1, pdf.Pages.Count))
    text.Style.ForegroundColor = Color.LightGray
    Dim block As New AcmBlock(text)
    block.Style.Top = 11.15F
    block.Style.Left = 7.0F
    Dim footer As New AcmBlock(block)
    render.Render(footer)
Next

Thanks for your help!
eo_support
Posted: Friday, January 6, 2012 1:01:26 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
That's great. Thank you very much for sharing!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.