tags:

views:

470

answers:

5

I am working on a billing program - right now when you click the appropriate button it generates a frame that shows the various charges etc, basically an invoice. Is there a way to give the user an option of saving that frame as a document, either Microsoft Word, Microsoft Works or PDF?

+2  A: 
  1. Paint JFrame in a BufferedImage. paint() method of JFrame
  2. Save the image as jpg or png or whatever image format
  3. Take some pdf library and create a blank pdf (e.g. iText)
  4. Insert the image into the PDF document
  5. Save it - done
jitter
+3  A: 

One approach would be to save the frame as an image, you can do that by using the following syntax to convert it to an image.

BufferedImage myImage = new BufferedImage(size.width,size.height,
                            BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = myImage.createGraphics();

myComponent.paint(g2);

you can then save this image and pass it into a jasper report. From the JasperPrint object you can then save in a few different formats, including pdf. A better but similar approach would be to pass the Graphics context into JasperReports(there is a renderer to do this in jasper, and the quality is much better).

broschb
+2  A: 

Instead of generating a word document, I'd rather use a Java library like iText to produce a PDF document (more portable) or, even better, the JasperReport report library that can output reports in a wide range of formats (PDF, XML, HTML, CSV, XLS, RTF, TXT) as suggested by bigbrother82 in a comment. This looks cleaner to me than using an image, especially for printing (not even mentioning that your invoice may be a multi-page document).

Pascal Thivent
I would also consider just using JasperReports (which uses itext to create pdfs) since you can write the report once and export it n rtf, word, pdf, or even html. So the user can choose. and it takes very little extra effort on your part.
jschoen
@bigbrother82 you're absolutely right
Pascal Thivent
A: 

I'd likely look at this from a slightly different direction and instead of asking how to splat the GUI form as-is into a PDF or word document I'd ask how to get that content into a Word/PDF document.

The answer to that question is Apache FOP. Generate a XSL-FO file and ask FOP to convert it into a RTF document (with a .DOC extension) or a PDF.

Normally one does this by generating an XML file containing the data you need printed. Then use an XSLT to convert that XML to XSL-FO. I however found it easier to generate a XSL-FO file directly using a templating language (such as Freemarker).

mlk
A: 

You might want to look at the online demo for Docmosis as an example which gives the user the options for requesting the document up front. That demo does a download, but it could direct the document into a frame instead and leave it to the browser to display. This style of working (as metioned by others) is looking at the problem from a different angle and deciding up front what format, rather than after the fact and then trying to save the frame contents.

jowierun