Search code examples
javaimagepdfxfdfpdf-annotations

How to create XFDF annotation with an image


I would like to generate in Java a XFDF Stamp Annotation that would have a transparent image inside. I have serious trouble recognizing the format in which the image is stored and how to transform my input image to that form.

So far I know

  • The stamp element is a child of the annots element and corresponds to the Rubber Stamp annotation. A Rubber Stamp annotation displays text or graphics intended to look as if they were stamped on the page with a rubber stamp. If present, the appearance child element (the AP key in the annotation dictionary) takes precedence over the icon attribute (Name key in the rubber stamp annotation dictionary). Content model ( contents-richtext? & contents? & appearance? & popup? )
  • The appearance element is a child of the stamp element and corresponds to the AP key in the annotation dictionary. The value is a base 64 encoded string. Content model Base 64 encoded string. Attributes None.

I have an example XFDF which contains an image, when I decode the image I get to see a syntax like this, but sadly I have no idea what kind of raw data is present in <DATA MODE="RAW" ENCODING="HEX"> tag.

<DICT KEY="AP">
<STREAM KEY="N">
    <ARRAY KEY="BBox">
        <FIXED VAL="250"/>
        <FIXED VAL="550"/>
        <FIXED VAL="400"/>
        <FIXED VAL="575"/>
</ARRAY>
    <INT KEY="FormType" VAL="1"/>
    <INT KEY="Length" VAL="35"/>
    <DICT KEY="Resources">
        <DICT KEY="XObject">
            <STREAM KEY="Im1">
                <INT KEY="BitsPerComponent" VAL="8"/>
                <NAME KEY="ColorSpace" VAL="DeviceRGB"/>
                <NAME KEY="Filter" VAL="FlateDecode"/>
                <INT KEY="Height" VAL="150"/>
                <INT KEY="Length" VAL="1907"/>
                <STREAM KEY="SMask">
                    <INT KEY="BitsPerComponent" VAL="8"/>
                    <NAME KEY="ColorSpace" VAL="DeviceGray"/>
                    <NAME KEY="Filter" VAL="FlateDecode"/>
                    <INT KEY="Height" VAL="150"/>
                    <INT KEY="Length" VAL="5690"/>
                    <NAME KEY="Subtype" VAL="Image"/>
                    <NAME KEY="Type" VAL="XObject"/>
                    <INT KEY="Width" VAL="300"/>
                    <DATA MODE="RAW" ENCODING="HEX">

I have managed to create a Rubber Stamp Annotation with custom Image in a PDF with the help of PDFBox, but sadly PDFBox does not seem to support exporting this kind of annotation to XFDF, they support only exporting from Forms(AcroForm). I don't know any other tool in Java that will allow me to export it, except the paid ones.


Solution

  • It's a HEX representation of DCT format (JPEG). If want a transparent image stamp you'll have a two of these things in your XFDF, the image itself and the mask.