Hello,
I have a JPanel whose graphics I would like to write to a PDF file without ever making the JPanel display on the screen. Is it possible?
First, I want to go from JPanel to PDF because I am faced with (complex) code that already generates the JPanel and displays it on the screen. I want to use this code to also be able to write the graphics to a PDF file, rather than starting from scratch.
Here's the approach I've taken so far (where panel is my JPanel):
com.lowagie.text.Document itextDoc = new com.lowagie.text.Document();
PdfWriter writer = PdfWriter.getInstance(itextDoc, new FileOutputStream("file.pdf"));
itextDoc.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.concatCTM(0, 1, -1, 0, 841, 0);
Graphics2D g2 = cb.createGraphics(841, 841);
panel.paintAll(g2);
g2.dispose();
cb.restoreState();
itextDoc.close();
This only works in writing what I expect to the file if I put my panel in a JFrame and make the frame visible. If I don't make the frame visible, the code generates what seems to be an empty PDF file.
Any suggestions on how to go about this?
Thanks,
Kevin Leuthold
SPS Commerce
