Thanks Paulo,
I think, I got it now. Below comes a modified version of your sample class
that prints column headers and row headers and splits the overall table in
subtables on several pages.
I got two questions left:
- How can I get the hight of a cell?
- Is there a more simple way to cope with the "column-"headerrow with
writeSelectedRows()? I tried table.setHeaderRows(1); but this didn't write
the header automatically.
Best regards
Peter
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
/**
* Writing a PdfPTable at an absolute position.
*/
public class SplitBigTable {
/**
* Demonstrates the writeSelectedRows method.
* @param args no arguments needed
*/
public static void main(String[] args) {
System.out.println("SplitBigTable");
Document document = new Document(PageSize.A4, 2, 2, 2, 2);
try {
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("SplitBigTable.pdf"));
document.open();
PdfPTable table = new PdfPTable(10);
float rowHeaderSize = 70f;
float colSize = 90f;
float headerCellHight = 16f; // how can I get the hight of a
cell?
float[] rows = { rowHeaderSize, colSize, colSize, colSize,
colSize, colSize, colSize, colSize, colSize,
colSize };
table.setTotalWidth(rows);
//Create the table data including all the header rows (column
headers and row headers)
for (int k = 0; k < 100; ++k) {
if (k == 0) {
table.addCell("");
} else {
PdfPCell pCell = getHighlightedHeaderCell("rowhead " +
k);
table.addCell(pCell);
}
for (int i = 1; i < 10; i++) {
if (k == 0) {
PdfPCell pCell = getHighlightedHeaderCell("colhead "
+ i);
table.addCell(pCell);
} else {
table.addCell("cell (r/c) " + k + "/" + i);
}
}
}
document.add(new Paragraph("row 0 - 40; column 0 - 4"));
// write row header
table.writeSelectedRows(0, 1, 0, 1, 50, 820,
writer.getDirectContent());
table.writeSelectedRows(0, 1, 1, 41, 50, 820 - headerCellHight,
writer.getDirectContent());
table.writeSelectedRows(1, 4, 0, 1, 50 + rowHeaderSize, 820,
writer.getDirectContent());
// write data cells
table.writeSelectedRows(1, 4, 1, 41, 50 + rowHeaderSize, 820 -
headerCellHight, writer.getDirectContent());
document.newPage();
document.add(new Paragraph("row 0 - 40; column 4 - 9"));
// write row header
table.writeSelectedRows(0, 1, 0, 1, 50, 820,
writer.getDirectContent());
table.writeSelectedRows(0, 1, 1, 41, 50, 820 - headerCellHight,
writer.getDirectContent());
table.writeSelectedRows(4, 9, 0, 1, 50 + rowHeaderSize, 820,
writer.getDirectContent());
// write data cells
table.writeSelectedRows(4, 9, 1, 41, 50 + rowHeaderSize, 820 -
headerCellHight, writer.getDirectContent());
document.newPage();
document.add(new Paragraph("row 0 - 40; column 9 - 10"));
// write row header
table.writeSelectedRows(0, 1, 0, 1, 50, 820,
writer.getDirectContent());
table.writeSelectedRows(0, 1, 1, 41, 50, 820 - headerCellHight,
writer.getDirectContent());
table.writeSelectedRows(9, 10, 0, 1, 50 + rowHeaderSize, 820,
writer.getDirectContent());
// write data cells
table.writeSelectedRows(9, 10, 1, 41, 50 + rowHeaderSize, 820 -
headerCellHight, writer.getDirectContent());
document.newPage();
document.add(new Paragraph("row 40 - 80; column 0 - 4"));
// write row header
table.writeSelectedRows(0, 1, 0, 1, 50, 820,
writer.getDirectContent());
table.writeSelectedRows(0, 1, 41, 80, 50, 820 - headerCellHight,
writer.getDirectContent());
table.writeSelectedRows(1, 4, 0, 1, 50 + rowHeaderSize, 820,
writer.getDirectContent());
// write data cells
table.writeSelectedRows(1, 4, 41, 80, 50 + rowHeaderSize, 820 -
headerCellHight, writer.getDirectContent());
document.newPage();
document.add(new Paragraph("row 40 - 80; column 4 - 9"));
// write row header
table.writeSelectedRows(0, 1, 0, 1, 50, 820,
writer.getDirectContent());
table.writeSelectedRows(0, 1, 41, 80, 50, 820 - headerCellHight,
writer.getDirectContent());
table.writeSelectedRows(4, 9, 0, 1, 50 + rowHeaderSize, 820,
writer.getDirectContent());
// write data cells
table.writeSelectedRows(4, 9, 41, 80, 50 + rowHeaderSize, 820 -
headerCellHight, writer.getDirectContent());
document.newPage();
document.add(new Paragraph("row 40 - 80; column 9 - 10"));
// write row header
table.writeSelectedRows(0, 1, 0, 1, 50, 820,
writer.getDirectContent());
table.writeSelectedRows(0, 1, 41, 80, 50, 820 - headerCellHight,
writer.getDirectContent());
table.writeSelectedRows(9, 10, 0, 1, 50 + rowHeaderSize, 820,
writer.getDirectContent());
// write data cells
table.writeSelectedRows(9, 10, 41, 80, 50 + rowHeaderSize, 820 -
headerCellHight, writer.getDirectContent());
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
/**
* Format a header cell (row header or column header).
* @param headertext the header text
* @return formatted cell
*/
private static PdfPCell getHighlightedHeaderCell(String headertext) {
PdfPCell pCell = new PdfPCell();
Chunk content = new Chunk(headertext);
content.setTextRenderMode
(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f,
Color.black);
com.lowagie.text.Phrase phrase = new Phrase();
phrase.setLeading(10);
phrase.add(content);
pCell.setPhrase(phrase);
pCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
pCell.setBackgroundColor(Color.lightGray);
return pCell;
}
}
Paulo Soares wrote:
>
> PdfPTable.writeSelectedRows() will write the rows and columns you
> require.
>
> Paulo
>
>> -----Original Message-----
>> From: [EMAIL PROTECTED]
>> [mailto:[EMAIL PROTECTED] On
>> Behalf Of Peter Hild
>> Sent: Monday, November 19, 2007 1:21 PM
>> To: [email protected]
>> Subject: [iText-questions] How to split a large table (too
>> many columns and too many rows) onto many pages
>>
>>
>> I have a very large table which I want to split onto many
>> pages. The base
>> table looks something like this:
>>
>> TableData:
>> R1 | C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | Cn
>> R2 | C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | Cn
>> R3 | C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | Cn
>> R4 | C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | Cn
>> R5 | C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | Cn
>> ...
>> R8 | C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | Cn
>>
>> The result table(s) on each page must have a row header on
>> each page and a
>> "column header" (this means that some columns, e.g. the
>> columns C1 and C2
>> should be placed in each page).
>>
>> Result tables:
>> Page 1:
>> R1 | C1 | C2 | C3 | C4 | C5 | C6
>> R2 | C1 | C2 | C3 | C4 | C5 | C6
>> R3 | C1 | C2 | C3 | C4 | C5 | C6
>> R4 | C1 | C2 | C3 | C4 | C5 | C6
>>
>> Page 2:
>> R1 | C1 | C2 | C7 | C8 | C9 | C10
>> R2 | C1 | C2 | C7 | C8 | C9 | C10
>> R3 | C1 | C2 | C7 | C8 | C9 | C10
>> R4 | C1 | C2 | C7 | C8 | C9 | C10
>>
>> Page 3:
>> R1 | C1 | C2 | Cn
>> R2 | C1 | C2 | Cn
>> R3 | C1 | C2 | Cn
>> R4 | C1 | C2 | Cn
>>
>> Page 4:
>> R1 | C1 | C2 | C3 | C4 | C5 | C6
>> R5 | C1 | C2 | C3 | C4 | C5 | C6
>> R6 | C1 | C2 | C3 | C4 | C5 | C6
>> R7 | C1 | C2 | C3 | C4 | C5 | C6
>> R8 | C1 | C2 | C3 | C4 | C5 | C6
>>
>> Page 5:
>> R1 | C1 | C2 | C7 | C8 | C9 | C10
>> R5 | C1 | C2 | C7 | C8 | C9 | C10
>> R6 | C1 | C2 | C7 | C8 | C9 | C10
>> R7 | C1 | C2 | C7 | C8 | C9 | C10
>> R8 | C1 | C2 | C7 | C8 | C9 | C10
>>
>> Page 6:
>> R1 | C1 | C2 | Cn
>> R5 | C1 | C2 | Cn
>> R6 | C1 | C2 | Cn
>> R7 | C1 | C2 | Cn
>> R8 | C1 | C2 | Cn
>>
>> How can I do this with iText? How can I specify the columns
>> that have to be
>> repeated on each row?
>>
>> Best Regard
>> Peter
>
>
> Aviso Legal:
>
> Esta mensagem é destinada exclusivamente ao destinatário. Pode conter
> informação confidencial ou legalmente protegida. A incorrecta transmissão
> desta mensagem não significa a perca de confidencialidade. Se esta
> mensagem for recebida por engano, por favor envie-a de volta para o
> remetente e apague-a do seu sistema de imediato. É proibido a qualquer
> pessoa que não o destinatário de usar, revelar ou distribuir qualquer
> parte desta mensagem.
>
>
>
> Disclaimer:
>
> This message is destined exclusively to the intended receiver. It may
> contain confidential or legally protected information. The incorrect
> transmission of this message does not mean the loss of its
> confidentiality. If this message is received by mistake, please send it
> back to the sender and delete it from your system immediately. It is
> forbidden to any person who is not the intended receiver to use,
> distribute or copy any part of this message.
>
>
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2005.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> iText-questions mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
> Buy the iText book: http://itext.ugent.be/itext-in-action/
>
>
--
View this message in context:
http://www.nabble.com/How-to-split-a-large-table-%28too-many-columns-and-too-many-rows%29-onto-many-pages-tf4836308.html#a13841242
Sent from the iText - General mailing list archive at Nabble.com.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/