Create a PDF file from a PNG image or Java panel

I am looking for a Java library that can take an image (PNG) and create a PDF file. Or create a PDF file directly from the java panel that was drawn.

+2
source share
3 answers

You can achieve this using Gnostice PDFOne for Java ( http://www.gnostice.com/PDFOne_Java.asp ).

Locate the code snippet below that creates the PDF from the PNG image.

PdfDocument doc = new PdfDocument(); // Read the image as BufferedImage object BufferedImage bufImg = ImageIO.read(new File( "SampleImage.PNG")); // Create PdfImage object using the above BufferedImage object PdfImage img = PdfImage.create(bufImg); // Create a PdfPage of image size (image width x image Height) PdfPage page1 = new PdfPage(img.width(), img.height()); // draw the image at 0, 0 page1.drawImage(img, 0, 0); // add the page to the document object doc.add(page1); // save the document to the output file doc.save("PNGImageToPDF.pdf"); doc.close(); 

To create a BufferedImage from JPanel, you can use the code snippet below.

 int w = jpanel.getWidth(); int h = jpanel.getHeight(); BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); jpanel.paint(g2); g2.dispose(); 

After creating the BuffereImage from JPanel, you can use the first code snippet to create the PDF.

Hope you find this helpful.

Disclaimer: I work for Gnostis.

+1
source

Take a look at iText and PDFBox.

http://itextpdf.com/

http://pdfbox.apache.org/

+2
source

Try xsPDF :

 BufferedImage image = ImageIO.read(new File(imageFileName)); int width = image.getWidth(), height = image.getHeight(); XSPDF.getInstance() .setPageSize(width, height) .setPageMargin(NO_MARGIN) .setImage(image, 0, 0, width, height, 0) .createPdf(pdfFileName); 
+1
source

All Articles