【问题标题】:How to add Header and Footer to a PDF with iText 7如何使用 iText 7 在 PDF 中添加页眉和页脚
【发布时间】:2020-04-26 12:37:23
【问题描述】:

使用 iTextSharp,您可以通过将事件附加到 PDF 来将页眉/页脚添加到 PDF,如此 SO 答案中所述:https://stackoverflow.com/a/19004392

如何使用 iText 7 做同样的事情?

This link 有 Java 代码示例,但看起来不像它使用页面事件。

【问题讨论】:

标签: c# pdf-generation itext7


【解决方案1】:

iText 7 .Net 示例TextFooter.cs 说明了如何通过事件自动添加页眉和页脚:

public class TextFooter
{
    public static readonly String DEST = "results/sandbox/events/text_footer.pdf";

    public static void Main(String[] args)
    {
        FileInfo file = new FileInfo(DEST);
        file.Directory.Create();

        new TextFooter().ManipulatePdf(DEST);
    }

    protected void ManipulatePdf(String dest)
    {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));

        for (int i = 0; i < 3; i++)
        {
            doc.Add(new Paragraph("Test " + (i + 1)));
            if (i != 2)
            {
                doc.Add(new AreaBreak());
            }
        }

        doc.Close();
    }

    private class TextFooterEventHandler : IEventHandler
    {
        protected Document doc;

        public TextFooterEventHandler(Document doc)
        {
            this.doc = doc;
        }

        public void HandleEvent(Event currentEvent)
        {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
            Rectangle pageSize = docEvent.GetPage().GetPageSize();
            PdfFont font = null;
            try {
                font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_OBLIQUE);
            }
            catch (IOException e) 
            {
                Console.Error.WriteLine(e.Message);
            }

            float coordX = ((pageSize.GetLeft() + doc.GetLeftMargin())
                             + (pageSize.GetRight() - doc.GetRightMargin())) / 2;
            float headerY = pageSize.GetTop() - doc.GetTopMargin() + 10;
            float footerY = doc.GetBottomMargin();
            Canvas canvas = new Canvas(docEvent.GetPage(), pageSize);
            canvas
                .SetFont(font)
                .SetFontSize(5)
                .ShowTextAligned("this is a header", coordX, headerY, TextAlignment.CENTER)
                .ShowTextAligned("this is a footer", coordX, footerY, TextAlignment.CENTER)
                .Close();
        }
    }
}

【讨论】:

  • AddEventHandler 没有显示为每个 IntelliSense 的 Document 方法。我需要包含哪些程序集/命名空间?
  • @joym8 "AddEventHandler 没有根据 IntelliSens 显示为 Document 的方法" - 这并不奇怪,因为它是 PdfDocument 的方法,而不是 @ 987654328@。请参阅引用的示例,pdfDoc.AddEventHandler for PdfDocument pdfDoc 被称为...
【解决方案2】:

Github 中添加一个代码示例作为答案(它将单词“复制”作为标题添加到现有 PDF 文件中)。

 protected void ManipulatePdf(String dest) 
    {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Paragraph header = new Paragraph("Copy")
                .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
                .SetFontSize(14)
                .SetFontColor(ColorConstants.RED);

        for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) 
        {
            Rectangle pageSize = pdfDoc.GetPage(i).GetPageSize();
            float x = pageSize.GetWidth() / 2;
            float y = pageSize.GetTop() - 20;
            doc.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
        }

        doc.Close();
    }

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 2015-12-05
    • 2013-11-08
    • 2013-11-20
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多