PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Resize the PdfPage after adding content
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3828
Page 1 of 1

Author:  Maarten [ Mon Aug 20, 2018 1:07 pm ]
Post subject:  Resize the PdfPage after adding content

Hello,

I create a pdf document containing one page. The size of the page depends on the total size of it content.

The following steps are executed:

1) create the pdf doc
me.PdfDoc = new pdfdocument

2) add a pdf page
me.PdfPage = me.PdfDoc.AddPage()

3) create Gfx object
me.gfx = xgraphics.FromPdfPage(me.PdfPage)

4) I add rectangles and text in teh Gfx object. All works fine.

5) After calculating the max width and Height used in the PdfPage I need to shrink the PdfPage to the minimal values (+10mm)
Using the "PdfPage.width = value" removes all contents.

Question:
Can I redefine the width and Height of the PdfPage after in contains elements like text and rectangles?
Or do I have to create a new PdfPage from start, set the width and height, and then fill the content of it?


Best regards,
Maarten

Author:  Thomas Hoevel [ Mon Aug 20, 2018 1:50 pm ]
Post subject:  Re: Resize the PdfPage after adding content

Hi!
Maarten wrote:
Using the "PdfPage.width = value" removes all contents.
Most likely it does not remove the contents. You expect to see the top half of the page, but what you still see could be the bottom half of the page. The contents are still there, they are just outside the visible area.

Maybe try playing around with the MediaBox to make sure you see the part where you added contents.

Or maybe create a second PDF page with the desired size and draw the oversized page on this page. This adds a few extra bytes to the PDF file.

The best way: determine the page size you need before creating the final contents. Call your code twice: first time to determine the correct page size (discarding the page created in this run), second time to draw the correct page.
Use caching if you access databases or do other "slow" things in the code.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/