Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
There is a question on StackOverFlow.com asking if there was a way to replace an image in a PDF file. PDFOne can parse through all content elements in a page and identify content like text, images, shapes, form fields, and annotations. PDFOne can delete form fields and annotations and redact text, but deleting images and shapes (path elements) is not supported yet. A quick-and-dirty solution would be draw another image after identify the bounding box of the existing image.
In this document, an old logo image was replaced with a new logo.
First, the index of the the images that need to be replaced was identified. For that, this code snippet extracts all images from the PDF document.
import java.io.File; import java.util.ArrayList; import javax.imageio.ImageIO; import com.gnostice.pdfone.PDFOne; import com.gnostice.pdfone.PdfDocument; import com.gnostice.pdfone.PdfPageElement; import com.gnostice.pdfone.PdfPageImageElement; public class replaceImage { public static void main(String[] args) { ArrayListoImageList; try { PdfDocument doc = new PdfDocument(); doc.load("sample_doc.pdf"); oImageList = (ArrayList ) doc.getPageElements( "1-", PdfPageElement.ELEMENT_TYPE_IMAGE); if (oImageList.size() > 0) { for (int i = 0; i < oImageList.size(); i++) { ImageIO.write( oImageList.get(i).getImage(), "png", new File("image" + i + ".png")); } doc.save("new-doc.pdf"); } else { System.out.println("No images"); } doc.close(); } catch (Exception e) { System.out.println("Error in replaceImage.java"); System.out.println(e.getMessage()); } } }
The output folder tells you the index of the images that need to be replaced.
The above code snippet was then modified to add draw new logo image wherever the old logo appeared.
import java.util.ArrayList; import com.gnostice.pdfone.PDFOne; import com.gnostice.pdfone.PdfDocument; import com.gnostice.pdfone.PdfImage; import com.gnostice.pdfone.PdfPage; import com.gnostice.pdfone.PdfPageElement; import com.gnostice.pdfone.PdfPageImageElement; public class replaceImage { public static void main(String[] args) { ArrayListoImageList; try { PdfDocument doc = new PdfDocument(); doc.load("sample_doc.pdf"); oImageList = (ArrayList ) doc.getPageElements( "1-", PdfPageElement.ELEMENT_TYPE_IMAGE); if (oImageList.size() > 0) { for (int i = 0; i < oImageList.size(); i++) { if ((i == 0) || (i == 3) || (i == 6)) { PdfPageImageElement oPageImage = oImageList.get(i); PdfImage pi = PdfImage .create("new-logo.png"); PdfPage pg = doc.getPage(oPageImage.getPageNum()); pg.drawImage(pi, oPageImage.getX(), oPageImage.getY()); } // ImageIO.write( // oImageList.get(i).getImage(), // "png", // new File("image" + i + ".png")); } doc.save("new-doc.pdf"); } else { System.out.println("No images"); } doc.close(); } catch (Exception e) { System.out.println("Error in replaceImage.java"); System.out.println(e.getMessage()); } } }
---o0O0o---
Our .NET Developer Tools | |
---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
Our Delphi/C++Builder developer tools | |
---|---|
Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
Our Java developer tools | |
---|---|
Gnostice Document Studio JavaMulti-format document-processing component suite for Java developers. |
PDFOne (for Java)A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications. |
Our Platform-Agnostic Cloud and On-Premises APIs | |
---|---|
StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved. |