使用iText7从现有PDF获取所有元数据

时间:2018-03-28 08:12:06

标签: c# itext metadata itext7

如何使用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版。

2 个答案:

答案 0 :(得分:2)

在iText 7中,PdfDocumentInfo类遗憾地没有公开检索基础字典中的键的方法。

但您可以通过立即从预告片词典中访问该词典来检索信息词典内容。例如。对于PdfDocument pdfDocument

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

答案 1 :(得分:0)

"UnicodeBig""UTF-8""PDF"编码的字符串存在问题。
例如,如果使用Microsoft Word创建PDF,则"/Creator"的编码不可读,需要转换:
enter image description here
iText7具有自己的转换功能: ...ToUnicodeString()
但这是PdfString对象的Method,并且必须首先将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);
    }
}