您可以使用Gnostice PDFOne for
Java(http://www.gnostice.com/PDFOne_Java.asp)实现此目的。
在下面的代码片段中找到可从PNG图像创建PDF文档的代码。
Pdfdocument doc = new Pdfdocument();// Read the image as BufferedImage objectBufferedImage bufImg = ImageIO.read(new File( "SampleImage.PNG"));// Create PdfImage object using the above BufferedImage objectPdfImage 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, 0page1.drawImage(img, 0, 0);// add the page to the document objectdoc.add(page1);// save the document to the output filedoc.save("PNGImageToPDF.pdf");doc.close();要从JPanel创建BufferedImage,可以使用以下代码片段。
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();
从JPanel创建BuffereImage之后,可以使用第一个代码段创建PDF。
希望您会觉得有用。
免责声明:我为Gnostice工作。



