PdfSharpCore icon indicating copy to clipboard operation
PdfSharpCore copied to clipboard

Migradoc dynamic page size (height)

Open DarioSalcedo opened this issue 3 years ago • 0 comments

Hi, I'm using Migradoc to generate PDF documents. Now I need to generate a single page document with fixed width and dynamically calculated height based on the content height.

initially I assign a very large height

 currentSection.PageSetup = document.DefaultPageSetup.Clone();
            currentSection.PageSetup.PageWidth = "80 mm"; // Unit.FromPoint(212.598F); 
            currentSection.PageSetup.PageHeight = Unit.FromPoint(14400F);

In the original way, it used this code to recalculate the height of the document (set to a ridiculously large height at the beginning). But now I don't know how to do it

Based on: https://stackoverflow.com/questions/23708750/migradoc-dynamic-page-size

        // Recalculamos la altura (establecida a una altura ridículamente grande al principio), para que la impresora sepa donde cortar el papel.
            DocumentRenderer renderer = new DocumentRenderer(document);
            renderer.PrepareDocument();
            MigraDocCore.Rendering.RenderInfo[] info = renderer.GetRenderInfoFromPage(1);
            int index = info.Length - 1;

            double stop = info[index].LayoutInfo.ContentArea.Y.Millimeter + info[index].LayoutInfo.ContentArea.Height.Millimeter
                + 2 * Tamanios.TopBottomPageMargin; // Add more if you have bottom page margin, borders on the last table etc.
            currentSection.PageSetup.PageHeight = Unit.FromMillimeter(stop);        

Please help me

DarioSalcedo avatar Apr 23 '22 17:04 DarioSalcedo