Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

PDF Properties (e.g. Title)

Help
2008-01-19
2013-01-26
  • Chris Alcock
    Chris Alcock
    2008-01-19

    How do I set the PDF properties, e.g. the Title, Author, Subject, etc.?

    I am using C# with the following code (which doesn't do anything):

                File file = new File(@"C:\files\test1.pdf");
                Document document = file.Document;          
               
                document.Information.Author = "John Doe";
                document.Information.Update();
                document.Update();
               
                file.WriteTo(@"C:\files\test2.pdf", SerializationModeEnum.Standard);

    Reading such properties is no problem, but writing them is.  Also, is there a problem with opening and saving the same file path (e.g. using @"C:\files\test1.pdf" in both the File constructor and WriteTo method? I seem to be getting "file in use by another proccess" on the WriteTo method when I try this.

    Thanks.

     
    • Hi Chris,

      1) Information editing

      Your code is almost correct: its only flaw is about the (legitimate, but wrong) assumption that the Information object is kept alive in the state of the Document object: as the objects at document level (i.e. those within documents.* subnamespaces) are by-design just volatile wrappers (views) of those at file level (i.e. those within files.* subnamespaces), anytime you request a document-level object (like Information) it queries its file-level counterpart for instantiation: since in the meantime it hasn't been updated yet, it serves the same unaltered data (therefore losing your changes).
      The right way to proceed is by keeping your Information instance and manipulating it:

      File file = new File(@"C:\files\test1.pdf");
      Document document = file.Document; 

      Information info = doc.Information; // keeps a reference to the Information instance.
      info.Author = "John Doe";
      info.Update(); // updates the metadata.

      file.WriteTo(@"C:\files\test2.pdf", SerializationModeEnum.Standard);

      I understand that's not intuitive: I'm considering to implement some caching mechanism to solve this so-perceived issue.

      By the way, for efficiency matters it's always a good general programming practise *not* to access multiple times the same interface getter (opt to call it once and assign it to a local variable instead!).

      2) Opening and saving the same file path

      If you instruct PDF Clown to read directly from a PDF file sitting in your file system you can't save it just by overwrite because, for efficiency matters (random access to arbitrarily-large files), PDF Clown keeps an input stream open to the file while working on it.

      If using a temporary output file that's eventually going to substitute your original file annoys you, I suggest to load into memory your file and passing it to PDF Clown via MemoryStream: this way you'll be able to freely overwrite your original file. NOTE: this implies that, if something goes wrong, you'll end up with a corrupted (and possibly unrecoverable) file! I provide as much care as possible to ensure the reliability of the library, but I can't guarantee that it would work correctly in any context (I'm sure you understand me ;-) ).

      Thanks for your requests
      Stefano

       
    • Chris Alcock
      Chris Alcock
      2008-01-20

      Stefano,

      Thanks for the help. Everything is working fine now.

      Also, I've very impressed both with the thouroughness (spelling?) and timeliness of your response. I wish all support forums were like this (you're even better than most of the large software development supporting companies). PDF Clown is awesome too; it is saving me an incredible amount of time.