【问题标题】:iTextsharp - Repeat a html table on top of every pageiTextsharp - 在每个页面的顶部重复一个 html 表格
【发布时间】:2016-08-24 14:47:13
【问题描述】:

我打算使用 iTextsharp 创建发票,在我的发票中,它由 3 个部分组成,分别是

  1. 页面顶部的表格(包括所有供应商信息)
  2. gridview(购买项目)
  3. 签名部分(仅在发票最后一页)

到目前为止,我使用 Pdfptable + splitlate 完成了 gridview 部分

       PdfPTable table = new PdfPTable(gv.Columns.Count);
       table.AddCell(new PdfPCell(new Phrase(cellText, fontH1)));
       ...
       ...
        //create PDF document
        Document pdfDoc = new Document(PageSize.A4, -30, -30, 15f, 15f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(table);
        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" + "filename=GridViewExport.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();

但我不知道如何在每一页上插入表格。我打算使用 html 表格,因为它需要控制很多东西,比如差异供应商、地址、显示/隐藏或取消图像。请帮忙。

【问题讨论】:

    标签: c# itext


    【解决方案1】:

    您的问题以前曾被问过。请参阅官方文档中的How to add HTML headers and footers to a page?,或查看 StackOverflow 上的How to add HTML headers and footers to a page?

    Roman Sidorov 的回答是错误的,因为 Roman 假设您正在从您的代码中触发 NewPage()。这并不总是正确的。您向Document 添加一个表,该表跨越多个页面。这意味着 iText 在内部触发了NewPage() 函数。

    您可以使用页面事件将内容添加到创建的每个页面。 OnEndPage() 事件在执行 NewPage() 操作之前触发。这是您向当前页面添加额外内容的时候。 OnStartPage() 事件在执行NewPage() 操作后立即触发。禁止在OnStartPage() 事件中添加内容。见iTextSharp - Header and Footer for all pages

    这是一个用 Java 实现的页面事件示例:

    public class HeaderFooter extends PdfPageEventHelper {
        protected ElementList header;
        protected ElementList footer;
        public HeaderFooter() throws IOException {
            header = XMLWorkerHelper.parseToElementList(HEADER, null);
            footer = XMLWorkerHelper.parseToElementList(FOOTER, null);
        }
        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            try {
                ColumnText ct = new ColumnText(writer.getDirectContent());
                ct.setSimpleColumn(new Rectangle(36, 832, 559, 810));
                for (Element e : header) {
                    ct.addElement(e);
                }
                ct.go();
                ct.setSimpleColumn(new Rectangle(36, 10, 559, 32));
                for (Element e : footer) {
                    ct.addElement(e);
                }
                ct.go();
            } catch (DocumentException de) {
                throw new ExceptionConverter(de);
            }
        }
    }
    

    您可以轻松地将其移植到 C#。我使用这个答案是因为它是对一个字面问题的字面答案。但是:为什么要在 HTML 中定义页眉(或页脚)。这没有意义,不是吗?

    您为什么不创建一个PdfPTable 并将其添加到页面事件中的每个页面。这在对问题How to add a table as a header?的回答中进行了解释,官方文档的page events部分中还有许多其他示例。

    【讨论】:

    • 感谢您的回答@Bruno Lowagie 我正在考虑使用 HTML 来做标题,因为里面的表格包含许多信息,如字体样式、图像、地址、电话等。对我来说,不使用 html table,将所有信息安排在 header 内对我来说是一个很大的挑战。或者你有任何设计标题的教程/解决方案?谢谢你再次回答
    • 不要指望 XML Worker 以与您在 HTML 中定义的方式完全相同的方式呈现表格。您似乎正在使用 iTextSharp 5。在这种情况下,您可以阅读“iText in Action - Second Edition”。然而,最新版本是 iText 7,这里解释了如何使用 iText 7:iText 7: building blocks。 iText 5 和 iText 7 之间存在巨大差异。
    • 嗨@Bruno Lowagie,对不起,我是编程新手,请问实现iText 7 是否与iTextSharp 5 相同?只需将 dll 添加到 Visual Studio 作为参考?谢谢
    • 现在它是一组 DLL,因为我们使 iText 7 更加模块化。另外:XML worker 还不能用于 iText 7(我们正在努力)。 API 发生了重大变化。
    • 嗨@Bruno Lowaige 对不起,请问您提到的页面事件示例对itextsharp 有效吗?
    猜你喜欢
    • 2017-10-11
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多