Welcome Guest Search | Active Topics | Sign In | Register

Set different output area on first and subsequent pages. Options
John Barton
Posted: Wednesday, March 28, 2018 7:17:34 AM
Rank: Newbie
Groups: Member

Joined: 9/10/2012
Posts: 4
I am using HtmlToPdf.ConvertUrl to generate a PDF document which will be multiple pages. The first page features a full width image, which to have top left of the page without margin i am setting the output area to this:

HtmlToPdf.Options.OutputArea = new RectangleF(0f, 0f, PdfPageSizes.A4.Width, (PdfPageSizes.A4.Height - 0.25f));

However from page 2 onwards there is pure text content so i would want to apply a small top margin such as :

HtmlToPdf.Options.OutputArea = new RectangleF(0f, 0.25f, PdfPageSizes.A4.Width, (PdfPageSizes.A4.Height - 0.25f));

eo_support
Posted: Wednesday, March 28, 2018 8:20:23 AM
Rank: Administration
Groups: Administration

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

You will need to do two different conversions for such cases. The code will be something like this:

Code: C#
PdfDocument doc = new PdfDocument();

HtmlToPdf.Options.OutputArea = ...
HtmlToPdf.ConvertHtml(html1, doc);

doc.Pages.Add();
HtmlToPdf.Options.OutputArea = ....
HtmlToPdf.Options.StartPageIndex = 1;
HtmlToPdf.ConvertHtml(html2, doc);


The key is:
1. Both conversions convert to the same PdfDocument object;
2. For the second conversion, StartPageIndex is set to 1 so it starts from the second page;

Hope this helps. Please feel free to let us know if you have any more question.

Thanks!
John Barton
Posted: Wednesday, March 28, 2018 9:40:26 AM
Rank: Newbie
Groups: Member

Joined: 9/10/2012
Posts: 4
I didn't explain the problem accurately enough.

Sample page layout, each of the content divs has a page-break-inside: avoid; applied to it.

<header image full width>
<div 1st paragraph>
<div 2nd paragraph>
<div 3rd paragraph>
<div 4th paragraph>
<div 5th paragraph>
<div 6th paragraph>

Page one of the pdf would have the image position 0,0 and a number of the paragraphs based on the page-break inside rules.

Pages 2 onwards would be the content laid at starting with a .25 margin top. so is it best to convert the html with start page index of 1 and then remove page 2 from the PDF?

Code: C#
PdfDocument doc = new PdfDocument();

HtmlToPdf.Options.OutputArea = ...
HtmlToPdf.ConvertHtml(html1, doc);

doc.Pages.Add();
HtmlToPdf.Options.OutputArea = ....
HtmlToPdf.Options.StartPageIndex = 1;
HtmlToPdf.ConvertHtml(html1, doc);

doc.Pages.RemoveAt(1);
eo_support
Posted: Wednesday, March 28, 2018 10:29:46 AM
Rank: Administration
Groups: Administration

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

You can do that too. The basic rule here is a single conversion can only use a single page width value. So as long as the first page and the other pages have the same page width (same paper width, same margin left/right value), you can do it with a single conversion. Otherwise you must use multiple conversion. There is no exception to this rule because the HTML to PDF converter loads the HTML into an internal "browser window" and that window can only have one width value.

The rules for Margin top/bottom is a little bit relaxed because there are a few ways to achieve different margin top/bottom. For example, if you know exactly where page break occurs, then you can set the converter's margin to 0 and then use CSS to apply top bottom margin to the content of each page. Or you can use custom paging to decide where to page:

https://www.essentialobjects.com/doc/pdf/htmltopdf/paging.aspx#custom

If you must use multiple conversion, what you can do is to perform "test" conversions and then use the HtmlToPdfResult object returned by the test conversion to find out how much contents have been fitted in the current page. You can then start another conversion from where it's left (for example, by modifying the input HTML to remove what's has already been converted, or by applying CSS styles to hide those contents, etc).

Hope this helps. Please feel free to let us know if you have any more question.

Thanks!
John Barton
Posted: Thursday, March 29, 2018 4:33:47 AM
Rank: Newbie
Groups: Member

Joined: 9/10/2012
Posts: 4
Are there any samples of using the paginator?

eo_support
Posted: Thursday, March 29, 2018 8:01:14 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,221
The above link already has the sample code. If you have any question about the code, please ask and we will be happy to assist you further.


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.