【问题标题】:Pagination in ItextPdfItextPdf中的分页
【发布时间】:2015-04-04 16:09:08
【问题描述】:

我们在应用程序中使用 itextpdf 和 xmlworker API(itextpdf-5.1.1.jar、Xmlworker-1.1.0.jar)来执行 PDF 生成任务。我们通过 XML-XSLT 转换生成 HTML 内容,然后使用 HTML 内容创建 pdf 文档。

当我们尝试实现分页(as 1 of 3, 2 of 3..)时,我们从 itextpdf 在线示例中了解到,除了我们创建的 PDF 文档之外,我们始终需要创建一个结果 pdf 文档以便盖章每页内容的页码。

这种方法在删除中间 pdf 文档时带来了挑战。有没有什么方法可以在我们第一次创建 pdf 时确定页码,以避免创建一个结果 pdf 文档?

谢谢 文卡特

class TableFooter extends PdfPageEventHelper {          
      String header;          
      PdfTemplate total;

      public void setHeader(String header) {            
          this.header = header;
      }

      public void onOpenDocument(PdfWriter writer, Document document) {
          total = writer.getDirectContent().createTemplate(30, 16);

      }

      public void onEndPage(PdfWriter writer, Document document) {
          Font ffont = new Font(Font.FontFamily.UNDEFINED, 7, Font.NORMAL);
          PdfPTable table = new PdfPTable(2);
          try 
          {
              table.setWidths(new int[]{1,1});
              table.setTotalWidth(50);
              table.getDefaultCell().setFixedHeight(15);
              table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
              Phrase footer = new Phrase(String.format("%d of", writer.getPageNumber()), ffont);
              table.addCell(footer);                
              PdfPCell cell = new PdfPCell(Image.getInstance(total));
              table.addCell(cell);
              table.writeSelectedRows(0, -1, 275, 20, writer.getDirectContent());
          }
          catch(Exception de) {
              throw new ExceptionConverter(de);
          }
      }

      public void onCloseDocument(PdfWriter writer, Document document) {            
        ColumnText.showTextAligned(total, Element.ALIGN_LEFT, new Phrase(String.valueOf(writer.getPageNumber() - 1)), 10, 4, 0);
      }
  }

【问题讨论】:

    标签: pagination itextpdf


    【解决方案1】:

    请下载免费电子书The Best iText Questions on StackOverflow 并阅读标题为“页面事件”一章中列出的问题。以下是为本书选择的一些问题:

    您还可以查看 iText 官方网站上的keywords page,更具体地说,查看关键字Page X of Y

    在您的问题中,您指的是创建中间文档。这就是TwoPasses 示例的意义所在。您正在寻求一种一次性添加 Page X of Y 的方法。这就是MovieCountries1 示例的意义所在。

    class PageXofYHeader extends PdfPageEventHelper {
        /** The template with the total number of pages. */
        PdfTemplate total;
    
        /**
         * Creates the PdfTemplate that will hold the total number of pages.
         * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(
         *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
         */
        public void onOpenDocument(PdfWriter writer, Document document) {
            total = writer.getDirectContent().createTemplate(30, 16);
        }
    
        /**
         * Adds a header to every page
         * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
         *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
         */
        public void onEndPage(PdfWriter writer, Document document) {
            PdfPTable table = new PdfPTable(2);
            try {
                table.setWidths(new int[]{48, 2});
                table.setTotalWidth(527);
                table.setLockedWidth(true);
                table.getDefaultCell().setFixedHeight(20);
                table.getDefaultCell().setBorder(Rectangle.BOTTOM);
                table.getDefaultCell()
                    .setHorizontalAlignment(Element.ALIGN_RIGHT);
                table.addCell(
                    String.format("Page %d of", writer.getPageNumber()));
                PdfPCell cell = new PdfPCell(Image.getInstance(total));
                cell.setBorder(Rectangle.BOTTOM);
                table.addCell(cell);
                table.writeSelectedRows(0, -1, 34, 803,
                    writer.getDirectContent());
            }
            catch(DocumentException de) {
                throw new ExceptionConverter(de);
            }
        }
    
        /**
         * Fills out the total number of pages before the document is closed.
         * @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(
         *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
         */
        public void onCloseDocument(PdfWriter writer, Document document) {
            ColumnText.showTextAligned(total, Element.ALIGN_LEFT,
                    new Phrase(String.valueOf(writer.getPageNumber() - 1)),
                    2, 2, 0);
        }
    }
    

    如您所见,我们在onOpenDocument() 方法中创建了一个名为total 的小占位符。由于我们事先不知道总页数是多少,因此我们将这个占位符添加到每个页面而不知道总页数是多少。我们只有在关闭文档时才知道最终的页数,这就是为什么我们要等到onCloseDocument() 方法被触发才能将内容添加到占位符。

    【讨论】:

    • 感谢 Bruno Lowagie 详细解释了如何在单程中实现 Y 的第 X 页。由于我们从 XML-XSLT-HTMLContent 方法生成 PDF 文档,我们尝试的类似于 MovieCountries1 的示例程序在页眉上创建了 Y 的页面 X。是否有任何示例可以将 Y 的第 X 页放在页脚区域?你能指导我看样品吗?
    • 不需要额外的样品。添加额外内容时只需更改坐标即可。将 803 替换为 20 之类的内容(由您决定页脚的位置)。
    • 感谢布鲁诺·洛瓦吉。我们尝试更改示例中的坐标,但没有任何帮助使页面总数 (Y) 更接近 X。
    • 是什么让您认为在一系列注释部分粘贴代码是个好主意?没有人会阅读您的代码。请按照预期的方式使用 StackOverflow。另外:您要求将页脚靠近底部。现在您要求使总数 (Y) 更接近 X。你受过教育吗?你上学的时候没学过“解析几何”吗?
    • 您好 Bruno Lowagie,我是 stackoverflow 的新手,我只是将代码粘贴到了一系列评论部分,但不了解如何发布代码 sn-p。现在我已经更新了我原来的帖子创建页脚的内部类的整个部分。我面临的问题是,总页数 (Y) 没有按预期显示每个页面值 (X)。围绕 onCloseDocument 方法中的坐标进行播放,但我们无法成功。我已将 3 个示例输出(带有差异坐标)添加到单个图像文件中以供您注意 [IMG]i59.tinypic.com/2ihqr2e.jpg[/IMG]
    猜你喜欢
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2017-06-14
    • 2017-02-07
    • 2018-05-10
    相关资源
    最近更新 更多