【问题标题】:How to append the text "Continuation of ..." at the new page, when an element got split by setKeepTogether(true)当元素被 setKeepTogether(true) 拆分时,如何在新页面上附加文本“...的继续”
【发布时间】:2014-03-04 13:35:07
【问题描述】:

ElementsetKeepTogether(true) 配置而被移动到新页面时,我需要在新页面上输入“Continuation of ...”

I tried thatPageEvent.onStartPage(),但不允许在PageEvent 内调用Document.add()

但是现在如何解决呢?

这个小例子重现了我的问题:

public class Main {
    public static String continuation = 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"));

        document.open();

        // Now I add the introduction text
        document.add(new Paragraph("A nice \nIntroduction \nover some lines."));

        // Now I put my "huge" paragraph. When it breaks,
        // the first line of the new page should be "Continuation of smth."
        continuation = "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();
    }
}

结果应该看起来像this

【问题讨论】:

    标签: java itext


    【解决方案1】:

    这可以通过使用headerRowsPdfPTable 来破解。

    这真是一个hack,也许其他人有更好的主意来达到目标​​:

    public static void main(final String[] args) throws Exception
    {
        final Document document = new Document(PageSize.A7);
    
        PdfWriter.getInstance(document, new FileOutputStream("text.pdf"));
    
        document.open();
    
        // The content gets wrapped into an table. If the table breaks
        // we utilize the "headerRows" to print `Continuation of xx`.
        final PdfPTable table = new PdfPTable(1);
        table.setHeadersInEvent(false);
        table.setHeaderRows(1);
        table.setSkipFirstHeader(true);
        table.setWidthPercentage(100);
        table.getDefaultCell().setBorder(0);
        table.getDefaultCell().setPadding(0);
    
        // This is the header we print ONLY if the page breaks.
        table.addCell("Continuation of " + "smth.");
    
        // Now I add the introduction text
        final PdfPTable firstText = new PdfPTable(1);
        firstText.addCell("A nice \nIntroduction \ninto \"smth.\" \nover some lines.");
        // in the original code i add some other things here as well
        table.addCell(firstText);
    
        // Now I put a "huge" paragraph (it will not fit on the first page
        final PdfPTable secondText = new PdfPTable(1);
        secondText.addCell("Force \na pagebreak\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf.");
        // in the original code i add some other things here as well
        table.addCell(secondText);
    
        document.add(table);
    
        document.close();
    }
    

    代码生成正确的 PDF。但是方式真的很丑……

    我已上传 PDF here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多