iText - how do I create a multipage PDF?
807588Feb 15 2008 — edited May 21 2009Can anyone tell me how to modify the code below so that if Component c has more content than one A4 page, all content is output to a multipage PDF.
Thanks
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.*;
import com.lowagie.text.Document;
import com.lowagie.text.*;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class PrintPDF {
Component c = null;
String documentName = null;
public PrintPDF(Component c, String name){
this.c = c;
this.documentName = name + ".pdf";
Document document = new Document(PageSize.A4,36,36,36,36);
try {
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream documentName));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(500, 500);
Graphics2D g2 = tp.createGraphics(500, 500);
c.paint(g2);
g2.dispose();
cb.addTemplate(tp, 30, 300);
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}