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

Migradoc legal page format leaves big margin at top
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3589
Page 1 of 1

Author:  hoppe [ Tue May 16, 2017 8:10 pm ]
Post subject:  Migradoc legal page format leaves big margin at top

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.

Author:  Thomas Hoevel [ Wed May 17, 2017 8:29 am ]
Post subject:  Re: Migradoc legal page format leaves big margin at top

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.

Author:  hoppe [ Wed May 17, 2017 2:18 pm ]
Post subject:  Re: Migradoc legal page format leaves big margin at top

Thanks! I cleaned the project and it was still huge. I didn't think to remove the packages :)

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