从 JButton 获取图像并导出为 PDF



如何使用 Java Netbeans 通过JButton上传图像并将该图像导出为 PDF 文件?

我正在使用iText库,但尚未获得结果,

谢谢

"通过 JButton 上传图片"是什么意思?您希望能够从文件选择器中选择图像,然后将所选图像导出为 pdf ?如果是,请阅读:

https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html

然后对于 PDF

导出,您必须创建一个包含图像的 PDF。使用iText之类的东西将图像转换为pdf需要花费,同时,如果只是将图像转换为pdf就可以为您工作,那么应该这样的事情:

Document document = new Document(PageSize.A4, 20, 20, 20, 20);
PdfWriter.getInstance(document, new FileOutputStream("C:/test.pdf"));
document.open();
Image image = Image.getInstance(getClass().getResource("/logo.png"));
document.add(image);
document.close();

希望我有帮助,祝你有美好的一天。

最新更新