【问题标题】:How to make a footer in generating a pdf file using an itextpdf如何使用 itextpdf 在生成 pdf 文件时制作页脚
【发布时间】:2013-04-01 20:29:16
【问题描述】:

我一直在网上搜索如何在 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);

【问题讨论】:

    标签: java footer itextpdf


    【解决方案1】:

    要实现页眉和页脚,您需要实现一个扩展的 HeaderFooter 类
    iText API 的 PdfPageEventHelper 类。然后覆盖onEndPage() 来设置页眉和页脚。在此示例中,我在页眉中设置name,在页脚中设置“页码”。

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

    希望对您有所帮助。我曾在其中一个联名中使用过它。

    【讨论】:

    • 我想出了另一种方法,但这很有帮助。希望这对其他用户也有帮助:D
    • 感谢您的回答。我只需要添加如下内容:在页脚中,我有 3 行,每行都有不同的字体大小。如何在页脚中添加这三行不同字体的行。我如何估计字体高度并根据它设置它的垂直值 - document.bottom()-XXXX ?
    【解决方案2】:

    带有 itextpdf 5 的小演示

            final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            final Document document = new Document();
            final PdfContentByte cb = PdfWriter.getInstance(document, byteStream).getDirectContent();
    
            final Paragraph footerTitleParagraph = new Paragraph();
            footerTitleParagraph.add("footer title with no style");
            ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footerTitleParagraph, document.right()/2, document.bottom()-15, 0);
    
            final Paragraph footerSubTitleParagraph = new Paragraph("footer sub-title with style", FOOTER_FONT_SUB));
            ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footerSubTitleParagraph, document.right()/2, document.bottom()-25, 0);
    

    【讨论】:

      猜你喜欢
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多