PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 6:39 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue May 16, 2017 8:10 pm 
Offline

Joined: Tue May 16, 2017 7:56 pm
Posts: 2
I tried the code below in Migradoc GDI 1.50.4000-beta3b and 1.32.4334.0. When I set the page size to legal format, it either doesn't happen or it leaves a big margin at the top, as if the page size was 8.5 x 11, and the extra length was inserted at the top of the PDF. I'd rather the text start at the top of the page. How can I get around this?

In the sample below, there is a large margin at the top.
Code:
            // Create a new MigraDoc document
            Document document = new Document();
            //document.UseCmykColor = true;

            // Add a section to the document
            Section section = document.AddSection();
            section.PageSetup = document.DefaultPageSetup.Clone();
            section.PageSetup.PageFormat = PageFormat.Legal; //setting page size here didn't seem to work
            section.PageSetup.TopMargin = "0cm";

            // Add a paragraph to the section
            Paragraph paragraph = section.AddParagraph();

            paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50);

            // Add some text to the paragraph
            paragraph.AddFormattedText(@"Hello World!", TextFormat.Bold);

#if GDI
            // Using GDI-specific routines.
            // Make sure to use "#if GDI" for any usings you add for platform-specific code.
            {
            }
#endif

#if WPF
                    // Using WPF-specific routines.
                    // Make sure to use "#if GDI" for any usings you add for platform-specific code.
                    {
                    }
#endif

            // Create a renderer for the MigraDoc document.
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);

            // Associate the MigraDoc document with a renderer
            pdfRenderer.Document = document;

            // Layout and render document to PDF
            pdfRenderer.RenderDocument();

            pdfRenderer.PdfDocument.Pages[0].Size = PdfSharp.PageSize.Legal;

            // Save the document...
            const string filename = "HelloWorld.pdf";

            pdfRenderer.PdfDocument.Save(filename);
            // ...and start a viewer.
            Process.Start(filename);
        }


I'm not sure how I'm supposed to attach the working sample project if the file size limit is 256 KiB.


Top
 Profile  
Reply with quote  
PostPosted: Wed May 17, 2017 8:29 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

Answered on Stack Overflow: http://stackoverflow.com/a/44019399/162529

PageFormat is used to set PageWidth and PageHeight if they are unset.

Invoking section.PageSetup = document.DefaultPageSetup.Clone(); assigns both PageWidth and PageHeight with the values for A4 size. Changing PageFormat later has no effect on the effective page size which remains A4.

After invoking section.PageSetup = document.DefaultPageSetup.Clone(); you have to set both PageWidth and PageHeight to the correct values.

section.PageSetup = document.DefaultPageSetup.Clone(); is used to initialize all values of PageSetup. Use this if you use PageSetup to make calculations based on margins and such.

It is not generally recommend to invoke section.PageSetup = document.DefaultPageSetup.Clone();. It is strongly recommend to use a Clone() instead of making changes to the DefaultPageSetup directly.

Setting PageFormat works as expected if you do not assign the Clone().

hoppe wrote:
I'm not sure how I'm supposed to attach the working sample project if the file size limit is 256 KiB.
Remove the BIN and OBJ folders and the NuGet packages from the ZIP.
Unless you have to include large PDFs, images, or fonts to replicate the problem, the remaining ZIP should be well below 256 KiB.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed May 17, 2017 2:18 pm 
Offline

Joined: Tue May 16, 2017 7:56 pm
Posts: 2
Thanks! I cleaned the project and it was still huge. I didn't think to remove the packages :)


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 120 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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group