Itext7 PDF创建页眉,页脚和内容区域

时间:2019-09-27 17:15:47

标签: c# pdf-generation itext7

我是IText7的新手,并且标题重叠,并查看了他们的示例。我创建了一个页脚,现在页脚重叠了。

     class Program
    {
        static string dest = @"HeaderSample.pdf";

        public static void Main(string[] args)
        {
            CreatePdf(dest);

            Console.Read();
        }

        public static  void CreatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, PageSize.LETTER);
            TableHeaderEventHandler handler = new TableHeaderEventHandler(doc);
            pdfDoc.AddEventHandler(PdfDocumentEvent.START_PAGE, handler);
            doc.SetMargins(20 + handler.GetTableHeight(), 36, 36, 36);

            TableFooterEventHandler FooterHandler = new TableFooterEventHandler(doc);
            pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, FooterHandler);

            for (int i = 0; i<150; i++)
            { 
                doc.Add(new Paragraph("Hello World"));
            }

            doc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));

            Table table = new Table(1);

            Cell contractor = new Cell().Add(new Paragraph("ANOTHER PARA."));
            contractor.SetBorder(iText.Layout.Borders.Border.NO_BORDER);
            table.AddCell(contractor);

            doc.Add(table);

            doc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));

            doc.Close();
        }

    }
    public class TableHeaderEventHandler : IEventHandler
    {
        protected Table table;
        protected float tableHeight;
        protected Document doc;

         public TableHeaderEventHandler(Document doc)
        {
            this.doc = doc;
            table = new Table(1);
            table.SetWidth(doc.GetPdfDocument().GetDefaultPageSize().GetRight() - doc.GetPdfDocument().GetDefaultPageSize().GetLeft() - doc.GetLeftMargin() - doc.GetRightMargin());
            table.AddCell("Header row 1");
            table.AddCell("Header row 2");
            table.AddCell("Header row 3");
            TableRenderer renderer = (TableRenderer)table.CreateRendererSubTree();
            renderer.SetParent(new Document(new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))).GetRenderer());
            tableHeight = renderer.Layout(new LayoutContext(new LayoutArea(0, PageSize.LETTER))).GetOccupiedArea().GetBBox().GetHeight();

        }


        public void HandleEvent(Event eventi)
        {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) eventi;
            PdfDocument pdfDoc = docEvent.GetDocument();
            PdfPage page = docEvent.GetPage();
            PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
            Rectangle rect = new Rectangle(pdfDoc.GetDefaultPageSize().GetX() + doc.GetLeftMargin(),
                    pdfDoc.GetDefaultPageSize().GetTop() - doc.GetTopMargin(), 100, GetTableHeight());
            new Canvas(canvas, pdfDoc, rect)
                    .Add(table);

        }

       public float GetTableHeight()
       {
            return tableHeight;
       }
    }

    public class TableFooterEventHandler : IEventHandler
    {
        protected Table table;
        protected float tableHeight;
        protected Document doc;

        public TableFooterEventHandler(Document doc)
        {
            this.doc = doc;
            table = new Table(1);
            table.SetWidth(doc.GetPdfDocument().GetDefaultPageSize().GetRight() - doc.GetPdfDocument().GetDefaultPageSize().GetLeft() - doc.GetLeftMargin() - doc.GetRightMargin());
            table.AddCell("Footer row 1");
            table.AddCell("Footer row 2");
            table.AddCell("Footer row 3");
            TableRenderer renderer = (TableRenderer)table.CreateRendererSubTree();
            renderer.SetParent(new Document(new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))).GetRenderer());
            tableHeight = renderer.Layout(new LayoutContext(new LayoutArea(0, PageSize.LETTER))).GetOccupiedArea().GetBBox().GetHeight();
        }


        public void HandleEvent(Event eventi)
        {
            PdfDocumentEvent docEvent = (PdfDocumentEvent)eventi;
            PdfDocument pdfDoc = docEvent.GetDocument();
            PdfPage page = docEvent.GetPage();
            PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
            Rectangle rect = new Rectangle(pdfDoc.GetDefaultPageSize().GetX() + doc.GetLeftMargin(),
                    pdfDoc.GetDefaultPageSize().GetBottom() - doc.GetBottomMargin(), 100, GetTableHeight() + 50);
            new Canvas(canvas, pdfDoc, rect)
                    .Add(table);
        }

        public float GetTableHeight()
        {
            return tableHeight;
        }
    }
}
  1. 是否可以控制页眉和页脚之间的内容区域? enter image description here
  2. 不在上面的代码示例中,但我还需要将变量值传递给页眉和页脚并动态呈现它们。我尝试了一些示例,并向标头传递了一个数字,如果我不删除事件处理程序并再次添加事件处理程序,则该值将被覆盖。 (下一页值2中的值1会覆盖1,并且您会在其顶部看到1和2。)

0 个答案:

没有答案