PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Page size changing back to A4?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1249
Page 1 of 1

Author:  935main [ Sat Jul 10, 2010 6:27 pm ]
Post subject:  Page size changing back to A4?

When I create my MigraDoc, I set doc.DefaultPageSetup.PageFormat = PageFormat.Letter;

But when I go to render the document as a PDF, the PDF's page size is always A4.
What does it take to force the PDF renderer to always render with PageFormat.Letter instead of PageFormat.A4?

Author:  tcogdill [ Mon Dec 27, 2010 7:29 pm ]
Post subject:  Re: Page size changing back to A4?

Hello -

Did you ever find an answer to your question? I am trying to solve the same problem.

Thanks

Author:  935main [ Mon Dec 27, 2010 7:31 pm ]
Post subject:  Re: Page size changing back to A4?

Nope, I have not found a solution. I work around it by forcing the PDF to print to letter-size paper, but it really would be nice if I could actually create letter-size PDFs.

Please post if you find a solution!

Author:  tcogdill [ Mon Dec 27, 2010 8:06 pm ]
Post subject:  Re: Page size changing back to A4?

Looks like if you use a DocumentRenderer (not PdfDocumentRenderer) to prepare the document (makes it into a formatted document), you can get the page count, and then print each page one at a time to an XGraphics object that is bound to to a pdf page. I'm hoping this will always work, so long as the document and the pdf pages are both the same size (Letter in my case). Not ideal, but it appears to be working for me ... give it a shot!

Heres some C# code :

Code:
            MigraDoc.DocumentObjectModel.Document doc = new MigraDoc.DocumentObjectModel.Document();
            PdfSharp.Pdf.PdfDocument pdfDoc = new PdfSharp.Pdf.PdfDocument();
           
            doc.DefaultPageSetup.PageFormat = MigraDoc.DocumentObjectModel.PageFormat.Letter;

            // create document
            // ....
           
            var renderer = new MigraDoc.Rendering.DocumentRenderer(doc);
            renderer.PrepareDocument();
            for (int i = 1; i <= renderer.FormattedDocument.PageCount; ++i)
            {
                var page = pdfDoc.AddPage();
                page.Size = PdfSharp.PageSize.Letter;
                PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
                renderer.RenderPage(gfx, i);
            }
           
            pdfDoc.Save(filename);

Author:  jeffhare [ Tue Dec 28, 2010 9:34 pm ]
Post subject:  Re: Page size changing back to A4?

I do not see this behavior and am using GDI+ rendering, not WPF. Perhaps it's related to that?

I simply do the following and the Acrobat Reader document properties say it's 8.27" x 11.69"

Document PdfDoc = new Document()

(write the doc content )

PdfDoc.DefaultPageSetup.PageFormat = PageFormat.Letter;
PdfDoc.DefaultPageSetup.Orientation = Orientation.Portrait;

PdfDoc.DefaultPageSetup.TopMargin = "1cm";
PdfDoc.DefaultPageSetup.RightMargin = "1cm";
PdfDoc.DefaultPageSetup.BottomMargin = "1cm";
PdfDoc.DefaultPageSetup.LeftMargin = "1cm";

PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.None) {Document = PdfDoc}
renderer.RenderDocument();
renderer.Save(filename);

Author:  tcogdill [ Tue Dec 28, 2010 9:38 pm ]
Post subject:  Re: Page size changing back to A4?

Hmm, I think you're having the same issue we described... the size you quote is A4, not letter.

Letter is 8.5 x 11 (not approximate)

If, for instance, you just create a pdf document with size Letter and write a string on it or something, the pdf will be exactly 8.5 x 11 ...

edit:
that is, if you save it without first passing it through the rederer. The following produce a letter sized pdf:
Code:
            PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument();
            PdfSharp.Pdf.PdfPage page = doc.AddPage();
            page.Size = PdfSharp.PageSize.Letter;
            doc.Save(filename);

Author:  jeffhare [ Tue Dec 28, 2010 10:28 pm ]
Post subject:  Re: Page size changing back to A4?

Hmm. You're right. Did you just try setting the DefaultPageSetup.PageHeight/PageWidth directly?

I know for a fact that I previously set these very large to non-standard sizes when I was testing my nested Table code and it worked.

EDIT: I just tried this and I now have 8.5x11 in pages....

Author:  tcogdill [ Tue Dec 28, 2010 10:39 pm ]
Post subject:  Re: Page size changing back to A4?

Ahh thats an easy work-around!

That did work for me. Thanks!

Author:  jeffhare [ Tue Dec 28, 2010 10:41 pm ]
Post subject:  Re: Page size changing back to A4?

Thanks for pointing out that bug.

I'll see if I can find the bug that keeps it from taking the Letter settings.

BTW, I'm not on "the team", just a user. :)

Author:  tcogdill [ Tue Dec 28, 2010 10:51 pm ]
Post subject:  Re: Page size changing back to A4?

being a user isn't so bad ... tron fights for you

Author:  jeffhare [ Tue Dec 28, 2010 11:03 pm ]
Post subject:  Re: Page size changing back to A4?

:) Tron... good point. :)

It appears that the PdfDocumentRenderer has 2 members:

Document and PdfDocument
When we call Renderer.RenderDocument, it looks at whether PdfDocument is null and if so, creates a new pdfDoc to render on which results in default A4 sizing. This implies that we need to initialize both these documents to the same thing... ?

Now, there are other places I saw in PdfDocumentRenderer that use Document, and others use the PdfDocument property. Still other places like WriteDocumentInformation() assume both are set, test one and write the other.

Not sure whether there are 2 different props for a good reason, or whether one is an artifact, although one comment mentions printing. I haven't analyzed that part enough to be sure.

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