如何在 C# 中使用 iText 添加标题到 PDF 文件?

发布于 2024-10-10 00:55:02 字数 454 浏览 0 评论 0原文

我正在尝试使用 iText 创建 PDF。我正在尝试添加将出现在每个页面上的标题,但我想添加一些文本,例如每个页面都不同的报告名称。我该如何解决这个问题?如果您不明白我想问什么,请参阅下面的示例:

在第 1 页中

Report Name: Test Report

Emp_id  Emp_Name  Emp_sal 

-----    -----      ------
-----    -----      ------
-----    -----      ------

在第 2 页中

Emp_id  Emp_Name  Emp_sal 

-----    -----      ------
-----    -----      ------
-----    -----      ------

注意: 在第 2 页中,“报告名称”不重复。

I am trying to create a PDF using iText. I am trying to add a header that will appear on every page, but I want add some text like the report name that will be different for every page. How can I solve this problem? If you don't understand what I'm trying to ask, please see the example below:

In Page 1

Report Name: Test Report

Emp_id  Emp_Name  Emp_sal 

-----    -----      ------
-----    -----      ------
-----    -----      ------

In Page 2

Emp_id  Emp_Name  Emp_sal 

-----    -----      ------
-----    -----      ------
-----    -----      ------

Note: In Page 2, "Report Name" is not repeating.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情定在深秋 2024-10-17 00:55:02

在 iText 5 中,您可以使用页面事件创建自定义标题。

using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace PDFLib.PageEvents
{ 
    public class CustomPageEvent : PdfPageEventHelper
    {
        private int _page;
        private readonly Rectangle _marges;
        private string _text;

        private static readonly Font FontHf = FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 9);

        private readonly Dictionary<int, float> _posicions;

        public CustomPageEvent(string text){
            _text = text;
            _marges = new Rectangle(10, 10, _pageSize.Width - 20, _pageSize.Height - 20);
            _posicions = new Dictionary<int, float>
                         {
                             {Element.ALIGN_LEFT, _marges.Left + 10},
                             {Element.ALIGN_RIGHT, _marges.Right - 10},
                             {Element.ALIGN_CENTER, (_marges.Left + _marges.Right)/2}
                         };
        }

        public override void OnStartPage(PdfWriter writer, Document document)
        {
            _page++;

            base.OnStartPage(writer, document);
        }

        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            _page = 0;
        }

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            if (page==1)
                ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase(string.Format("{0}", _text), FontHf), _posicions[Element.ALIGN_CENTER], _marges.Top-10, 0);

            base.OnEndPage(writer,document);
        }

    }
}

像这样的东西。它可以被简化,因为我从一个页面事件中提取了它,该事件做了很多事情,例如添加水印和自定义页眉和页脚。

In iText 5 you can create custom headers using page events.

using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace PDFLib.PageEvents
{ 
    public class CustomPageEvent : PdfPageEventHelper
    {
        private int _page;
        private readonly Rectangle _marges;
        private string _text;

        private static readonly Font FontHf = FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 9);

        private readonly Dictionary<int, float> _posicions;

        public CustomPageEvent(string text){
            _text = text;
            _marges = new Rectangle(10, 10, _pageSize.Width - 20, _pageSize.Height - 20);
            _posicions = new Dictionary<int, float>
                         {
                             {Element.ALIGN_LEFT, _marges.Left + 10},
                             {Element.ALIGN_RIGHT, _marges.Right - 10},
                             {Element.ALIGN_CENTER, (_marges.Left + _marges.Right)/2}
                         };
        }

        public override void OnStartPage(PdfWriter writer, Document document)
        {
            _page++;

            base.OnStartPage(writer, document);
        }

        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            _page = 0;
        }

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            if (page==1)
                ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase(string.Format("{0}", _text), FontHf), _posicions[Element.ALIGN_CENTER], _marges.Top-10, 0);

            base.OnEndPage(writer,document);
        }

    }
}

Something like this. It can be simplified as I extracted this from a page event that did many more things as adding watermarks and custom headers and footers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文