|
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}"
|
|
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!
|
|
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!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
That's great. Thank you very much for sharing!
|
|