Hi all. Here is a test case that does not seem to render fine here.
I have attached the java code and the generated pdf. The problem is on the beginning of the second page, due to the table being spanned over two pages. Can anyone test it and tell us if the result is the same? Any advice on what we are doing wrong? Thanks. -- Daniele Dellafiore http://blog.ildella.net/
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.Set;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class ITextSpike {
class StartPage extends PdfPageEventHelper {
@Override
public void onStartPage(PdfWriter writer, Document document) {
try {
Table header = new Table(2);
header.addCell("Header");
header.setPadding(10);
header.setBorder(0);
header.setAlignment(Paragraph.ALIGN_LEFT);
document.add(header);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
class EndPage extends PdfPageEventHelper {
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
Table header = new Table(2);
header.addCell("footer");
document.add(header);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
// private Font elvetica;
public static void main(String[] args) throws Exception {
new ITextSpike().start();
}
void start() throws FileNotFoundException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
document.setMargins(10, 10, 10, 10);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("prova.pdf"));
writer.setPageEvent(new StartPage());
// writer.setPageEvent(new EndPage());
document.open();
printRegisteredFonts();
// FontFactory.register("Vera.ttf");
// printRegisteredFonts();
// elvetica = FontFactory.getFont("bitstreamverasans-roman", 24);
Chunk titolo = new Chunk("TITOLO");
// titolo.setFont(elvetica);
document.add(new Paragraph(titolo));
document.add(createFirstTable());
Paragraph header = new Paragraph("DESTINAZIONI DEL VIAGGIO");
header.setAlignment(Element.ALIGN_CENTER);
document.add(header);
document.add(createGroupTable());
document.add(createLastTable());
document.close();
}
private Element createGroupTable() throws BadElementException {
Table table = new Table(2);
Cell cell = new Cell("order 00999");
cell.setRowspan(40);
table.addCell(cell);
for (int i = 0; i < 20; i++) {
table.addCell("fratellis");
Cell cell2 = new Cell("lou reed");
cell2.setGroupChange(true);
table.addCell(cell2);
}
return table;
}
private void printRegisteredFonts() {
System.out.println("number of registered fonts: " + FontFactory.getRegisteredFonts().size());
Set registeredFonts = FontFactory.getRegisteredFonts();
for (Iterator iterator = registeredFonts.iterator(); iterator.hasNext();) {
String font = (String) iterator.next();
System.out.println(font);
}
}
private Table createFirstTable() throws BadElementException {
Table table = new Table(6);
table.setAlignment(Paragraph.ALIGN_LEFT);
table.setWidth(100);
table.getDefaultCell().setBorder(0);
table.setPadding(5);
populateIntestazioneFirstLine(table);
populateIntestazioneSecondLine(table);
return table;
}
private void populateIntestazioneSecondLine(Table table) throws BadElementException {
Chunk label1Phar = new Chunk("Targa Mot./Rim.:");
// label1Phar.setFont(elvetica);
Cell label1 = new Cell(label1Phar);
table.addCell(label1);
String targa = "CW625PB / AC13794";
Cell targaCell = new Cell(new Paragraph(targa));
targaCell.setColspan(3);
table.addCell(targaCell);
table.addCell("Caricam.");
table.addCell("BASSO");
}
private void populateIntestazioneFirstLine(Table table) throws BadElementException {
Paragraph label1Phar = new Paragraph("Ricevuta di prenotazione del");
label1Phar.setFont(FontFactory.getFont("arial", 10, Font.BOLD, Color.BLACK));
Cell label1 = new Cell(label1Phar);
table.addCell(label1);
table.addCell("19/03/08 alle 09:34");
table.addCell("F. Viaggio");
table.addCell("000051");
table.addCell("Pagina: ");
table.addCell("1 / 1");
}
private PdfPTable createLastTable() throws DocumentException {
PdfPTable table = new PdfPTable(9);
table.setSpacingBefore(10);
table.setHeaderRows(1);
table.addCell("Indirizzo di consegna");
table.addCell("SP");
table.addCell("GM");
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
table.setWidths(new int[] { 100, 10, 10, 10, 10, 10, 10, 10, 10 });
for (int i = 0; i < 20; i++) {
populateTable(table);
}
return table;
}
private void populateTable(PdfPTable table) {
PdfPCell addressCell1 = new PdfPCell(new Paragraph("Stazione dei desideri"));
table.addCell(addressCell1);
addProducts(table);
PdfPCell addressCell2 = new PdfPCell(new Paragraph("Di Giulio Giovanni"));
table.addCell(addressCell2);
addProducts(table);
}
private void addProducts(PdfPTable table) {
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("4.1");
table.addCell("5.1");
table.addCell("6.1");
table.addCell("7.1");
table.addCell("8.1");
}
}
prova.pdf
Description: Adobe PDF document
------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions Do you like iText? Buy the iText book: http://www.1t3xt.com/docs/book.php Or leave a tip: https://tipit.to/itexttipjar
