I have several use cases for editing metadata of PDF documents programmatically.The idea is for example to remove the author and the producer of a PDF document.
The idea is to leave everything else in the PDF document untouched and only edit the needed objects in order not to break anything.
If you look at the PDF specification (or a PDF document with a text editor) the structure of a PDF document is quite clear. It's just a bunch of objects that might contain the data I am searching for (if it is further evaluated). For example:
9 0 obj <</Author(Lastname, Givenname) /Creator(þÿ M i c r o s o f t ® W o r d f ü r M i c r o s o f t 3 6 5) /CreationDate(D:20210419134552+02'00') /ModDate(D:20210419134552+02'00') /Producer(þÿ M i c r o s o f t ® W o r d f ü r M i c r o s o f t 3 6 5) >> endobj
So what I have to do is find the correct object that contains this data and remove or alter the data there. This could be made by some string search algorithm inside the PDF data. Which is of course error prone for arbitrary PDF documents because of:
- The objects can as far as I can tell be compressed "streams" which makes a text search impossible
- The order of the settings inside the objects can be different
- You would have to make sure to not resize the data in any of the objects since there is some kind of index at the end of a PDF (citation needed) that points to byte positions inside the PDF and this index would then have to be rebuilt.
A better alternative would be to have a kind of parser/editor that understands the low level structure of a PDF document (objects, streams, index) and would let you edit the found objects as strings.
This does not seem to hard but is too much for the budget at hand to do it right.
Unfortunately I did not find any (open source) library that is able to do this so I ended up here. This is a commercial use case so I think I can rule iText 7 out.