1

Is it possible to modify/remove the creation date in metadata? I'm looking to do something similar to this:

Overwrite creationDate in pdf using iText and pdf writer

EDIT:

I have tried the following methods:

writer.Info.Remove(PdfName.CREATIONDATE);

or

writer.Info.Put(PdfName.CREATIONDATE, new PdfDate(new DateTime(2017, 01, 01)));

where writer is a PdfWriter object.

However, that creates a copy of the object (a PdfDictionary) and doesn't modify the PDF I'm creating.

I also can't assign i.e. writer.Info = info

I tried following the advice given in the Java article.

I tried to do this:

var info = writer.Info;
stamper.MoreInfo = info

where stamper is a PdfStamper

But the types are incompatible and I don't think this would work. Does anyone know the actual methods to remove/modify the metadata?

EDIT 2: Here is the code, I'm creating a new file from an existing PDF.

var filename = @"C:\Users\Someone\Documents\aPdf.pdf";
        using( var output = new MemoryStream() )
        {
            Document document = new Document();
            PdfCopy writer = new PdfCopy( document, output );
            writer.CloseStream = false;

            document.Open();

            //read in PDF
            PdfReader reader = new PdfReader(filename);
            reader.ConsolidateNamedDestinations();

            PdfImportedPage page = writer.GetImportedPage(reader, 1);
            writer.AddPage(page);

            reader.Close();

            writer.Close();
            document.Close();

            return output.ToArray();
        }

Now, when I open the file with a text editor this line is inserted (I need it constant/gone):

<</Producer(iTextSharp’ 5.5.12 ©2000-2017 iText Group NV \(AGPL-version\))/CreationDate(D:20180412155130+01'00')/ModDate(D:20180412155130+01'00')>>

The reason why we need to remove/set the date is that we're taking the MD5 hash of the file. Every time a new document is generated, that line changes leading to different MD5 hashes.

PraiseTheBaud
  • 80
  • 2
  • 6
  • 1
    Yes, very often you can outright remove all meta-data and it would still be a valid file. That's why its called meta-data. – Neijwiert Apr 12 '18 at 09:10
  • Possible duplicate of [iText setting Creation Date & Modified Date in sandbox.stamper.SuperImpose.java](https://stackoverflow.com/questions/37110820/itext-setting-creation-date-modified-date-in-sandbox-stamper-superimpose-java) – Manfred Radlwimmer Apr 12 '18 at 09:14
  • The linked duplicate is java, but it should be **very** similar in C# – Manfred Radlwimmer Apr 12 '18 at 09:15
  • *"doesn't modify the PDF I'm creating"* - are you modifying a PDF or creating a new one? Please share enough code to reproduce the issue, the expected result, and the observed result. – mkl Apr 12 '18 at 13:40
  • *"Every time a new document is generated, that line changes leading to different MD5 hashes."* - Not only that line changes, the generated ID for the document most likely will change, too! – mkl Apr 12 '18 at 16:12
  • 1
    The `PdfDocument` inner class `PdfInfo` explicitly prevents changing the **Producer** and **CreationDate** values. You might try reflection to circumvent this. – mkl Apr 12 '18 at 16:30
  • 1
    I also really needed this kind of thing, and I have made DefinitivePDF.cs according to the idea to reduce `/CreationDate`, `/ModDate`, and `/ID` https://gist.github.com/kenjiuno/df942d7cecb96dd826862dc2829e4241 – kenjiuno Sep 15 '20 at 05:20

1 Answers1

2

As I was trying to get a constant MD5 checksum for the generated file, I had to also set the ID constant, as mentioned by mkl.

My solution was to search byte array produced (i.e. the created PDF), and manually set the values to constants. The text is ASCII chars. I removed the /CreationDate and /ModifiedDated from the PDF entirely, and set the generated ID to a constant arbitrary value.

PraiseTheBaud
  • 80
  • 2
  • 6