|
Rank: Newbie Groups: Member
Joined: 12/16/2015 Posts: 5
|
I'm converting HTML to PDF and need to have a different page 1 header than the remaining pages in the document. The headers are also different sizes. Page 1 is 2.5 inches tall. All other pages have a 1.5 inch header.
I tried using On_BeforeRenderPage and On_AfterRenderPage, but it doesn't appear that setting OutputArea there affects the rendered size of the page. I also need to display total number of pages in the header (page X of Y) and the {total_pages} field doesn't work in those events.
Is there another way to do this?
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi, You can set this property to push the output "down" on the first page: http://www.essentialobjects.com/doc/eo.pdf.htmltopdfoptions.startposition.aspxFor example, if you set it to 1, then it will push the output down one inch. That will give you enough room to add header. Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 12/16/2015 Posts: 5
|
Thanks, that worked to adjust the size of the first page. How can I solve the issue of page numbers in the header? The page_number and total_pages variables aren't working in AfterRenderPage.
Thanks
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi,
You just call HtmlToPdf.ConvertHtml(your_page_header_html, pdfPage) to create the header/footer in AfterRenderPage event. The difference is here the second argument is a PdfPage object (the page that you just finished rendering main content). Also make sure you set HtmlToPdf.Options.OutputArea correctly so that the output will go to the header/footer area.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 12/16/2015 Posts: 5
|
The fields aren't getting replaced. Is there something else I need to call to get that to happen? This is what I'm doing:
Code:
string header = "Page {page_number} of {total_pages}"; HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, 7.5f, 1.5f); HtmlToPdf.ConvertHtml(header, e.Page.Document.Pages[e.Page.Index]);
My header ends up looking like:
Code:
Page: {page_number} of {total_pages}
Thanks
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Variables are only recognized in HtmlToPdf.Options.HeaderHtmlFormat/FooterHtmlFormat. If you need to add variables into your header/footer with ConvertHtml, you can do it after the main contents conversion and then use a loop to loop through all pages and call ConvertHtml on each page.
|
|
Rank: Newbie Groups: Member
Joined: 12/16/2015 Posts: 5
|
Just to make sure I understand, I would render the PDF first, then loop through each page and add my page numbers where I want them? Or is there another callback I should hook into in order to do that?
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Your understanding is correct. There is no other callback you can use in your case.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 12/17/2015 Posts: 4
|
Hi guys,
i am looking for an example for adding page numbers. In this thread you say, that you can pass variables to ConvertHtml after the first/main conversion. Can you provide an example for this? Or can you tell me how i can access the page numbers in my HTML Markup?
|
|
Rank: Newbie Groups: Member
Joined: 12/16/2015 Posts: 5
|
This code adds a header using HeaderHtmlFormat, creates the PDF, saves it and then opens it. The {page_number} and {total_pages} fields get automatically filled with the current page number and total number of pages respectively.
Code:
string pdfOutput = @"c:\temp\pdfOutput.pdf"; string html = "<html><body><p>My sample document</p></body></html>"; string header = "<div style='text-align: center;font-size: 18; font-family: Times New Roman'><b>Page Heading</b></div>" + "<table width='100%' border='0' style='font-size: 15; font-family: Times New Roman' cellpadding='0'>" + "<tr><td width='80%'>Printed: 01/08/2016</td><td width='50px' align='right'>Page:</td><td>{page_number} of {total_pages}</td></tr>" + "</table>"; EO.Pdf.HtmlToPdf.Options.HeaderHtmlFormat = header;
PdfDocument pdfDoc = new PdfDocument(); HtmlToPdf.ConvertHtml(html, pdfDoc);
pdfDoc.Save(pdfOutput);
Process.Start(pdfOutput);
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
SHeinze wrote:Hi guys,
In this thread you say, that you can pass variables to ConvertHtml after the first/main conversion. Can you provide an example for this? Or can you tell me how i can access the page numbers in my HTML Markup? No. That's not what we meant. You use variable in your own code, not to pass variable to ConvertHtml. For example, the following code add a "page number" on every page of an existing PdfDocument:
Code: C#
for (int i = 0; i < doc.Pages.Count; i++)
{
HtmlToPdf.ConverHtml((i + 1).ToString(), doc.Pages[i]);
}
Here variable "i" is a variable in your own code that you use to generate the page number string (in this case "(i + 1).toString())". Obviously this has nothing to do with ConvertHtml. This code is just to give you an idea. You will need to adjust the above code to set the output position. Please feel free to let us know if you still have any questions. Thanks!
|
|