PDF处理控件Aspose.PDF功能演示:使用Java添加,提取,删除或替换PDF文件中的图像

  • 使用Java在PDF中添加图像
  • 使用Java从PDF提取图像
  • 使用Java从PDF删除图像
  • 使用Java替换PDF中的图像

PDF处理控件Aspose.PDF功能演示:使用Java添加,提取,删除或替换PDF文件中的图像整合所有格式的Aspose.Total永久授权正在火热促销中,联系客服立马1分钟了解全部咨询!

使用Java在PDF文件中添加图像

以下是使用Java在PDF文件中添加图像的步骤。

  • 首先,创建Document 类的实例 以加载PDF文档。
  • 使用Document.getPages()。get_Item(int)方法获取要添加图像的页面。
  • 将图像文件加载到FileInputStream对象中。
    • 将新页面添加到PDF文档并设置其属性。
    • 从列表中将每个图像文件加载到文件流中。
    • 将图像添加到页面的段落集合中。
  • 使用Page.getResources().getImages().add(FileInputStream)方法将图像添加到页面的资源中。
  • 使用运算符将图像放置在页面上:
    • GSave运算符可保存当前的图形状态。
    • ConcatenateMatrix运算符,用于指定要放置图像的位置。
    • Do运算符在页面上绘制图像。
    • GRestore操作员保存更新的图形状态。
  • 最后,使用Document.save(string)方法保存更新的PDF文件。

以下代码示例显示了如何使用Java将图像添加到PDF文件。

// Open a documentDocument pdfDocument1 = new Document("input.pdf");// Set coordinatesint lowerLeftX = 100;int lowerLeftY = 100;int upperRightX = 200;int upperRightY = 200;// Get the page you want to add the image toPage page = pdfDocument1.getPages().get_Item(1);// Load image into streamjava.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File("input_image1.jpg"));// Add an image to the Images collection of the page resourcespage.getResources().getImages().add(imageStream);// Using the GSave operator: this operator saves current graphics statepage.getContents().add(new Operator.GSave());// Create Rectangle and Matrix objectsRectangle rectangle = new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);Matrix matrix = new Matrix(new double[] { rectangle.getURX() - rectangle.getLLX(), 0, 0, rectangle.getURY() - rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY() });// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placedpage.getContents().add(new Operator.ConcatenateMatrix(matrix));XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());// Using Do operator: this operator draws imagepage.getContents().add(new Operator.Do(ximage.getName()));// Using GRestore operator: this operator restores graphics statepage.getContents().add(new Operator.GRestore());// Save the new PDFpdfDocument1.save("Updated_document.pdf");// Close image streamimageStream.close();

使用Java从PDF文件中提取图像

以下是使用Java从PDF文档提取图像的步骤。

  • 创建Document 类的实例 以加载PDF文档。
  • 使用Document.getPages()。get_Item(int).getResources()。getImages()。get_Item(int)方法将所需图像提取到XImage对象中。
  • 还可以遍历图像集合以提取并保存所有图像。
  • 最后,使用OutputStream将提取的图像保存为文件。

以下代码示例显示了如何使用Java从PDF文件提取图像。

// Open a documentDocument pdfDocument = new Document("input.pdf");// Extract a particular imageXImage xImage = pdfDocument.getPages().get_Item(1).getResources().getImages().get_Item(1);// Create stream object to save the output imagejava.io.OutputStream output = new java.io.FileOutputStream("output.jpg");// Save the output imagexImage.save(output);// Close streamoutput.close();

使用Java从PDF文件中删除图像

以下是使用Java从PDF文件中删除图像的步骤。

  • 将PDF文件加载到 Document 对象中。
  • 使用以下方法之一删除所需的图像。
    • delete()从集合中删除图像。
    • delete(int index)按索引从集合中删除图像。
    • delete(String name)按名称从集合中删除图像。
  • 最后,使用Document.save(string)方法保存更新的PDF文件。

以下代码示例显示了如何使用Java删除PDF中的图像。

// Open a documentDocument pdfDocument = new Document("input.pdf");// Delete a particular imagepdfDocument.getPages().get_Item(1).getResources().getImages().delete(1);// Save the updated PDF filepdfDocument.save("output.pdf");

使用Java替换PDF文件中的图像

以下是使用Java替换PDF文件中的图像的步骤。

  • 将PDF文件加载到 Document 对象中。
  • 将新图像加载到FileInputStream对象中。
  • 使用Document.getPages()。get_Item(int).getResources()。getImages()。replace(int,FileInputStream)方法通过指定索引来替换图像。
  • 最后,使用Document.save(string)方法保存更新的PDF文件。

以下代码示例显示了如何使用Java替换PDF中的图像。

// Open a documentDocument pdfDocument = new Document("input.pdf");// Replace a particular imagepdfDocument.getPages().get_Item(1).getResources().getImages().replace(1, new java.io.FileInputStream(new java.io.File("apose.png")));// Save the updated PDF filepdfDocument.save("output.pdf");

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月13日
下一篇 2021年1月13日

相关推荐

发表回复

登录后才能评论