如何使用Java添加,提取,删除或替换PDF文件中的图像?试试Aspose
图像被广泛用于PDF文件中的不同类型的描绘和演示。在本文中,将学习如何以编程方式处理PDF文件中的图像。特别是,本文将介绍如何使用Java添加,提取,删除或替换PDF文件中的图像。
- 使用Java在PDF中添加图像
- 使用Java从PDF提取图像
- 使用Java从PDF删除图像
- 使用Java替换PDF中的图像
Aspose.PDF for Java是功能强大的API,可为您提供多种PDF操作功能。该API可让您无缝处理PDF文件中的文本,注释或图像。感兴趣的朋友可点击下载最新版。
使用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 document
Document pdfDocument1 = new Document("input.pdf");
// Set coordinates
int lowerLeftX = 100;
int lowerLeftY = 100;
int upperRightX = 200;
int upperRightY = 200;
// Get the page you want to add the image to
Page page = pdfDocument1.getPages().get_Item(1);
// Load image into stream
java.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File("input_image1.jpg"));
// Add an image to the Images collection of the page resources
page.getResources().getImages().add(imageStream);
// Using the GSave operator: this operator saves current graphics state
page.getContents().add(new Operator.GSave());
// Create Rectangle and Matrix objects
Rectangle 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 placed
page.getContents().add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());
// Using Do operator: this operator draws image
page.getContents().add(new Operator.Do(ximage.getName()));
// Using GRestore operator: this operator restores graphics state
page.getContents().add(new Operator.GRestore());
// Save the new PDF
pdfDocument1.save("Updated_document.pdf");
// Close image stream
imageStream.close();
使用Java从PDF文件中提取图像
以下是使用Java从PDF文档提取图像的步骤。
- 创建Document 类的实例 以加载PDF文档。
- 使用Document.getPages()。get_Item(int).getResources()。getImages()。get_Item(int)方法将所需图像提取到XImage对象中。
- 还可以遍历图像集合以提取并保存所有图像。
- 最后,使用OutputStream将提取的图像保存为文件。
以下代码示例显示了如何使用Java从PDF文件提取图像。
// Open a document
Document pdfDocument = new Document("input.pdf");
// Extract a particular image
XImage xImage = pdfDocument.getPages().get_Item(1).getResources().getImages().get_Item(1);
// Create stream object to save the output image
java.io.OutputStream output = new java.io.FileOutputStream("output.jpg");
// Save the output image
xImage.save(output);
// Close stream
output.close();
使用Java从PDF文件中删除图像
以下是使用Java从PDF文件中删除图像的步骤。
- 将PDF文件加载到 Document 对象中。
- 使用以下方法之一删除所需的图像。
- delete()从集合中删除图像。
- delete(int index)按索引从集合中删除图像。
- delete(String name)按名称从集合中删除图像。
- 最后,使用Document.save(string)方法保存更新的PDF文件。
以下代码示例显示了如何使用Java删除PDF中的图像。
// Open a document
Document pdfDocument = new Document("input.pdf");
// Delete a particular image
pdfDocument.getPages().get_Item(1).getResources().getImages().delete(1);
// Save the updated PDF file
pdfDocument.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 document
Document pdfDocument = new Document("input.pdf");
// Replace a particular image
pdfDocument.getPages().get_Item(1).getResources().getImages().replace(1, new java.io.FileInputStream(new java.io.File("apose.png")));
// Save the updated PDF file
pdfDocument.save("output.pdf");
如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。
转载自:https://juejin.cn/post/6940829876769259528