【问题标题】:Get all metadata from an existing PDF using iText7使用 iText7 从现有 PDF 中获取所有元数据
【发布时间】:2018-03-28 08:12:06
【问题描述】:

如何使用 iText7 检索存储在 PDF 中的所有元数据?

using (var pdfReader = new iText.Kernel.Pdf.PdfReader("path-to-a-pdf-file"))
{
    var pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfReader);
    var pdfDocumentInfo = pdfDocument.GetDocumentInfo();

    // Getting basic metadata
    var author = pdfDocumentInfo.GetAuthor();
    var title = pdfDocumentInfo.GetTitle();

    // Getting everything else
    var someMetadata = pdfDocumentInfo.GetMoreInfo("need-a-key-here");
    // How to get all metadata ?
}

我将它与 iTextSharp 一起使用,但我不知道如何使用新的 iText7。

using (var pdfReader = new iTextSharp.text.pdf.PdfReader("path-to-a-pdf-file"))
{
    // Getting basic metadata
    var author = pdfReader.Info.ContainsKey("Author") ? pdfReader.Info["Author"] : null;
    var title = pdfReader.Info.ContainsKey("Title") ? pdfReader.Info["Title"] : null;

    // Getting everything else
    var metadata = pdfReader.Info;
    metadata.Remove("Author");
    metadata.Remove("Title");

    // Print metadata
    Console.WriteLine($"Author: {author}");
    Console.WriteLine($"Title: {title}");

    foreach (var line in metadata)
    {
        Console.WriteLine($"{line.Key}: {line.Value}");
    }
}

我使用的是 iText7 的 7.1.1 版。

【问题讨论】:

    标签: c# itext metadata itext7


    【解决方案1】:

    不幸的是,在 iText 7 中,PdfDocumentInfo 类没有公开检索底层字典中的键的方法。

    但是您可以通过立即从预告片字典中访问该字典来简单地检索 Info 字典的内容。例如。对于PdfDocument pdfDocument

    PdfDictionary infoDictionary = pdfDocument.GetTrailer().GetAsDictionary(PdfName.Info);
    foreach (PdfName key in infoDictionary.KeySet())
        Console.WriteLine($"{key}: {infoDictionary.GetAsString(key)}");
    

    【讨论】:

      【解决方案2】:

      "UnicodeBig""UTF-8""PDF" 编码字符串存在问题。
      例如,如果 PDF 是使用 Microsoft Word 创建的,则 "/Creator" 是不可读的编码,需要转换:
      .
      iText7 有自己的转换功能: ...ToUnicodeString().
      但它是 PdfString 对象的方法,必须首先将 PdfDictionary 值 (PdfObject) 强制转换为 PdfString 类型。
      完整的解决方案async,“牢不可破”和自动处置功能:

      public static async Task<(Dictionary<string, string> MetaInfo, string Error)> GetMetaInfoAsync(string path)
      {
          try
          {
              var metaInfo = await Task.Run(() =>
              {
                  var metaInfoDict = new Dictionary<string, string>();
                  using (var pdfReader = new PdfReader(path))
                  using (var pdfDocument = new PdfDocument(pdfReader))
                  {
                      metaInfoDict["PDF.PageCount"] = $"{pdfDocument.GetNumberOfPages():D}";
                      metaInfoDict["PDF.Version"] = $"{pdfDocument.GetPdfVersion()}";
      
                      var pdfTrailer = pdfDocument.GetTrailer();
                      var pdfDictInfo = pdfTrailer.GetAsDictionary(PdfName.Info);
                      foreach (var pdfEntryPair in pdfDictInfo.EntrySet())
                      {
                          var key = "PDF." + pdfEntryPair.Key.ToString().Substring(1);
                          string value;
                          switch (pdfEntryPair.Value)
                          {
                              case PdfString pdfString:
                                  value = pdfString.ToUnicodeString();
                                  break;
                              default:
                                  value = pdfEntryPair.Value.ToString();
                                  break;
                          }
                          metaInfoDict[key] = value;
                      }
                      return metaInfoDict;
                  }
              });
              return (metaInfo, null);
          }
          catch (Exception ex)
          {
              if (Debugger.IsAttached) Debugger.Break();
              return (null, ex.Message);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-25
        • 2020-05-05
        • 1970-01-01
        • 2018-06-23
        相关资源
        最近更新 更多