- 将图像转换为PDF

用Java将图像转换为PDF
以下是将PNG,JPEG,BMP或TIFF图像转换为PDF的步骤。
为了演示,我们将文本注释添加到PDF文件中。以下是与API引用一起使用Java向PDF添加文本注释的步骤。
- 创建一个Document类的对象。
- 使用Document.getPages()。add()方法向文档添加新页面,并在Page对象中获取其引用。
- 创建Image类的对象。
- 通过提供图像文件的路径,使用Image.setFile(String)方法设置图像。
- 使用Page.getParagraphs()。add(Image)方法将图像添加到PDF的段落中。
- 使用Document.save(String)方法保存PDF文件。
以下代码示例显示了如何在Java中将PNG,JPEG,BMP或TIFF图像转换为PDF。
// Instantiate Document ObjectDocument doc = new Document();// Add a page to pages collection of documentPage page = doc.getPages().add();// Load the source image file to Stream objectjava.io.FileInputStream fs = new java.io.FileInputStream("source.png");// Set margins so image will fit, etc.page.getPageInfo().getMargin().setBottom(0);page.getPageInfo().getMargin().setTop(0);page.getPageInfo().getMargin().setLeft(0);page.getPageInfo().getMargin().setRight(0);page.setCropBox(new Rectangle(0, 0, 400, 400));// Create an image objectImage image1 = new Image();// Add the image into paragraphs collection of the sectionpage.getParagraphs().add(image1);// Set the image file streamimage1.setImageStream(fs);// Save resultant PDF filedoc.save("Image2PDF.pdf");
输出结果

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!