翻譯|使用教程|編輯:李顯亮|2021-03-17 10:35:07.930|閱讀 337 次
概述:圖像被廣泛用于PDF文件中的不同類型的描繪和演示。在本文中,將學習如何以編程方式處理PDF文件中的圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
圖像被廣泛用于PDF文件中的不同類型的描繪和演示。在本文中,將學習如何以編程方式處理PDF文件中的圖像。特別是,本文將介紹如何使用Java添加,提取,刪除或替換PDF文件中的圖像。
Aspose.PDF for Java是功能強大的API,可為您提供多種PDF操作功能。該API可讓您無縫處理PDF文件中的文本,注釋或圖像。感興趣的朋友可點擊下方按鈕下載最新版。
以下是使用Java在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文件提取圖像。
// 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中的圖像。
// 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中的圖像。
// 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的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn