Do you save directly or incrementally?

If directly then the old one should be gone. If not, please share the PDF (upload to sharehoster) and tell us why you think it's still there.

Tilman

On 26.04.2024 12:37, Jurgen Doll wrote:
Hi

I would like to know how to remove an image resource from a PDF form.

I use the following code to set an image object on a field:

    private static void setAppearance( PDDocument pdfDoc, PDField field, PDImageXObject image )
    {
        var widget = field.getWidgets().get(0);
        var rect = widget.getRectangle();

        var formObj = new PDFormXObject( new PDStream( pdfDoc ) );
        formObj.setResources( new PDResources() );
        formObj.setBBox( new PDRectangle
        (
            // The TransformationMatrix below enlarges the image slightly,
            // so adjust accordingly here otherwise the image is cropped.
            rect.getLowerLeftX(), rect.getLowerLeftY() - 10,
            rect.getWidth(), rect.getHeight() + 20
        ));
        formObj.setFormType(1);

        var appearanceStream = new PDAppearanceStream( formObj.getCOSObject() );         var pageContentStream = new PDPageContentStream( pdfDoc, appearanceStream );         pageContentStream.drawImage( image, getTransformationMatrix( image, widget ) );
        pageContentStream.close();

        var appearance = new PDAppearanceDictionary();
        appearance.setNormalAppearance( appearanceStream );
        appearance.getCOSObject().setDirect( true );
        widget.setAppearance( appearance );
    }

To remove the image I do "widget.setAppearance( null )" or to replace the image I use the above method to set the new image as the appearance. However I've noticed that this doesn't remove the previous image object/resource from the PDF. How do I go about removing it ?

Thanks, regards
Jurgen

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org

Reply via email to