class RubberStampWithImage
{
    private static final String SAVE_GRAPHICS_STATE = "q\n";
    private static final String RESTORE_GRAPHICS_STATE = "Q\n";
    private static final String CONCATENATE_MATRIX = "cm\n";
    private static final String XOBJECT_DO = "Do\n";
    private static final String SPACE = " ";

    private static final NumberFormat FORMATDECIMAL = NumberFormat.getNumberInstance( Locale.US );

    /**
     * Add a rubber stamp with an jpg image to every page of the given document.
     * @param args the command line arguments
     * @throws IOException an exception is thrown if something went wrong
     */
    public void doIt( String[] args ) throws IOException
    {<FILL_FUNCTION_BODY>}
    
    private void drawXObject( PDImageXObject xobject, PDResources resources, OutputStream os,
            float x, float y, float width, float height ) throws IOException
    {
        // This is similar to PDPageContentStream.drawXObject()
        COSName xObjectId = resources.add(xobject);

        appendRawCommands( os, SAVE_GRAPHICS_STATE );
        appendRawCommands( os, FORMATDECIMAL.format( width ) );
        appendRawCommands( os, SPACE );
        appendRawCommands( os, FORMATDECIMAL.format( 0 ) );
        appendRawCommands( os, SPACE );
        appendRawCommands( os, FORMATDECIMAL.format( 0 ) );
        appendRawCommands( os, SPACE );
        appendRawCommands( os, FORMATDECIMAL.format( height ) );
        appendRawCommands( os, SPACE );
        appendRawCommands( os, FORMATDECIMAL.format( x ) );
        appendRawCommands( os, SPACE );
        appendRawCommands( os, FORMATDECIMAL.format( y ) );
        appendRawCommands( os, SPACE );
        appendRawCommands( os, CONCATENATE_MATRIX );
        appendRawCommands( os, SPACE );
        appendRawCommands( os, "/" );
        appendRawCommands( os, xObjectId.getName() );
        appendRawCommands( os, SPACE );
        appendRawCommands( os, XOBJECT_DO );
        appendRawCommands( os, SPACE );
        appendRawCommands( os, RESTORE_GRAPHICS_STATE );
    }

    private void appendRawCommands(OutputStream os, String commands) throws IOException
    {
        os.write( commands.getBytes(StandardCharsets.ISO_8859_1));
    }

    /**
     * This creates an instance of RubberStampWithImage.
     *
     * @param args The command line arguments.
     *
     * @throws IOException If there is an error parsing the document.
     */
    public static void main( String[] args ) throws IOException
    {
        RubberStampWithImage rubberStamp = new RubberStampWithImage();
        rubberStamp.doIt(args);
    }

    /**
     * This will print the usage for this example.
     */
    private void usage()
    {
        System.err.println( "Usage: java "+getClass().getName()+" <input-pdf> <output-pdf> <image-filename>" );
    }
}

        if( args.length != 3 )
        {
            usage();
        }
        else 
        {
            try (PDDocument document = Loader.loadPDF(new File(args[0])))
            {
                if( document.isEncrypted() )
                {
                    throw new IOException( "Encrypted documents are not supported for this example" );
                }
    
                for (int i = 0; i < document.getNumberOfPages(); i++)
                {
                    PDPage page = document.getPage(i);
                    List<PDAnnotation> annotations = page.getAnnotations();
                    PDAnnotationRubberStamp rubberStamp = new PDAnnotationRubberStamp();
                    rubberStamp.setName(PDAnnotationRubberStamp.NAME_TOP_SECRET);
                    rubberStamp.setRectangle(new PDRectangle(200,100));
                    rubberStamp.setContents("A top secret note");

                    // create a PDXObjectImage with the given image file
                    // if you already have the image in a BufferedImage, 
                    // call LosslessFactory.createFromImage() instead
                    PDImageXObject ximage = PDImageXObject.createFromFile(args[2], document);           

                    // define and set the target rectangle
                    float lowerLeftX = 250;
                    float lowerLeftY = 550;
                    float formWidth = 150;
                    float formHeight = 25;
                    float imgWidth = 50;
                    float imgHeight = 25;
                    
                    PDRectangle rect = new PDRectangle();
                    rect.setLowerLeftX(lowerLeftX);
                    rect.setLowerLeftY(lowerLeftY);
                    rect.setUpperRightX(lowerLeftX + formWidth);
                    rect.setUpperRightY(lowerLeftY + formHeight);

                    // Create a PDFormXObject
                    PDFormXObject form = new PDFormXObject(document);
                    form.setResources(new PDResources());
                    form.setBBox(rect);
                    form.setFormType(1);

                    // adjust the image to the target rectangle and add it to the stream
                    try (OutputStream os = form.getStream().createOutputStream())
                    {
                        drawXObject(ximage, form.getResources(), os, lowerLeftX, lowerLeftY, imgWidth, imgHeight);
                    }

                    PDAppearanceStream myDic = new PDAppearanceStream(form.getCOSObject());
                    PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary());
                    appearance.setNormalAppearance(myDic);
                    rubberStamp.setAppearance(appearance);
                    rubberStamp.setRectangle(rect);

                    // add the new RubberStamp to the document
                    annotations.add(rubberStamp);
                
                }
                document.save( args[1] );
            }
        }
    
<no_super_class>