/*
* $Id$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
package com.lowagie.examples.objects.tables;
import com.lowagie.text.LwgDocument;
import com.lowagie.text.LwgPageSize;
import com.lowagie.text.LwgPhrase;
import com.lowagie.text.pdf.LwgPdfPCell;
import com.lowagie.text.pdf.LwgPdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import org.geoforge.demo.GfrFileOutputStream;
/**
* Add a table to a PDF with document.add() and if the cell doesn't fit, the row is split in two parts.
*/
public class SplitRows {
/**
* Demonstrates how rows are split when the cell is too big.
*
* @param args
* no arguments needed
*/
public static void main(String[] args) {
System.out.println("Split rows");
// step1
LwgDocument document1 = new LwgDocument(LwgPageSize.A4.rotate(), 10, 10, 10, 10);
LwgDocument document2 = new LwgDocument(LwgPageSize.A4.rotate(), 10, 10, 10, 10);
LwgDocument document3 = new LwgDocument(LwgPageSize.A4.rotate(), 10, 10, 10, 10);
try {
// step2
PdfWriter.getInstance(document1,
new GfrFileOutputStream("com.lowagie.examples.objects.tables.SplitRows_Between.pdf"));
PdfWriter.getInstance(document2,
new GfrFileOutputStream("com.lowagie.examples.objects.tables.SplitRows_Within.pdf"));
PdfWriter.getInstance(document3,
new GfrFileOutputStream("com.lowagie.examples.objects.tables.SplitRows_OmitRows.pdf"));
// step3
document1.open();
document2.open();
document3.open();
// step4
String text = "Quick brown fox jumps over the lazy dog. ";
for (int i = 0; i < 5; i++) text += text;
LwgPdfPTable table = new LwgPdfPTable(2);
LwgPdfPCell largeCell;
LwgPhrase phrase;
for (int i = 0; i < 10; i++) {
phrase = new LwgPhrase(text);
for (int j = 0; j < i; j++) {
phrase.add(new LwgPhrase(text));
}
if (i == 7) phrase = new LwgPhrase(text);
table.add(String.valueOf(i));
largeCell = new LwgPdfPCell(phrase);
table.add(largeCell);
}
document1.add(table);
table.setSplitLate(false);
document2.add(table);
table.setSplitRows(false);
document3.add(table);
} catch (Exception de) {
de.printStackTrace();
}
// step5
document1.close();
document2.close();
document3.close();
}
}