Java 如何强制在将内容写入文档之前调用iText PageEvent.onStartPage()

Java 如何强制在将内容写入文档之前调用iText PageEvent.onStartPage(),java,itext,Java,Itext,通常在编写任何内容之前调用onStartPage() 在我的用例中,在将某些内容写入新页面后,它会以某种方式调用 我所做的: 创建一个带有介绍的段落 使用setKeepTogether(true)创建一个元素。这个元素太大了,iText将把它放在下一页上(正确的行为是什么) 现在,在将元素添加到新页面之后,调用onStartPage() 我依赖这种方法,因为我需要在页面顶部添加一个小文本,如xx的延续 此示例再现了以下行为: public class Main { public stat

通常在编写任何内容之前调用
onStartPage()

在我的用例中,在将某些内容写入新页面后,它会以某种方式调用

我所做的:

  • 创建一个带有介绍的
    段落
  • 使用
    setKeepTogether(true)
    创建一个元素。这个元素太大了,iText将把它放在下一页上(正确的行为是什么)
  • 现在,在将元素添加到新页面之后,调用
    onStartPage()
  • 我依赖这种方法,因为我需要在页面顶部添加一个小文本,如xx的延续

    此示例再现了以下行为:

    public class Main {
        public static String someTompic = null;
    
        public static void main(final String[] args) throws Exception {
            final Document document = new Document(PageSize.A7);
    
            final PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("text.pdf"));
    
            // This PageEventHelper should write a small text "Continuation: ..." at the beginning if
            // a PageBreak was forced by "keepTogether"
            writer.setPageEvent(new PdfPageEventHelper() {
    
                @Override
                public void onStartPage(final PdfWriter writer, final Document document) {
                    try {
                        if (someTompic != null)
                            document.add(new Paragraph("Continuation: " + someTompic));
                    }
                    catch (final DocumentException e) {
                        e.printStackTrace();
                    }
                }
            });
    
            document.open();
    
            // Now I add the introduction text
            document.add(new Paragraph("A nice \nIntroduction \nover some lines."));
    
            // Now I put my "huge" thing. If this breaks,
            // the first line of the new page should be Continuation of ...
            someTompic = "smth.";
    
            final Paragraph paragraph = new Paragraph("What is \nthe answer \nto life the \nuniverse and \neverything?\n\nThe Answer to \nthis question \nis:\n42");
            paragraph.setKeepTogether(true);
            document.add(paragraph);
    
            document.close();
        }
    }
    

    此链接显示损坏的PDF:

    a)在启动页面上的
    过程中,您不需要进行任何文档更改。您可以使用它来检索页面启动时的某些状态信息(例如,当时的当前章节)。b) 在任何
    PdfPageEvent
    方法实现中,都不需要向
    文档
    实例添加内容。您可以在
    onEndPage
    中添加直接
    PdfWriter
    内容,例如,页面顶部的一些标题,可能利用了在
    onStartPage
    期间检索到的章节信息。您是对的。文档上说“不要使用Document.add()”。。。你知道如何在页面顶部插入“Continuation:…”吗?@mkl我将这篇文章链接到一个问题:“当一个元素被setkeepTogether(true)拆分时,如何在新页面追加文本“Continuation of…”。也许你可以帮我。你知道如何在页面顶部插入“Continuation:…”吗正如我在前面的评论中所暗示的:仅使用
    onStartPage
    检查是否需要添加“Continuation of…”,并将其存储在
    PdfPageEvent
    实现的变量中;然后在
    onEndPage
    中检查该变量,如果它是
    true
    ,则使用
    PdfWriter.getDirectContent
    @mkl在页面顶部添加“Continuation of…”,这可能是个好主意。但问题是,在新的一页上,段落已经使用了上页边距正下方的空格。所以没有空间写“继续:…”(或者我必须在页边空白处写下看起来不好的:()。