如何阅读自定义pdf属性 [英] How to read custom pdf properties

查看:130
本文介绍了如何阅读自定义pdf属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要使用itextSharp读取PDF文件的自定义属性。我能够阅读以下属性: -

作者,标题,创作者,创作日期等,但无法阅读自定义标签属性。



我尝试过:



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:

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());
}



使用上面的代码我能够读取常规pdf属性,但不能读取自​​定义选项卡中的属性


Using the above code I am able to read regular pdf properties, but not the properties from the custom tab

推荐答案

使用字典< string,string>像这样:



Use Dictionary<string, string> like so:

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;
}





现在只需按名称(Key)查找您想要的房产,如下所示:





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

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.


这篇关于如何阅读自定义pdf属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆