I need to find the (raster) images in a pdf file and resize them (to change their resolution).
My code is based on the example PrintImageLocations. For the part that works, I extend PDFStreamEngine and do special processing for the "Do" operator:
- I get the original PDImageXObject using the first parameter of the operator and the resources. 
- then I create a BufferedImage from that and work on it to change the number of pixels. 
- then I create a new PDImageXObject from the BufferedImage via LosslessFactory 
- finally I put the new object in the page resources using the original object's name 
I try to do something similar for inline images and I arrive at the point where I have a BufferedImage, but I don't know how to use it to replace the original inline image.
It would also be ok to replace the inline image with a XObject, but again, I don't know how to substitute the two...
Below is my code; the interesting part is the function "processOperator".
// WIP!
// find raster images inside a pdf
// if their resolution is more than 900dpi
// then resize them
// reducing the resolution to 200dpi
// NB bug: fails on pdf files with more than one page
// ...DEBUG ScratchFileBuffer:516 - ScratchFileBuffer not closed!
// also fails on pdf with included pdf
// (e.g. latex \includegraphics{x.pdf})
// # to compile:
// apt install libpdfbox2-java
// export CLASSPATH=.:/usr/share/java/pdfbox2.jar:/usr/share/java/commons-logging.jar
// javac Resampleimages.java
// # to run:
// java Resampleimages x.pdf
// see
// https://pdfbox.apache.org/2.0/examples.html
// https://pdfbox.apache.org/docs/2.0.11/javadocs/
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImage;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDInlineImage;
import org.apache.pdfbox.util.Matrix;
import org.apache.pdfbox.contentstream.operator.DrawObject;
import org.apache.pdfbox.contentstream.operator.Operator;
import org.apache.pdfbox.contentstream.PDFStreamEngine;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Color;
import org.apache.pdfbox.contentstream.operator.state.Concatenate;
import org.apache.pdfbox.contentstream.operator.state.Restore;
import org.apache.pdfbox.contentstream.operator.state.Save;
import org.apache.pdfbox.contentstream.operator.state.SetGraphicsStateParameters;
import org.apache.pdfbox.contentstream.operator.state.SetMatrix;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ReplaceBigImages extends PDFStreamEngine
{
    private static Log log = LogFactory.getLog(ReplaceBigImages.class);
    public ReplaceBigImages() throws IOException
    {
        addOperator(new Concatenate());
        addOperator(new DrawObject());
        addOperator(new SetGraphicsStateParameters());
        addOperator(new Save());
        addOperator(new Restore());
        addOperator(new SetMatrix());
    }
    public static PDDocument document;
    public static void main( String[] args ) throws IOException
    {
        if( args.length != 1 )
        {
            usage();
        }
        else
        {
            try
            {
                document = PDDocument.load(new File(args[0]));
                ReplaceBigImages printer = new ReplaceBigImages();
                int pageNum = 0;
                for( PDPage page : document.getPages() )
                {
                    pageNum++;
                    log.info( "Processing page: " + pageNum );
                    printer.processPage(page);
                }
            }
            finally {
                if( document != null )
                    {
                        document.save(args[0].replace(".pdf", "_test.pdf"));
                        document.close();
                    }
            }
        }
    }
    protected void processOperator( Operator operator, List<COSBase> operands) throws IOException
    {
        String operation = operator.getName();
        // log.debug(String.format("Operator %s", operation));
        if( "Do".equals(operation) ) {
            log.debug("### Found Do operator");
            COSName objectName = (COSName) operands.get( 0 );
            PDXObject xobject = getResources().getXObject( objectName );
            // log.debug(String.format("%s isa %s", objectName, xobject.getClass().getSimpleName()));
            if( xobject instanceof PDImageXObject)
            {
                log.debug(String.format("Looking at %s (%s)", objectName.getName(), xobject));
                PDImageXObject image = (PDImageXObject)xobject;
                BufferedImage scaledImage = changeImageResolution(image);
                if (scaledImage != null) {
                    log.debug(String.format("Replacing with %s", scaledImage));
                    PDImageXObject replacement_img = LosslessFactory.createFromImage(document, scaledImage);
                    PDPage currentPage = getCurrentPage();
                    PDResources resources = currentPage.getResources();
                    resources.put(objectName, replacement_img);
                }
            }else if(xobject instanceof PDFormXObject)
            {
                PDFormXObject form = (PDFormXObject)xobject;
                showForm(form);
            }
        } else if  ("BI".equals(operation)) {
            PDPage currentPage = getCurrentPage();
            log.debug("### Found BI operator");
            PDResources resources = currentPage.getResources();
            PDInlineImage image = new PDInlineImage(operator.getImageParameters(),
                                                    operator.getImageData(),
                                                    resources);
            BufferedImage scaledImage = changeImageResolution(image);
            if (scaledImage != null) {
                log.debug(String.format("Replacing with %s", scaledImage));
                PDImageXObject replacement_img = LosslessFactory.createFromImage(document, scaledImage);
                // ARGH!!! How do I replace the inline image???
                resources.add(replacement_img, "pippo");
                // operator.setImageParameters(scaledImage???)
                // operator.setImageData(scaledImage???)
            }
        } else {
            super.processOperator( operator, operands);
        }
    }
    protected BufferedImage changeImageResolution( PDImage image)
        throws IOException
    {
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();
        Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();
        float imageXScale = Math.abs(ctmNew.getScalingFactorX());
        float imageYScale = Math.abs(ctmNew.getScalingFactorY());
        float resolution = imageWidth / ( imageXScale / 72 );
        String stencil = "";
        if (image.isStencil()) {
            stencil = " (stencil)";
        }
        // TODO: take into consideration the size at which this file is included by TeX
        log.debug("size: ("+imageWidth+","+imageHeight+")@("+imageXScale+","+imageYScale+") resolution = "+resolution+stencil);
        // if ( resolution > 899f ) {
        if ( resolution > 200f ) {
            // what do the following two lines mean???
            BufferedImage bImage = new BufferedImage(imageWidth,
                                                     imageHeight,
                                                     BufferedImage.TYPE_INT_ARGB);
            if (image.isStencil()) {
                log.warn("Is stencil; painting black.");
                bImage = image.getStencilImage(Color.black);
            } else {
                bImage = image.getImage();
            }
            int desiredResolution = 200;
            float xFactor = (imageXScale / 72) * desiredResolution / imageWidth;
            float yFactor = (imageYScale / 72) * desiredResolution / imageHeight;
            log.info("Scaling x to "+xFactor);
            int dWidth = (int) (xFactor * imageWidth);
            int dHeight = (int) (yFactor * imageHeight);
            // the image type is from
            // https://docs.oracle.com/javase/6/docs/api/constant-values.html#java.awt.image.
            log.debug(String.format("Destination: %d x %d [%s]",
                                    dWidth,
                                    dHeight,
                                    bImage.getType()));
            BufferedImage scaledImage = new BufferedImage(dWidth,
                                                          dHeight,
                                                          bImage.getType());
            Graphics2D graphics2D = scaledImage.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,
                                        RenderingHints.VALUE_RENDER_QUALITY);
            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                        RenderingHints.VALUE_ANTIALIAS_ON);
            graphics2D.drawImage(bImage, 0, 0, dWidth, dHeight, null);
            graphics2D.dispose();
            // see https://pdfbox.apache.org/docs/2.0.11/javadocs/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.html#createFromByteArray-org.apache.pdfbox.pdmodel.PDDocument-byte:A-java.lang.String-
            return scaledImage;
        }
        return null;
    }
    private static void usage()
    {
        System.err.println( "Usage: java " + ReplaceBigImages.class.getName() + " <input-pdf>" );
    }
}
