【问题标题】:PDF Page Numbering in Java & iTextJava和iText中的PDF页面编号
【发布时间】:2012-06-27 16:17:51
【问题描述】:

我正在使用 Java 中的 iText 创建一些 PDF 报告。根据要求,我应该按照page_number/page_numbers_in_total的格式对页面进行编号。

但是,内存操作给我的项目带来了负担。因此,我不想为了给它们编号而再次浏览所有页面。有什么方法可以实现吗?

【问题讨论】:

    标签: java pdf itext page-numbering


    【解决方案1】:

    看看this example,它设置了一个类似的标题(“Page X of Y”):

    您会看到onEndPage 方法打印"Page X of "onCloseDocument 方法在所有页面上设置"Y"通过 PdfTemplate。

    【讨论】:

    • 知道如何在 iText7 中执行此操作...?我正在努力寻找一个类似的例子!
    【解决方案2】:

    标记为正确的答案对我有用。 我刚刚花了很多时间重构代码(字体大小、位置)并使其适用于所有现有的 PDF。

    注意页面的编号在页面的右下角,一些可选文本在左下角。

    下面是使用之前的注释创建的PageNumeration类的代码。

    package demo;
    
    import com.itextpdf.text.BaseColor;
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Element;
    import com.itextpdf.text.ExceptionConverter;
    import com.itextpdf.text.Font;
    import com.itextpdf.text.Image;
    import com.itextpdf.text.Phrase;
    import com.itextpdf.text.Rectangle;
    import com.itextpdf.text.pdf.BaseFont;
    import com.itextpdf.text.pdf.ColumnText;
    import com.itextpdf.text.pdf.PdfPCell;
    import com.itextpdf.text.pdf.PdfPTable;
    import com.itextpdf.text.pdf.PdfPageEventHelper;
    import com.itextpdf.text.pdf.PdfTemplate;
    import com.itextpdf.text.pdf.PdfWriter;
    
    class PageNumeration extends PdfPageEventHelper {
    /** The template with the total number of pages. */
    PdfTemplate total;
    
    private Font normal, normalSmall;
    private Company company;
    
    public PageNumeration (){
        try{
            this.normal = new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 8);
            this.normalSmall = new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 6);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 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, 12);
    }
    
    /**
     * 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(3);
        try {
            table.setWidths(new int[]{24, 24, 2});
            table.getDefaultCell().setFixedHeight(10);
            table.getDefaultCell().setBorder(Rectangle.TOP);
            PdfPCell cell = new PdfPCell();
            cell.setBorder (0);
            cell.setBorderWidthTop (1);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setPhrase(new Phrase("some text", normalSmall));
            table.addCell(cell);
    
            cell = new PdfPCell();
            cell.setBorder (0);
            cell.setBorderWidthTop (1);
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell.setPhrase(new Phrase(String.format("Page %d of", writer.getPageNumber()), normal));
            table.addCell(cell);
    
            cell = new PdfPCell(Image.getInstance(total));
            cell.setBorder (0);
            cell.setBorderWidthTop (1);
            table.addCell(cell);
            table.setTotalWidth(document.getPageSize().getWidth()
                    - document.leftMargin() - document.rightMargin());
            table.writeSelectedRows(0, -1, document.leftMargin(),
                    document.bottomMargin() - 15, 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), normal),
                2, 2, 0);
        }
    }  
    

    创建此类后,只需在 PDF 类中实例化它,并将事件设置在编写器定义的下方。

    writer = PdfWriter.getInstance(document, new  FileOutputStream("somepath/somedir/file.pdf"));
    PageNumeration event = new PageNumeration(this.company);
    writer.setPageEvent(event);
    

    希望这会有所帮助。

    【讨论】:

    • 公司是什么公司?
    【解决方案3】:

    你可以用@page样式表处理页码像这样 @page { @top-right { 内容:“页面”计数器(页面)“的”计数器(页面); } }

    当您需要页码/总页数时,请尝试以下操作: 计数器(页)“/”计数器(页)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多