Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying PDF document properties [closed]

Tags:

.net

pdf

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.

like image 399
Martin Hollingsworth Avatar asked Nov 26 '08 21:11

Martin Hollingsworth


People also ask

Why can't I edit PDF properties?

These issues can occur when you open a PDF file in Adobe Reader instead of Adobe Acrobat, which is used for editing PDF file. Note: If the PDF file is password protected, you need to have the document open password and the change permissions password to edit the PDF/PDF Portfolio.

How do you change document properties in PDF?

Choose File > Properties, and then select Custom. To add a property, type the name and value, and then click Add. To change the properties, do any of the following, and then click OK: To edit a property, select it, change the Value, and then click Change.

How do I remove properties from a PDF document?

To edit a property, select it, change the Value, and then click Change. To delete a property, select it and click Delete.


2 Answers

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();
        }
like image 140
Martin Hollingsworth Avatar answered Oct 21 '22 09:10

Martin Hollingsworth


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

like image 22
Mindaugas Mozūras Avatar answered Oct 21 '22 11:10

Mindaugas Mozūras