Question

The last tag in an XFDF file looks something like this:

<ids original="20639838865717E80D2556CB7B2AEC2D" 
     modified="754C78B10C9159419708446C3395CDBE"/>

I can get these values by exporting PDF form data from Acrobat using this method: http://wiki.developerforce.com/page/Adobe_XFDF_Ids_Determination.

However I want to get the IDs programmatically in order to build correct xfdf documents for arbitrary PDF forms.

How do I get these values using iText?

Was it helpful?

Solution

As explained in this post (removing PDFID in PDF) /ID is a recommended entry in the "trailer dictionary" (and required if an AcroForm is encrypted).

Using iText, IDs are accessed as a PdfArray of two PdfString objects in the trailer PdfDictionary. The String values will look like garbage because each is a representation of a byte array. These are the hex values you need for "original" and "modified".

The following code will print out the two IDs, which can be verified against e.g. an export from Acrobat Pro (NB Hex.encodeHexString is Apache commons-codec):

public void printIds(PdfReader reader) {
    PdfDictionary trailer = reader.getTrailer();
    if (trailer.contains(PdfName.ID)) {
        PdfArray ids = (PdfArray) trailer.get(PdfName.ID);
        PdfString original = ids.getAsString(0);
        PdfString modified = ids.getAsString(1);
        System.out.println(Hex.encodeHexString(original.getBytes()));
        System.out.println(Hex.encodeHexString(modified.getBytes()));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top