// Path: src/main/java/mkl/testarea/pdfbox1/content/ImageLocator.java
// public class ImageLocation
// {
//     public ImageLocation(PDPage page, Matrix matrix, PDXObjectImage image)
//     {
//         this.page = page;
//         this.matrix = matrix;
//         this.image = image;
//     }
// 
//     public PDPage getPage()
//     {
//         return page;
//     }
// 
//     public Matrix getMatrix()
//     {
//         return matrix;
//     }
// 
//     public PDXObjectImage getImage()
//     {
//         return image;
//     }
// 
//     final PDPage page;
//     final Matrix matrix;
//     final PDXObjectImage image;
// }


import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.imageio.ImageIO;
import mkl.testarea.pdfbox1.content.ImageLocator.ImageLocation;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.exceptions.CryptographyException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.junit.BeforeClass;
import org.junit.Test;
package mkl.testarea.pdfbox1.content;





/**
 * <a href="http://stackoverflow.com/questions/31009949/replacing-images-with-same-resource-in-pdfbox">
 * Replacing images with same resource in PDFBox
 * </a>
 * <p>
 * Sample code showing how to overwrite images..
 * </p>
 * 
 * @author mkl
 */
public class OverwriteImage
{
    final static File RESULT_FOLDER = new File("target/test-outputs", "content");

    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
        RESULT_FOLDER.mkdirs();
    }

    /**
     * Applying the code to the OP's sample file from a former question.
     * 
     * @throws IOException
     * @throws CryptographyException
     * @throws COSVisitorException
     */
    @Test
    public void testDrunkenFistSample() throws IOException, CryptographyException, COSVisitorException
    {
        try (   InputStream resource = getClass().getResourceAsStream("sample.pdf");
                InputStream left = getClass().getResourceAsStream("left.png");
                InputStream right = getClass().getResourceAsStream("right.png");
                PDDocument document = PDDocument.load(resource) )
        {
            if (document.isEncrypted())
            {
                document.decrypt("");
            }
            
            PDJpeg leftImage = new PDJpeg(document, ImageIO.read(left));
            PDJpeg rightImage = new PDJpeg(document, ImageIO.read(right));

            ImageLocator locator = new ImageLocator();
            List<?> allPages = document.getDocumentCatalog().getAllPages();
            for (int i = 0; i < allPages.size(); i++)
            {
                PDPage page = (PDPage) allPages.get(i);
                locator.processStream(page, page.findResources(), page.getContents().getStream());
            }

// Path: src/main/java/mkl/testarea/pdfbox1/content/ImageLocator.java
// public class ImageLocation
// {
//     public ImageLocation(PDPage page, Matrix matrix, PDXObjectImage image)
//     {
//         this.page = page;
//         this.matrix = matrix;
//         this.image = image;
//     }
// 
//     public PDPage getPage()
//     {
//         return page;
//     }
// 
//     public Matrix getMatrix()
//     {
//         return matrix;
//     }
// 
//     public PDXObjectImage getImage()
//     {
//         return image;
//     }
// 
//     final PDPage page;
//     final Matrix matrix;
//     final PDXObjectImage image;
// }


// Path: src/test/java/mkl/testarea/pdfbox1/content/OverwriteImage.java
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.imageio.ImageIO;
import mkl.testarea.pdfbox1.content.ImageLocator.ImageLocation;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.exceptions.CryptographyException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.junit.BeforeClass;
import org.junit.Test;

package mkl.testarea.pdfbox1.content;





/**
 * <a href="http://stackoverflow.com/questions/31009949/replacing-images-with-same-resource-in-pdfbox">
 * Replacing images with same resource in PDFBox
 * </a>
 * <p>
 * Sample code showing how to overwrite images..
 * </p>
 * 
 * @author mkl
 */
public class OverwriteImage
{
    final static File RESULT_FOLDER = new File("target/test-outputs", "content");

    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
        RESULT_FOLDER.mkdirs();
    }

    /**
     * Applying the code to the OP's sample file from a former question.
     * 
     * @throws IOException
     * @throws CryptographyException
     * @throws COSVisitorException
     */
    @Test
    public void testDrunkenFistSample() throws IOException, CryptographyException, COSVisitorException
    {
        try (   InputStream resource = getClass().getResourceAsStream("sample.pdf");
                InputStream left = getClass().getResourceAsStream("left.png");
                InputStream right = getClass().getResourceAsStream("right.png");
                PDDocument document = PDDocument.load(resource) )
        {
            if (document.isEncrypted())
            {
                document.decrypt("");
            }
            
            PDJpeg leftImage = new PDJpeg(document, ImageIO.read(left));
            PDJpeg rightImage = new PDJpeg(document, ImageIO.read(right));

            ImageLocator locator = new ImageLocator();
            List<?> allPages = document.getDocumentCatalog().getAllPages();
            for (int i = 0; i < allPages.size(); i++)
            {
                PDPage page = (PDPage) allPages.get(i);
                locator.processStream(page, page.findResources(), page.getContents().getStream());
            }

            for (ImageLocation location : locator.getLocations())