Discussion:
[iText-questions] Problem with page breaks within rowspans
T***@deutschebahn.com
2012-08-30 13:27:41 UTC
Permalink
Hi all,

we are currently evaluating iText for use in a commercial application. So
far it looks quite good with the exception of one critical problem.
Before buying a commercial licence we would like to know whether that
problem can be fixed (or how to get around it).

The problem is as follows: we need to create long multipage tables
containing lots of cells with rowspans.
Certain business rules govern where page breaks occur and the important
thing is that page breaks must be possible _within rowspans_,
resulting in those cells having a remaining rowspan to continue with that
remaining rowspan on the new page.

Unfortunately iText crashes when trying to accomplish this, because it
seems that the information about cells having a remaining rowspan
gets lost when the table gets partially added to the document with
document.add(table).
When trying to add cells for the new page iText crashes in
PdfPTable.rowSpanAbove with a NullPointerException
when calling aboveCell.getRowspan(), because aboveCell is null.
I have attached an example demonstrating the problem.

I tried to use table.writeSelectedRows() instead of document.add(table),
but that doesn't work with rowspans at all.
I didn't find anything with respect to our problem in the iText book (iText
in Action, 2nd edition) either.

So the question is: are we simply doing something wrong here or does iText
have to be fixed or extended?

Any help would be greatly appreciated!

Thorsten


########################## Code Example
##################################################

/**
* Copyright 2012, DB Systel GmbH
*/
package org.example;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
* The following test example should write a two page document with a multi
page table
* containing one column with a cell with a rowspan and a page break within
that rowspan
* (i.e. only part of the rowspan should be on the first page, the rest
should be on the second page).
*
* Expected output should look like that:
*
* Page 1 (first column contains the first 5 lines of the 10 lines of
rowspan)
* -------------------------------------------
* Rowspan 10 | Cell 1
* | Cell 2
* | Cell 3
* | Cell 4
* | Cell 5
* -------------------------------------------
*
* Page 2 (first column contains remaining 5 lines of rowspan)
* -------------------------------------------
* | Cell 6
* | Cell 7
* | Cell 8
* | Cell 9
* | Cell 10
* -------------------------------------------
*
*/
public class PDFExample {

/**
* @param args
* @throws DocumentException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException,
DocumentException {

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream
("TestPageBreak.pdf"));

document.open();
try {
writeDocument(document);
} finally {
document.close();
}
}

private static void writeDocument(Document document) throws
DocumentException {

// Start multipage table

PdfPTable table = new PdfPTable(2);
table.setComplete(false);

// First page
--------------------------------------------------------------------------------------------

// add five rows
// the first cell has rowspan 10
// the second column gets just 5 cells

PdfPCell cellWithRowspan = new PdfPCell();
cellWithRowspan.setRowspan(10);
cellWithRowspan.addElement(new Phrase("Rowspan 10"));
table.addCell(cellWithRowspan);

for (int i = 0; i < 5; ++i) {
PdfPCell cell = new PdfPCell();
cell.addElement(new Phrase("Cell " + i));
table.addCell(cell);
table.completeRow();
}

// force page break (this would result from business rules)

document.add(table);
document.newPage();

// Second page
--------------------------------------------------------------------------------------------

// would like to have the remaining rowspan (5 rows) of first
column to continue on this page
// BUT: adding the table to the document lost the information about
cells with rowspan

// as a consequence adding the remaining 5 rows of cells for column
2 does not work as expected
// => crashes in PdfPTable.rowSpanAbove with NullPointerException
for (int i = 6; i < 10; ++i) {
PdfPCell cell = new PdfPCell();
cell.addElement(new Phrase("Cell " + i));
table.addCell(cell);
}

// finish second page

table.setComplete(true);
document.add(table);
}

}

#########################################################################################################


Thorsten Seitz
Development Center Architekturkonzeption und -entwicklung (T.SVD 32)

DB Systel GmbH
Silvertower J29E3.1, Jürgen-Ponto-Platz 1, 60329 Frankfurt a. Main
Tel. 069 265-48245, intern 955-
_________________________________________________________________________________

Der DB-Konzern im Internet >> http://www.deutschebahn.com

Sitz der Gesellschaft: Frankfurt am Main
Registergericht: Frankfurt am Main, HRB 78707
USt-IdNr.: DE252204770
Geschäftsführer: Detlef Exner (Vorsitzender), Dr. Burkhard Klanke, Dr.
Klaus Rüffler
Vorsitzender des Aufsichtsrates: Dr. Rolf Kranüchel
iText Info
2012-08-30 14:05:45 UTC
Permalink
This is a typical pre-sales question.
You've posted the question on the free mailing-list, where you depend on
the community.
I'm forwarding the question to sales so that the iText sales people can
create an issue in the iText ticketing system.
There are paid engineers on that system; most of the people on the list,
are unpaid volunteers.
Post by T***@deutschebahn.com
Hi all,
we are currently evaluating iText for use in a commercial application. So
far it looks quite good with the exception of one critical problem.
Before buying a commercial licence we would like to know whether that
problem can be fixed (or how to get around it).
The problem is as follows: we need to create long multipage tables
containing lots of cells with rowspans.
Certain business rules govern where page breaks occur and the important
thing is that page breaks must be possible _within rowspans_,
resulting in those cells having a remaining rowspan to continue with that
remaining rowspan on the new page.
Unfortunately iText crashes when trying to accomplish this, because it
seems that the information about cells having a remaining rowspan
gets lost when the table gets partially added to the document with
document.add(table).
When trying to add cells for the new page iText crashes in
PdfPTable.rowSpanAbove with a NullPointerException
when calling aboveCell.getRowspan(), because aboveCell is null.
I have attached an example demonstrating the problem.
I tried to use table.writeSelectedRows() instead of document.add(table),
but that doesn't work with rowspans at all.
I didn't find anything with respect to our problem in the iText book (iText
in Action, 2nd edition) either.
So the question is: are we simply doing something wrong here or does iText
have to be fixed or extended?
Any help would be greatly appreciated!
Thorsten
########################## Code Example
##################################################
/**
* Copyright 2012, DB Systel GmbH
*/
package org.example;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
* The following test example should write a two page document with a multi
page table
* containing one column with a cell with a rowspan and a page break within
that rowspan
* (i.e. only part of the rowspan should be on the first page, the rest
should be on the second page).
*
*
* Page 1 (first column contains the first 5 lines of the 10 lines of
rowspan)
* -------------------------------------------
* Rowspan 10 | Cell 1
* | Cell 2
* | Cell 3
* | Cell 4
* | Cell 5
* -------------------------------------------
*
* Page 2 (first column contains remaining 5 lines of rowspan)
* -------------------------------------------
* | Cell 6
* | Cell 7
* | Cell 8
* | Cell 9
* | Cell 10
* -------------------------------------------
*
*/
public class PDFExample {
/**
*/
public static void main(String[] args) throws FileNotFoundException,
DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream
("TestPageBreak.pdf"));
document.open();
try {
writeDocument(document);
} finally {
document.close();
}
}
private static void writeDocument(Document document) throws
DocumentException {
// Start multipage table
PdfPTable table = new PdfPTable(2);
table.setComplete(false);
// First page
--------------------------------------------------------------------------------------------
// add five rows
// the first cell has rowspan 10
// the second column gets just 5 cells
PdfPCell cellWithRowspan = new PdfPCell();
cellWithRowspan.setRowspan(10);
cellWithRowspan.addElement(new Phrase("Rowspan 10"));
table.addCell(cellWithRowspan);
for (int i = 0; i < 5; ++i) {
PdfPCell cell = new PdfPCell();
cell.addElement(new Phrase("Cell " + i));
table.addCell(cell);
table.completeRow();
}
// force page break (this would result from business rules)
document.add(table);
document.newPage();
// Second page
--------------------------------------------------------------------------------------------
// would like to have the remaining rowspan (5 rows) of first
column to continue on this page
// BUT: adding the table to the document lost the information about
cells with rowspan
// as a consequence adding the remaining 5 rows of cells for column
2 does not work as expected
// => crashes in PdfPTable.rowSpanAbove with NullPointerException
for (int i = 6; i < 10; ++i) {
PdfPCell cell = new PdfPCell();
cell.addElement(new Phrase("Cell " + i));
table.addCell(cell);
}
// finish second page
table.setComplete(true);
document.add(table);
}
}
#########################################################################################################
Thorsten Seitz
Development Center Architekturkonzeption und -entwicklung (T.SVD 32)
DB Systel GmbH
Silvertower J29E3.1, Jürgen-Ponto-Platz 1, 60329 Frankfurt a. Main
Tel. 069 265-48245, intern 955-
_________________________________________________________________________________
Der DB-Konzern im Internet >> http://www.deutschebahn.com
Sitz der Gesellschaft: Frankfurt am Main
Registergericht: Frankfurt am Main, HRB 78707
USt-IdNr.: DE252204770
Geschäftsführer: Detlef Exner (Vorsitzender), Dr. Burkhard Klanke, Dr.
Klaus Rüffler
Vorsitzender des Aufsichtsrates: Dr. Rolf Kranüchel
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Loading...