Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

iText - how do I create a multipage PDF?

807588Feb 15 2008 — edited May 21 2009
Can 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();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 18 2009
Added on Feb 15 2008
6 comments
863 views