tags:

views:

788

answers:

3

How can I modify the PDF document properties programmatically using .NET code?

I have purchased a number of eBooks in PDF format and unfortunately the publishers have not set the Title, Author and Subject properties. You can see this on a document by accessing the file Properties dialog and selecting the PDF tab. This is a real pain when attempting to use the PDF eBook on an eReader device.

I don't want to have to purchase a full PDF Writer product to do this so I'm hoping someone can point me to a simple free library that I can use to modify the properties programmatically.

If no .NET library is available I'd appreciate any other technique.

+4  A: 

Here's a list of open-source PDF Libraries in C#

A couple of other libraries, that are not on that list:
ByteScout-PDF
iTextSharp

Mindaugas Mozūras
+3  A: 

Have you looked at iTextSharp? It's a PDF API for .Net. You can do some fairly useful stuff with PDF's using it.

iTextSharp on Sourceforge

Knobloch
+2  A: 

Thanks to both Mindaugas and Knobloch. Since you both pointed to iTextSharp I went for this and was able to solve my problem using iTextSharp and code similar to that shown below. One thing I noticed was that the resulting file was 115,143 bytes smaller, from a starting file of 3,639,172, so it looks like I'm either losing some information or this library is more efficient than the original product used to create the document.

The other interesting thing is that when reading about this library I kept seeing links to iText in Action which is published by the same publisher of the eBooks that I am having problems with:-)

        using System.Diagnostics;
        using iTextSharp.text.pdf;
        using System.IO;
        using System.Collections;

        PdfReader pdfReader = new PdfReader(filePath);
        using (FileStream fileStream = new FileStream(newFilePath, FileMode.Create, FileAccess.Write))
        {
            string title = pdfReader.Info["Title"] as string;
            Trace.WriteLine("Existing title: " + title);

            PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);

            // The info property returns a copy of the internal HashTable
            Hashtable newInfo = pdfReader.Info;

            newInfo["Title"] = "New title";

            pdfStamper.MoreInfo = newInfo;

            pdfReader.Close();
            pdfStamper.Close();
        }
Martin Hollingsworth