Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a requirement to read the custom properties of the PDF file using itextSharp. I am able to read the below properties:-
Author, Title, Creator, Creation Date etc., but not able to read custom tab properties.

What I have tried:

C#
StringBuilder text = new StringBuilder();
            try
            {
                using (PdfReader reader = new PdfReader(path))
                {
                    //for (int i = 1; i <= reader.NumberOfPages; i++)
                    //{
                    //    text.Append(PdfTextExtractor.GetTextFromPage(reader, i));   
                    //}

                    text.Append("<br />");
                    text.Append("Author: " + reader.Info["Author"]);
                    text.Append("<br />");
                    text.Append("Title: " + reader.Info["Title"]);
                    text.Append("<br />");
                    text.Append("Creator: " + reader.Info["Creator"]);
                    text.Append("<br />");
                    text.Append("Creation Date: " + reader.Info["CreationDate"]);
                    text.Append("<br />");
                    text.Append("Modified: " + reader.Info["ModDate"]);
                    text.Append("<br />");
                    text.Append("Producer: " + reader.Info["Producer"]);
                    text.Append("<br />");
                    text.Append("PDF Version " + reader.PdfVersion.ToString());
}

Using the above code I am able to read regular pdf properties, but not the properties from the custom tab
Posted
Updated 9-Aug-16 10:44am
v2

1 solution

Use Dictionary<string, string> like so:

C#
static string GetPropertyByName(string filePath, string propertyName)
{
   if (String.IsNullOrEmpty(filePath) || !System.IO.File.Exists(filePath) || String.IsNullOrEmpty(propertyName)) { return null; }
   Dictionary<string, string> propertyInfo = GetPdfProperties(filePath);
   foreach (KeyValuePair<string, string> property in propertyInfo)
   {
      if (property.Key == propertyName) { return property.Value; }
   }
   return null; ;
}

static Dictionary<string, string> GetPdfProperties(string filePath)
{
   Dictionary<string, string> propertyInfo = null;

   using (PdfReader reader = new PdfReader(filePath))
   {
      propertyInfo = reader.Info;
      reader.Close();
   }

   return propertyInfo;
}


Now just look for the property you want by name (Key), like so:

C#
string pdfFilePath = @"C:\....\Some File.pdf";
string propertyName = "CustomProperty1";
string propertyValue = GetPropertyByName(pdfFilePath, propertyName)
if (propertyValue == null) { Console.WriteLine("Property "+ propertyName + " was not found."); }
else { Console.WriteLine(propertyName + " = " + propertyValue); }


Note: Above code has been tested.
 
Share this answer
 
v4
Comments
Member 12676479 11-Aug-16 1:45am    
I am not able to find the properties defined in the custom tab. Can you help me in finding pdf properties from the CUSTOM tab
NathanRO 11-Aug-16 7:55am    
That should list all properties, to include the custom tab as well. As long as you know the name of the property you are looking for you can extract the data by checking the key, then reading the value (string). If you don't know the name of the custom property, then you can just list all of them in the Dictionary.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900