When an Element
gets moved to a new page because of a setKeepTogether(true)
configuration, I need to a text "Continutation of ..." to the new page.
I tried that with the PageEvent.onStartPage()
but it's not allowed to call Document.add()
inside a PageEvent
.
But how to solve that now?
This small example reproduces my problem:
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();
}
}
The result should look somehow like this.