public void doIt( String[] args ) throws IOException
    {
        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] );
            }
        }
    }
@Test
    void test() throws IOException
    {
        String documentFile = "src/test/resources/org/apache/pdfbox/examples/pdmodel/document.pdf";
        String stampFile = "src/test/resources/org/apache/pdfbox/examples/pdmodel/stamp.jpg";
        String outFile = "target/test-output/TestRubberStampWithImage.pdf";

        new File("target/test-output").mkdirs();

        BufferedImage bim1;
        try (PDDocument doc1 = Loader.loadPDF(new File(documentFile)))
        {
            bim1 = new PDFRenderer(doc1).renderImage(0);
        }

        String[] args = { documentFile, outFile, stampFile };
        RubberStampWithImage rubberStamp = new RubberStampWithImage();
        rubberStamp.doIt(args);

        try (PDDocument doc2 = Loader.loadPDF(new File(outFile)))
        {
            BufferedImage bim2 = new PDFRenderer(doc2).renderImage(0);
            assertFalse(compareImages(bim1, bim2));
            PDAnnotationRubberStamp rubberStampAnnotation = (PDAnnotationRubberStamp) doc2.getPage(0).getAnnotations().get(0);
            PDAppearanceDictionary appearance = rubberStampAnnotation.getAppearance();
            PDAppearanceEntry normalAppearance = appearance.getNormalAppearance();
            PDAppearanceStream appearanceStream = normalAppearance.getAppearanceStream();
            PDImageXObject ximage = (PDImageXObject) appearanceStream.getResources().getXObject(COSName.getPDFName("Im1"));
            BufferedImage actualStampImage = ximage.getImage();
            BufferedImage expectedStampImage = ImageIO.read(new File(stampFile));
            assertTrue(compareImages(expectedStampImage, actualStampImage));
        }
    }