How do I add XMP metadata to each page of an existing PDF?

Tags: metadataXMPiText 7

So far all the examples seem to involve adding metadata while creating a new Document. I want to take an existing PDF and add a GUID to each page's XMP using the stamper.

Posted on StackOverflow on Feb 10, 2015 by Rahul

The most common use of XMP in the context of PDF is when you add an XMP stream for the whole document that is referred to from the root dictionary of the PDF (aka the Catalog).

However, if you consult the PDF specification, you notice that XMP can be used in the context of many other objects inside a PDF, the page level being one of them. If you look at the spec, you will discover that /Metadata is an optional key in the page dictionary. It expects a reference to an XMP stream.

Your question isn't about creating a PDF from scratch, but about modifying an existing PDF. In that case, you also need the page dictionary. It is very easy to obtain these dictionaries:

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
int n = pdfDoc.getNumberOfPages();
PdfPage page;
PdfNumber rotate;
for (int p = 1; p <= n; p++) {
    page = pdfDoc.getPage(p);
    PdfDictionary dictionary =page.getPdfObject();
    // do stuff with the page dictionary

}

This snippet was taken fro the Rotate90Degrees example. In that example, we look at the /Rotate entry which is a number:

PdfNumber rotate = page.getPdfObject().getAsNumber(PdfName.Rotate);

You need to look for the /Metadata entry, which is a stream. And in iText 7 you can do it like:

PdfStream stream = page.getXmpMetadata();

Maybe this stream is null, in this case, you need to add a /Metadata entry as is shown in the AddXmpToPage example:

PdfPage page = pdfDoc.getFirstPage();
page.setXmpMetadata(XMPMetaFactory.create());

If there is an XMP stream, you want to keep it and add something to it.

This is how you get the XMP bytes:

byte[] xmpBytes = page.getXmpMetadata().getBytes();

You perform your XML magic on these bytes, resulting a a new byte[] named newXmpBytes. You replace the original bytes with these new bytes like this:

page.setXmpMetadata(newXmpBytes);

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