Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在起始页itextPdf上添加对象(最后一页除外)_Java_Itext - Fatal编程技术网

Java 在起始页itextPdf上添加对象(最后一页除外)

Java 在起始页itextPdf上添加对象(最后一页除外),java,itext,Java,Itext,我正在为所有页面在页面顶部添加一个矩形,但我不希望最后一页上的矩形。这是我的密码: @Override public void onStartPage(PdfWriter writer, Document output) { Font bold = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD); bold.setStyle(Font.UNDERLINE); bold.setColor

我正在为所有页面在页面顶部添加一个矩形,但我不希望最后一页上的矩形。这是我的密码:

 @Override
    public void onStartPage(PdfWriter writer, Document output) {
        Font bold = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD);
        bold.setStyle(Font.UNDERLINE);
        bold.setColor(new BaseColor(171, 75, 15));
        PdfContentByte cb = writer.getDirectContent();
        // Bottom left coordinates x & y, followed by width, height and radius of corners.
        cb.roundRectangle(100f, 1180f, 400f, 100f, 5f);//I dont want this on the ;ast page
        cb.stroke();
        try {
            output.add(new Paragraph("STATEMENT OF ACCOUNT", bold));
            output.add(new Paragraph(new Phrase(new Chunk(" "))));
            output.add(new Paragraph(new Phrase(new Chunk(" "))));
            output.add(new Paragraph(new Phrase(new Chunk(" "))));
            output.add(new Paragraph(new Phrase(new Chunk(" "))));
            Image logo = Image.getInstance(imagepath);
            logo.setAbsolutePosition(780, 1230);
            logo.scaleAbsolute(200, 180);
            writer.getDirectContent().addImage(logo);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

有没有办法从文档的最后一页跳过或删除此矩形?

首先,iText开发人员经常强调,在onStartPage中,不能向PDF添加内容。原因是在某些情况下,会创建未使用的页面并为其调用onStartPage,但随后会删除这些页面。但是,如果在onStartPage中向它们添加内容,它们不会被删除,而是保留在文档中

因此,始终使用OneDPage向页面添加任何内容

在您的用例中,使用onEndPage还有另一个原因:通常只有在文档中添加了最后一点内容后,才能清楚地知道给定的页面是最后一页。这通常发生在为页面调用onStartPage之后,但在onEndPage之前

因此,在将最后一点常规页面内容添加到文档后,只需在页面事件侦听器中设置一个标志,表明当前页面是最终的文档页面。现在,下面的onedpage调用知道它处理最终页面,并且可以以不同的方式添加内容

因此,页面事件侦听器将如下所示

class MyPageEventListener extends PdfPageEventHelper {
    public boolean lastPage = false;

    @Override
    public void onEndPage(PdfWriter writer, Document output) {
        if (!lastPage) {
            [add extra content for page before the last one]
        } else {
            [add extra content for last page]
        }
    }

    ...
}
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, TARGET);
MyPageEventListener pageEventListener = new MyPageEventListener();
writer.setPageEvent(pageEventListener);
document.open();
[add all regular content to the document]
pageEventListener.lastPage = true;
document.close();
然后像这样使用

class MyPageEventListener extends PdfPageEventHelper {
    public boolean lastPage = false;

    @Override
    public void onEndPage(PdfWriter writer, Document output) {
        if (!lastPage) {
            [add extra content for page before the last one]
        } else {
            [add extra content for last page]
        }
    }

    ...
}
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, TARGET);
MyPageEventListener pageEventListener = new MyPageEventListener();
writer.setPageEvent(pageEventListener);
document.open();
[add all regular content to the document]
pageEventListener.lastPage = true;
document.close();

非常感谢。我没有关于在onStartPage添加内容的一点信息。你的方法对我的情况也很有效。