0

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:

  • Encoding
  • 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.

0

This is a simple job and theoretically any PDF library should be able to do it.

With PDF4NET you can do it like this:

PDFFixedDocument document = new PDFFixedDocument(pdfFileName);
if (document.DocumentInformation != null)
{
    document.DocumentInformation.Author = "";
    document.DocumentInformation.Producer = "";

    // Save it only if you made changes
    document.Save(pdfFileName);
}

Disclaimer: I work for the company that develops PDF4NET.

0

You can change PDF properties by using Syncfusion’s Essential PDF (.NET PDF library).

KB: https://www.syncfusion.com/kb/9502/how-to-change-the-pdf-properties-using-c-and-vb-net

The whole suite of controls is available for free (commercial applications also) through the community license program if you qualify. The community license is the full product with no limitations or watermarks.

Note: I work for Syncfusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.