Last active
June 10, 2016 18:49
-
-
Save JoelGeraci-Datalogics/87cea18052bf5f5e7ab8c7d3662a0027 to your computer and use it in GitHub Desktop.
This sample replaces one image in a PDF file with a new one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright Datalogics, Inc. 2015 | |
*/ | |
package pdfjt.cookbook.imaging; | |
import com.adobe.internal.io.ByteReader; | |
import com.adobe.internal.io.InputStreamByteReader; | |
import com.adobe.pdfjt.pdf.document.PDFDocument; | |
import com.adobe.pdfjt.pdf.document.PDFOpenOptions; | |
import com.adobe.pdfjt.pdf.document.PDFResources; | |
import com.adobe.pdfjt.pdf.graphics.xobject.PDFXObject; | |
import com.adobe.pdfjt.pdf.graphics.xobject.PDFXObjectImage; | |
import com.adobe.pdfjt.pdf.graphics.xobject.PDFXObjectMap; | |
import com.adobe.pdfjt.pdf.page.PDFPage; | |
import com.adobe.pdfjt.services.imageconversion.ImageManager; | |
import com.datalogics.pdf.document.DocumentHelper; | |
import java.awt.image.BufferedImage; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.Iterator; | |
import javax.imageio.ImageIO; | |
import javax.imageio.ImageReader; | |
/** | |
* This sample replaces one image in a PDF file with a new one. | |
*/ | |
public class ReplaceImage { | |
private static final String inputPDFURL = "http://dev.datalogics.com/cookbook/imaging/ReplaceImage_Input.pdf"; | |
private static final String inputImageURL = "http://dev.datalogics.com/cookbook/imaging/bird-2.jpg"; | |
private static final String outputDir = "cookbook/Imaging/output/"; | |
static public void main(String[] args) throws Exception { | |
// First read in the PDF file and get the first page so we can add a | |
// Stamp to it. | |
URLConnection connection = new URL(inputPDFURL).openConnection(); | |
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); | |
connection.connect(); | |
InputStream fis = connection.getInputStream(); | |
ByteReader byteReader = new InputStreamByteReader(fis); | |
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance()); | |
PDFPage pdfPage = pdfDocument.requirePages().getPage(0); | |
// Read in the image file as a BufferedImage. | |
ImageReader reader = null; | |
Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByFormatName("JPG"); | |
if (imageReaders.hasNext()) { | |
reader = imageReaders.next(); | |
} else { | |
System.out.println("A JPG Reader isn't available"); | |
System.exit(0); | |
} | |
URLConnection connection2 = new URL(inputImageURL).openConnection(); | |
connection2.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); | |
connection2.connect(); | |
reader.setInput(ImageIO.createImageInputStream(connection2.getInputStream())); | |
BufferedImage bufferedImage = reader.read(0); | |
/* | |
* Use the ImageManager class to convert the BufferedImage into a | |
* PDF XObject we can add to the page. | |
*/ | |
PDFXObjectImage newImage = ImageManager.getPDFImage(bufferedImage, pdfDocument); | |
/* | |
* The sample input file has only one image so we don't really need | |
* to go looking for it. We know it's the first one. | |
*/ | |
PDFResources pdfResources = pdfPage.getResources(); | |
PDFXObjectMap pdfXObjectMap = pdfResources.getXObjectMap(); | |
PDFXObject pdfXObject = pdfXObjectMap.get(pdfXObjectMap.keySet().iterator().next()); | |
/* | |
* Now we can use the InputStream from the new image's XObject to | |
* replace the original image. | |
*/ | |
pdfXObject.setStreamData(newImage.getImageStreamData()); | |
// Save and close | |
DocumentHelper.saveFullAndClose(pdfDocument, outputDir+"Bird.pdf"); | |
// Save the file. | |
System.out.println("Done!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment