0

I am writing a little archiver for PDF documents and would like to add a few Keywords to the metadata.

I used code from Read/Modify PDF Metadata using iTextSharp but when adding them, some metadata is overridden (ie ModDate). Is there a way to suppress this behaviour? This is not helpful for (ie searching/ filtering) documents lateron.

PDF Producer: iText 1.4 (by lowagie.com); modified using iTextSharp (TM) 5.5.6 (c) 2000-2014 iTextGroup NV (AGPL-version)

Code I use to add keywords

    private void Button_Click_ADD_KEYWORDS(object sender, RoutedEventArgs e)
    {
        using (MemoryStream m = new MemoryStream())
        {
            using (PdfReader pdfReader = new PdfReader(@"c:\sim\input.pdf"))
            using (PdfStamper pdfStamper = new PdfStamper(pdfReader, m))
            {
                pdfStamper.Writer.CloseStream = false;

                Dictionary<String, String> info_orig = pdfStamper.Reader.Info;
                Dictionary<String, String> info_new = new Dictionary<String, String>();

                foreach (String key in info_orig.Keys)
                {
                    if(key.ToLower().Equals("keywords"))
                    {
                        info_new["Keywords"] = info_orig["Keywords"] + "; " + this.AddKeywords();
                    }
                    else
                    {
                        info_new[key] = info_orig[key];
                    }
                }

                info_orig.Clear();

                pdfStamper.MoreInfo = info_new;
            }

            m.Seek(0L, SeekOrigin.Begin);

            using (FileStream f = new FileStream(@"C:\sim\output.pdf", FileMode.Create))
            {
                m.CopyTo(f);
            }
        }

        MessageBox.Show("I think it worked ...");
    }
Community
  • 1
  • 1
Benj
  • 889
  • 1
  • 14
  • 31
  • Let me try to understand your question ... you would like to modify a file and have the modified date to be not changed. Is that your question? – Kevin Brown Jun 22 '15 at 01:15
  • Sort of. I would like to change specific metadata fields without having other metadata fields (ie the one labeled `Modified date`) automatically updated by the framework. The file system's `Last modified` field can be updated. – Benj Jun 22 '15 at 05:01
  • Or to put it another way: I want the `Last modified` field in the metadata refer to the last time the documents *content* has been modified. not its metadata. For metadata fields I want to use the file system's timestamp – Benj Jun 22 '15 at 05:07
  • The only way I see right now is to re-edit he metadata in Visual Studio, but I can hardly include that as a package for my project :-). Any hint appreciated to solve this programmatically – Benj Jun 22 '15 at 07:00
  • Looking at the underlying code for `PdfStamperImp` it appears that the [date setting code](http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/src/core/iTextSharp/text/pdf/PdfStamperImp.cs#l285) is baked into the `Close()` method. – Chris Haas Jun 22 '15 at 13:40

0 Answers0