Shree
Shree

Reputation: 41

Read/Modify PDF Metadata using iTextSharp without showing any data to user on pdf properties

I am trying to use iTextSharp to read/modify PDF metadata. Without showing any information to the user. I gone through below code:

iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 20f, 20f, 20f, 20f);
using (MemoryStream memStream = new MemoryStream())
{
    using (PdfWriter wri = PdfWriter.GetInstance(document, memStream))
    {
        document.Open(); document.AddSubject("Test"); document.Close();
    }
}

I would greatly appreciate any pointers to the solution.

Upvotes: 3

Views: 8864

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

The code snippet you are using is about adding metadata to a PDF document that is created from scratch. You are asking to read metadata from an existing PDF document. Please take a look at the documentation on the iText website, more specifically to Q&A entry How to add / delete / retrieve information from a PDF using a custom property?

Granted, the code used in the answer to that question is written in Java, but it's fairly easy to port it to C#. Here's another snippet:

public byte[] ManipulatePdf(byte[] src) {
    PdfReader reader = new PdfReader(src);
    using (MemoryStream ms = new MemoryStream()) {
        using (PdfStamper stamper = new PdfStamper(reader, ms)) {
            Dictionary<String, String> info = reader.Info;
            info["Title"] = "Hello World stamped";
            info["Subject"] = "Hello World with changed metadata";
            info["Keywords"] = "iText in Action, PdfStamper";
            info["Creator"] = "Silly standalone example";
            info["Author"] = "Also Bruno Lowagie";
            stamper.MoreInfo = info;
        }
        return ms.ToArray();
    }
}

You can retrieve the metadata from an existing PDF through PdfReader. You get a Dictionary where the keys correspond with the keys of the info dictionary in ISO-32000-1. If you want to change the metadata, you can do so with PdfStamper.

This functionality will read and adapt the Info dictionary (which is what you referred to in your code snippet). The PDF can also contains XMP metadata.

XMP metadata can be read like this:

public string ReadXmpMetadata(byte[] src) {
    PdfReader reader = new PdfReader(src);
    byte[] b = reader.Metadata;
    return Encoding.UTF8.GetString(b, 0, b.Length);
}  

You can change the XMP metadata like this:

public byte[] ManipulatePdf(byte[] src) {
    PdfReader reader = new PdfReader(src);
    using (MemoryStream ms = new MemoryStream()) {
        using (PdfStamper stamper = new PdfStamper(reader, ms)) {
            Dictionary<String, String> info = reader.Info;
            using (MemoryStream msXmp = new MemoryStream()) {
                XmpWriter xmp = new XmpWriter(msXmp, info);
                xmp.Close();
                stamper.XmpMetadata = msXmp.ToArray();      
            }
        }
        return ms.ToArray();
    }
}

I am assuming that you know the difference between the Info dictionary and an XMP metadata stream in PDF.

Upvotes: 3

Related Questions