PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Jul 27, 2024 4:37 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Sat Jul 10, 2010 6:27 pm 
Offline

Joined: Wed Jun 30, 2010 10:08 pm
Posts: 4
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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 27, 2010 7:29 pm 
Offline

Joined: Mon Dec 27, 2010 7:27 pm
Posts: 5
Hello -

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

Thanks


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 27, 2010 7:31 pm 
Offline

Joined: Wed Jun 30, 2010 10:08 pm
Posts: 4
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!


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 27, 2010 8:06 pm 
Offline

Joined: Mon Dec 27, 2010 7:27 pm
Posts: 5
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);


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 28, 2010 9:34 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
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);


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 28, 2010 9:38 pm 
Offline

Joined: Mon Dec 27, 2010 7:27 pm
Posts: 5
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);


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 28, 2010 10:28 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
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....


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 28, 2010 10:39 pm 
Offline

Joined: Mon Dec 27, 2010 7:27 pm
Posts: 5
Ahh thats an easy work-around!

That did work for me. Thanks!


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 28, 2010 10:41 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
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. :)


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 28, 2010 10:51 pm 
Offline

Joined: Mon Dec 27, 2010 7:27 pm
Posts: 5
being a user isn't so bad ... tron fights for you


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 28, 2010 11:03 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
:) 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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 121 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group