本文介绍了使用iText在页眉和页脚中生成带有图像的pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待使用iText创建pdf.pdf将具有页眉和页脚.页眉和页脚都将带有图像(公司徽标).

I am looking forward to create a pdf using iText.The pdf will have a header and a footer. Both the header and footer will have a image (company logo) in it.

我期待着一个类似的例子.我将第一次使用iText库,因此我不确定从哪里开始.

I am looking forward to an example similar to that. I will be using the iText library for the first time so I am not sure where to start off from.

推荐答案

要将标题和页脚添加到使用iText 7.x生成的PDF中,通常将为页面开始和/或页面结束创建事件侦听器,并添加其中的页眉和页脚内容.

To add headers and footers to a PDF you generate using iText 7.x, you will generally create event listeners for page starts and/or page ends and add the header and footer contents there-in.

例如,您可以在此示例在iText网站上.这里定义了两个事件侦听器:

For example you can do it like in this sample on the iText site. Here two event listeners are defined:

//Header event handler
protected class Header implements IEventHandler {
    String header;
    public Header(String header) {
        this.header = header;
    }
    @Override
    public void handleEvent(Event event) {
        //Retrieve document and
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdf = docEvent.getDocument();
        PdfPage page = docEvent.getPage();
        Rectangle pageSize = page.getPageSize();
        PdfCanvas pdfCanvas = new PdfCanvas(
                page.getLastContentStream(), page.getResources(), pdf);
        Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
        canvas.setFontSize(18f);
        //Write text at position
        canvas.showTextAligned(header,
                pageSize.getWidth() / 2,
                pageSize.getTop() - 30, TextAlignment.CENTER);
    }
}

此事件处理程序将一个简单的常量字符串作为标头添加到当前页面.同样,您也可以添加图片.

This event handler adds a simple constant string as header to the current page. Similarly you can also add an image.

//page X of Y
protected class PageXofY implements IEventHandler {
    protected PdfFormXObject placeholder;
    protected float side = 20;
    protected float x = 300;
    protected float y = 25;
    protected float space = 4.5f;
    protected float descent = 3;
    public PageXofY(PdfDocument pdf) {
        placeholder =
                new PdfFormXObject(new Rectangle(0, 0, side, side));
    }
    @Override
    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdf = docEvent.getDocument();
        PdfPage page = docEvent.getPage();
        int pageNumber = pdf.getPageNumber(page);
        Rectangle pageSize = page.getPageSize();
        PdfCanvas pdfCanvas = new PdfCanvas(
                page.getLastContentStream(), page.getResources(), pdf);
        Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
        Paragraph p = new Paragraph()
                .add("Page ").add(String.valueOf(pageNumber)).add(" of");
        canvas.showTextAligned(p, x, y, TextAlignment.RIGHT);
        pdfCanvas.addXObject(placeholder, x + space, y - descent);
        pdfCanvas.release();
    }
    public void writeTotal(PdfDocument pdf) {
        Canvas canvas = new Canvas(placeholder, pdf);
        canvas.showTextAligned(String.valueOf(pdf.getNumberOfPages()),
                0, descent, TextAlignment.LEFT);
    }
}

此事件处理程序稍微复杂一点,它向当前页面添加了 y 页脚的页面 x .由于当时 y 的页面总数未知,因此将添加对占位符XObject的引用,并且一旦您知道所有页面均已创建,便可以可以调用writeTotal以使此方法将当前文档页面数写入占位符XObject.

This event handler is a bit more complex, it adds a Page x of y footer to the current page. As the total number of pages y is unknown at the time, a reference to a placeholder XObject is added instead, and as soon as you know all the pages are created, you can call writeTotal to have this method write the current number of document pages into the placeholder XObject.

您可以按以下方式注册这些事件侦听器:

You register these event listeners as following:

PdfWriter writer= new PdfWriter(pdfDest);
PdfDocument pdfDocument = new PdfDocument(writer);

//Create event-handlers
String header = "pdfHtml Header and footer example using page-events";
Header headerHandler = new Header(header);
PageXofY footerHandler = new PageXofY(pdfDocument);

//Assign event-handlers
pdfDocument.addEventHandler(PdfDocumentEvent.START_PAGE,headerHandler);
pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE,footerHandler);

//Add content
[... here you add the regular page content to the PdfDocument ...]

//Write the total number of pages to the placeholder and close the document
footerHandler.writeTotal(pdfDocument);
pdfDocument.close();

(实际示例使用pdfHTML添加基于HTML的常规页面内容.您也可以这样做,也可以直接使用DocumentParagraphText和其他布局类生成内容. )

(The actual example adds the regular page content based on HTML using pdfHTML. You may do so, too, or you may generate the content directly using Document, Paragraph, Text, and other layout classes.)

这篇关于使用iText在页眉和页脚中生成带有图像的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:39