Reusing images

Tags: imagesreuse imageextract contentiText 5
Files: 
/**
 * This code sample was written by Bruno Lowagie in answer to this question:
 * http://stackoverflow.com/questions/21720802/how-to-make-text-invisible-in-an-existing-pdf
 * 
 * In this example, we take an image that is present in the background,
 * and we add the same image (by its reference) to the foreground so that
 * it covers the OCR'd text.
 */
package sandbox.images;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PRIndirectReference;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class RepeatImage {
 
    public static final String SRC = "resources/pdfs/chinese.pdf";
    public static final String DEST = "results/images/chinese.pdf";
 
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RepeatImage().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(src);
        // We assume that there's a single large picture on the first page
        PdfDictionary page = reader.getPageN(1);
        PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
        PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
        PdfName imgName = xobjects.getKeys().iterator().next();
        Image img = Image.getInstance((PRIndirectReference)xobjects.getAsIndirectObject(imgName));
        img.setAbsolutePosition(0, 0);
        img.scaleAbsolute(reader.getPageSize(1));
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.getOverContent(1).addImage(img);
        stamper.close();
        reader.close();
    }
 
}
package sandbox.images;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.parser.PdfImageObject;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class ReuseImage {
 
    public static final String SRC = "resources/pdfs/single_image.pdf";
    public static final String DEST = "results/images/image_on_A4.pdf";
 
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ReuseImage().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(src);
        // We assume that there's a single large picture on the first page
        PdfDictionary page = reader.getPageN(1);
        PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
        PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
        PdfName imgRef = xobjects.getKeys().iterator().next();
        PRStream imgStream = (PRStream) xobjects.getAsStream(imgRef);
        // We now create a new Image object based on the bytes in the stream
        PdfImageObject imgObject = new PdfImageObject(imgStream);
        reader.close();
        Image img = Image.getInstance(imgObject.getImageAsBytes());
        img.scaleToFit(842, 595);
        img.setAbsolutePosition((842 - img.getScaledWidth()) / 2, (595 - img.getScaledHeight()) / 2);
        // We create a new document with the correct size
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        document.add(img);
        document.close();
    }
}
File nameRaw URLUpdated
RepeatImage.javaRepeatImage.java2015-11-07 5:42 pm
ReuseImage.javaReuseImage.java2015-11-07 5:42 pm
Resources: 
File nameRaw URLUpdated
chinese.pdfchinese.pdf2015-11-07 5:43 pm
single_image.pdfsingle_image.pdf2015-11-07 5:43 pm
Results: 
File nameRaw URLUpdated
cmp_chinese.pdfcmp_chinese.pdf2015-11-07 5:44 pm
cmp_image_on_A4.pdfcmp_image_on_A4.pdf2015-11-07 5:44 pm