ItextPdf中的分页 [英] Pagination in ItextPdf

查看:1473
本文介绍了ItextPdf中的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

we use itextpdf and xmlworker APIs ( itextpdf-5.1.1.jar, Xmlworker-1.1.0.jar) for PDF generation tasks in our application. We generate HTML content from XML-XSLT conversion and then use the HTML content to create pdf document.

当我们尝试实现分页时(3个中的1个,2个中的3个) ..),我们从itextpdf在线样本中学到了我们总是需要创建一个结果pdf文档,而不是我们创建的PDF文档,以便在每个页面内容上标记页码。

When we trying to implement pagination ( as 1 of 3, 2 of 3..), we learnt from itextpdf online samples that we always need to create a resultant pdf document apart from the PDF document that we create in order to stamp the page number on the each page content.

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

This approach brings challenges in removing the intermediate pdf document. Is there any way by which page numbers can be determined during the time we create the pdf very first time so that we will avoid creating one resultant pdf document?

谢谢
Venkat

Thanks Venkat

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);
      }
  }


推荐答案

请下载免费电子书 StackOverflow上最好的iText问题并阅读下面列出的问题标题为页面事件的章节。这些是本书选择的一些问题:

Please download the free ebook The Best iText Questions on StackOverflow and read the questions listed in the chapter entitled "Page events". These are some of the questions that were selected for this book:

  • How to add multiple headers and footers in pdf using itext
  • How to add text as a header or footer?
  • Creating table with 2 rows in pdf footer using itext
  • ...

您还可以查看官方iText网站上的关键字页面,更具体地说,关键词 Y页的第X页

You can also look at the keywords page on the official iText site, more specifically at the key word Page X of Y.

In你的问题,你指的是创建一个中介电子文件。这就是 TwoPasses 示例的内容。您想要一种方法在一次传递中添加第X页的。这就是 MovieCountries1 示例的内容。

In your question, you refer to creating an intermediate document. That's what the TwoPasses example is about. You are asking for a way to add Page X of Y in a single pass. That's what the MovieCountries1 example is about.

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()方法向占位符添加内容的原因。

As you can see, we create a small place holder named total in the onOpenDocument() method. As we don't know in advance what the total number of pages will be, we add this placeholder to each page without knowing what the page total will be. We only know the final page count when we close the document, and that's why we wait until the onCloseDocument() method is triggered to add content to the placeholder.

这篇关于ItextPdf中的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆