0

I'm searching for any .pdf in a directory that doesn't currently have a Title property and replacing it with the file name of the document. I've seen a couple of instances in which this is done such as here, but it looks like they're replacing the entire document. I just want to add a Title to the document without completely rewriting it.

How can I do this with iTextSharp?

Community
  • 1
  • 1
The Vanilla Thrilla
  • 1,915
  • 10
  • 30
  • 49
  • 1
    It is done as explained [here](http://stackoverflow.com/questions/7380837/set-metadata-in-itextsharp). (1.) Every byte counts in PDF; changing *X* bytes into *Y* bytes can make your complete PDF corrupt, hence you have to rewrite all the bytes. (2.) See [How to update a PDF without creating a new PDF](http://stackoverflow.com/questions/16081831/how-to-update-a-pdf-without-creating-a-new-pdf) to find why many tools completely rewrite files. Moreover: are you talking about the metadata in the Info dictionary or in the XMP stream, or both? – Bruno Lowagie May 26 '15 at 13:50
  • I believe it's part of the info dictionary. – The Vanilla Thrilla May 26 '15 at 13:58
  • If the input and output file have the same directory/name, will the output file simply override the input file, or will it throw an exception? – The Vanilla Thrilla May 26 '15 at 14:10
  • It will throw an exception: you can not overwrite a file while you are still reading it. Also: if the PDF is a PDF/A file and you change the title in the Info dictionary, then you'll break PDF/A compliance unless you also change the title in the XMP stream. – Bruno Lowagie May 26 '15 at 14:24
  • Just to add on to Bruno's comment, if your PDFs are relatively small (relative to your server's memory, you'll need to gauge this on your own) you can read the entire file into a byte array first and then save over your original file. – Chris Haas May 26 '15 at 16:32
  • I've made a python script that achieves just that, and also supports recursive scan for pdfs in folders. What OS do you use? – 1w3j Jan 15 '20 at 04:56

0 Answers0