Rank: Newbie Groups: Member
Joined: 11/3/2016 Posts: 4
|
Is it possible to create mixed (landscape/portrait) pages in a single documents from a single HTML page?
I am trying to render a single web page that has some sections (elements followed by page breaks) that should be rendered as Landscape and some as Portrait.
Each section in the source document would be defined as following: <div class="landscape"></div>
<div class="portrait"></div>
From trial and error, it seems that I can resize each individual page from BeforeRenderPage but I am not sure if there is a way to to access the content to figure out which orientation is required.
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi,
No. It's not possible to do that in a single conversion. You can have two separate conversions and then use PdfDocument.Merge to merge them into a single PdfDocument object.
Thanks!
|
Rank: Newbie Groups: Member
Joined: 11/3/2016 Posts: 4
|
Just thought to add this here as I believe I figured out a way to do it. It probably won't work in every situation, but for me, where HTML pages are formatted for a specific size this works quite well. There are some caveats such that automatic paging would be disabled, but for my situation I had full control of HTML and the layout.
1) Initially setup page to be 11x11 (if trying to render Letter 8.5x11) 2) Use HtmlToPdfSession to load HTML and then call CreatePaginator 3) Use GetElementsByClassName to find elements that represent pages, and then use "ClassName" to determine if page needs to be landscape or portrait (this is done by landscape/portrait class) 4) Record this information based on page index 5) Define HtmlToPdf.Options.BeforeRenderPage callback, and use args.Page.MediaBox to "crop" the page depending on the size. Keep in mind, that 0,0 is at the bottom left.
float bigSide = args.Page.MediaBox.Width; float smallSide = bigSide*8.5f/11f; .. // Landscape args.Page.MediaBox = new PdfRectangle(0, bigSide - smallSide, bigSide, bigSide);
// Portriat args.Page.MediaBox = new PdfRectangle((bigSide - smallSide)/2, 0, (bigSide - smallSide) / 2 + smallSide, bigSide);
Then assign same to CropBox args.Page.CropBox = args.Page.MediaBox;
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Wow! That is something we would have never thought off. Thanks for sharing!
|