从PNG图像或Java面板创建PDF [英] Create PDF from a PNG image Or Java Panel
问题描述
我正在寻找一个可以拍摄图像(PNG)并创建PDF的Java库。
或直接从已绘制的java面板创建PDF。
I'm looking for a Java library that will can take a Image (PNG) and create a PDF. Or Create the PDF directly from a java panel that has been drawn.
推荐答案
您可以使用Gnostice实现此目的PDFOne for Java( http://www.gnostice.com/PDFOne_Java.asp )。
You can achieve this using Gnostice PDFOne for Java (http://www.gnostice.com/PDFOne_Java.asp).
在下面找到从PNG图像创建PDF文档的代码片段。
Find below the code snippet that creates a PDF document from a 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();
要从JPanel创建BufferedImage,您可以使用以下代码段。
To create a BufferedImage from a JPanel you can use the below code snippet.
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。
After creating BuffereImage from JPanel you can use the first code snippet to create PDF.
我希望你会觉得这很有用。
I hope you will find this useful.
免责声明:我为Gnostice工作。
Disclaimer: I work for Gnostice.
这篇关于从PNG图像或Java面板创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!