java 如何在使用 itextpdf 生成 pdf 文件时制作页脚

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15918382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:17:15  来源:igfitidea点击:

How to make a footer in generating a pdf file using an itextpdf

javafooteritextpdf

提问by Weddy

I have been searching the net on how to make or set a footer using itextpdf in java. and up until now i havent found anything on how to do it. I have seen some articles on how to use and set a header. but not footers. here's a sample code

我一直在网上搜索如何使用 java 中的 itextpdf 制作或设置页脚。直到现在我还没有找到任何关于如何做到这一点的信息。我看过一些关于如何使用和设置标题的文章。但不是页脚。这是一个示例代码

Document document = new Document(PageSize.LETTER);

Paragraph here = new Paragraph();
Paragraph there = new Paragraph();

Font Font1 = new Font(Font.FontFamily.HELVETICA, 9, Font.BOLD);

here.add(new Paragraph("sample here", Font1));
there.add(new Paragraph("sample there", Font1));
//footer here 

document.add(here);
document.add(there);
document.add(footer);

回答by Pranav Kumar

For implementing Header and footer you need to implement a HeaderFooter class that extends
PdfPageEventHelper class of iText API. Then override the onEndPage()to set header and footer. In this example I am settingnamein header and 'page mumber` in footer.

为了实现页眉和页脚,您需要实现一个 HeaderFooter 类,该类扩展
了 iText API 的 PdfPageEventHelper 类。然后覆盖onEndPage()设置页眉和页脚。在这个例子中,我name在页眉中设置,在页脚中设置“页面编号”。

In pdf creation side code you need to use HeaderAndFooterclass like this:

在 pdf 创建端代码中,您需要使用这样的HeaderAndFooter类:

    Document document = new Document(PageSize.LETTER);
    PdfWriter writer = PdfWriter.getInstance(document, "C:\sample.pdf");
    //set page event to PdfWriter instance that you use to prepare pdf
    writer.setPageEvent(new HeaderAndFooter(name));
    .... //Add your content to documne here and close the document at last

    /*
     * HeaderAndFooter class
     */
    public class HeaderAndFooter extends PdfPageEventHelper {

    private String name = "";


    protected Phrase footer;
    protected Phrase header;

    /*
     * Font for header and footer part.
     */
    private static Font headerFont = new Font(Font.COURIER, 9,
            Font.NORMAL,Color.blue);

    private static Font footerFont = new Font(Font.TIMES_ROMAN, 9,
            Font.BOLD,Color.blue);


    /*
     * constructor
     */
    public HeaderAndFooter(String name) {
        super();

        this.name = name;


        header = new Phrase("***** Header *****");
        footer = new Phrase("**** Footer ****");
    }


    @Override
    public void onEndPage(PdfWriter writer, Document document) {

        PdfContentByte cb = writer.getDirectContent();

        //header content
        String headerContent = "Name: " +name;

        //header content
        String footerContent = headerContent;
        /*
         * Header
         */
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(headerContent,headerFont), 
                document.leftMargin() - 1, document.top() + 30, 0);

        /*
         * Foooter
         */
        ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase(String.format(" %d ", 
                writer.getPageNumber()),footerFont), 
                document.right() - 2 , document.bottom() - 20, 0);

    }

}

Hope it helps. I had used this in one of the allication.

希望能帮助到你。我曾在其中一个联盟中使用过它。