7

How can you add a new page to an iText document? document.newPage(); doesn't seem to work.

I am using iText with RTF support from http://sourceforge.net/projects/itextrtf/

Part of my code:

Font titleFont = new Font(Font.COURIER, 14, Font.BOLD);
document.add(new Paragraph("Title1", titleFont));

Table table = new Table(4);
table.setBorderWidth(0);

// Filling table

document.add(table);

document.newPage();

document.add(new Paragraph("Title2", titleFont));

Table table = new Table(4);
table.setBorderWidth(0);

// Filling table

document.add(table);
Mark Storer
  • 15,672
  • 3
  • 42
  • 80
Thizzer
  • 16,153
  • 28
  • 98
  • 139

3 Answers3

10

Edit: Re your updated question with code, neither of the below seems to apply. Leaving in case they help someone else out.

Calling newPage tells iText to place subsequent objects on a new page. The new page will only actually get created when you place the next object (at least, that's what it does for me). Also, newPage only creates a new page if the current page is not blank; otherwise, it's ignored; you can use setPageBlank(false) to overcome that.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Added some example code, I am adding new Elements after newPage is called. – Thizzer Nov 26 '10 at 11:13
  • @MrThys: V. strange. I have code that looks quite similar and works. Sorry not to have another idea for you, good luck with it. – T.J. Crowder Nov 26 '10 at 11:16
  • @MyThys: Not really, it's too spread out, but it basically comes down to adding some objects (images or text), calling `newPage`, and adding more objects (images or text). – T.J. Crowder Nov 26 '10 at 14:19
  • Additional : After called `newPage()`,Define as `table= new PdfPTable(4)` and then add 3 cell into `table`.The last page of three cells is disappeared.In this case, add `4 or 4*n` cells seems worked well. – Ayano Jan 18 '18 at 08:05
0

RTF is no longer supported by iText, as the main author of the relevant code moved on to other projects... or was transformed into a frog... or something. Anyway, I recommend you seek a new RTF library, or perhaps start maintaining it yourself?

At any rate, the Source Is Available, and I suspect the RTFDocument/RTFWriter ignores newPage(). Nope. RtfWriter2.java:

/**
 * Adds a page break
 *
 * @return <code>false</code>
 */
public boolean newPage() {
    rtfDoc.add(new RtfNewPage(rtfDoc));
    return true;
}

which should just write "//page" into the output file. Is it there?

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
  • Hi Mark, I/m facing same issue with pdfContentbByte. http://stackoverflow.com/questions/27737501/how-to-get-new-page-in-itext . Any suggestion? – Sai prateek Jan 02 '15 at 05:29
0

The problem was I was using a wrong RTF reader, the breakline was there, the reader just didn't render it.

Thizzer
  • 16,153
  • 28
  • 98
  • 139