Esempio n. 1
0
        private static void AddSection(PdfDocument pdfDoc, Paragraph paragraph, int pageNumber, int sectionNumber)
        {
            Document          doc      = new Document(pdfDoc);
            ParagraphRenderer renderer = (ParagraphRenderer)paragraph.CreateRendererSubTree();

            renderer.SetParent(new DocumentRenderer(doc));

            float     pageHeight           = pdfDoc.GetDefaultPageSize().GetHeight();
            float     pageWidth            = pdfDoc.GetDefaultPageSize().GetWidth();
            Rectangle textSectionRectangle = new Rectangle(
                doc.GetLeftMargin(),
                doc.GetBottomMargin() + ((pageHeight - doc.GetTopMargin() - doc.GetBottomMargin()) / 3) * sectionNumber,
                pageWidth - doc.GetLeftMargin() - doc.GetRightMargin(),
                (pageHeight - doc.GetTopMargin() - doc.GetBottomMargin()) / 3);

            // Simulate the positioning of the renderer to find out how much space the text section will occupy.
            LayoutResult layoutResult = renderer
                                        .Layout(new LayoutContext(new LayoutArea(pageNumber, textSectionRectangle)));

            /* Fill the current page section with the content.
             * If the content isn't fully placed in the current page section,
             * it will be split and drawn in the next page section.
             */
            while (layoutResult.GetStatus() != LayoutResult.FULL)
            {
                if (pdfDoc.GetNumberOfPages() < pageNumber)
                {
                    pdfDoc.AddNewPage();
                }

                pageNumber++;

                layoutResult.GetSplitRenderer().Draw(new DrawContext(pdfDoc,
                                                                     new PdfCanvas(pdfDoc.GetPage(pageNumber - 1)), false));

                renderer = (ParagraphRenderer)layoutResult.GetOverflowRenderer();

                layoutResult = renderer
                               .Layout(new LayoutContext(new LayoutArea(pageNumber, textSectionRectangle)));
            }

            if (pdfDoc.GetNumberOfPages() < pageNumber)
            {
                pdfDoc.AddNewPage();
            }

            renderer.Draw(new DrawContext(pdfDoc, new PdfCanvas(pdfDoc.GetPage(pageNumber)), false));
        }