How to add / delete / retrieve information from a PDF using a custom property?

Tags: metadatainfo dictionarycustom propertyiText 7

I want to store 2 strings (string1 and string2) as a custom entry (key and value) that is stored inside the PDF in the custom property area. You can find this custom property area under File > Properties > Custom > Custom properties in Adobe Reader, where you can find "Name" and "Value" in pairs. I want to store string1 in the "Value" and store string2 in the "Name". Later, I want to retrieve/delete the strings in the custom property area.

Posted on StackOverflow on Nov 4, 2014 by brian

In order to better understand the problem, I've used Acrobat to add a custom metadata entry named "Test" with value "test" and when you look inside that file, you can see that this key/value pair turns up on two places (marked with a red dot).

  1. It is present in the Info dictionary, which is the traditional place to store metadata.
  2. It is present in the XMP metadata stream as a tag named Test with prefix pdfx (for custom tags).
Metadata inside a PDF file
Metadata inside a PDF file

Adding an extra value to the Info dictionary is easy when using iText. Updating the XMP metadata is also possible, but you'll have to create the XMP stream yourself and maybe it's overkill in your case. Maybe your PDF only has an Info dictionary and no XMP.

Moreover: you say that the purpose of having that key is to retrieve its value and to delete the custom entry afterwards. In that case, it's sufficient to add the extra entry in the Info dictionary.

Depending on whether you want to add a custom entry to the Info dictionary to a PDF created from scratch or to an existing PDF you need one of the following examples.

In CustomMetaEntry, we add a standard metadata entry for the title and a custom entry, Test.

public void createPdf(String dest) throws IOException {
    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileOutputStream(dest)));
    Document doc = new Document(pdfDoc);
    pdfDoc.getDocumentInfo().setTitle("Some example");
    pdfDoc.getDocumentInfo().setMoreInfo("Test", "test");
    Paragraph p = new Paragraph("Hello World");
    doc.add(p);
    doc.close();
}

As you can see, iText had setX() methods to add Title, Author,... metadata. However, if you want to add a custom entry, you need to use the setMoreInfo(). You need to add the metadata before opening the document.

If you want to modify entries in the info dictionary of an existing PDF, take a look at the MetadataPdf example:

public void manipulatePdf(String src, String dest) throws IOException {
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
    PdfDocumentInfo info = pdfDoc.getDocumentInfo();
    info.setTitle("Hello World stamped");
    info.setSubject("Hello World with changed metadata");
    info.setKeywords("iText in Action, PdfStamper");
    info.setCreator("Silly standalone example");
    info.setAuthor("Also Bruno Lowagie");
    pdfDoc.close();
}

In this example, we get the info dictionary from a PdfDocument instance using the getDocumentInfo() method. This also answers how to retrieve the custom data from a PDF. If the Map contains an entry with key Test, you can get its value like this:

String value=  info.getPdfObject().get(new PdfName("Test")).toString();

You can now add extra pairs of Strings to this Map. In the example, we add standard keys for metadata, but you can also use custom keys.

Removing an entry from an existing PDF file is done in the same way as adding an entry. It's sufficient to add a null value. For instance:

info.setMoreInfo("Test", null);

This will remove a custom entry named Test in case such a value was present in your Info dictionary.

Metadata inside a PDF file

Click this link if you want to see how to answer this question in iText 5.