C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# iText获取PDF的trailer数据

C#使用iText获取PDF的trailer数据的代码示例

作者:Humbunklung

开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了,所以本文将详细介绍C#使用iText获取PDF的trailer数据的方法步骤,需要的朋友可以参考下

引言

开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了。

实际上,使用 iText 获取 PDF 的 trailer 数据是一个稍微底层一些的操作,但完全可以实现。trailer 是 PDF 文件结构的核心部分,它告诉解析器如何找到文件的关键部分,比如交叉引用表 (xref)、文档信息字典 (/Info) 和文档根对象 (/Root)。

在 iText 中,这个操作被很好地封装了。本文将详细说明能从 trailer 中获得什么信息。

iText 核心概念

C# 代码示例

这个示例将演示如何打开一个 PDF 文件,并同时使用高级方法和底层方法来检查 trailer 相关的数据。

步骤 1: 确保已安装 iText

请在你的项目中通过 NuGet 包管理器安装 itext

Install-Package itext

步骤 2: C# 代码

using System;
using System.IO;
using iText.Kernel.Pdf;

public class PdfTrailerInspector
{
    public static void InspectPdfTrailer(string filePath)
    {
        if (!File.Exists(filePath))
        {
            Console.WriteLine($"错误:文件不存在 '{filePath}'");
            return;
        }

        try
        {
            // 使用 PdfReader 和 PdfDocument 打开 PDF 文件
            using (var pdfReader = new PdfReader(filePath))
            using (var pdfDocument = new PdfDocument(pdfReader))
            {
                Console.WriteLine($"--- 正在分析文件: {Path.GetFileName(filePath)} ---");

                // --- 方法 1: 使用高级 API 访问 Trailer 指向的内容 (推荐的常规做法) ---
                Console.WriteLine("\n=== 通过高级 API 获取 Trailer 指向的信息 ===");
                
                // GetDocumentInfo() 会读取 trailer 的 /Info 字典
                PdfDocumentInfo docInfo = pdfDocument.GetDocumentInfo();
                Console.WriteLine($"信息字典 (来自 /Info): Creator = {docInfo.GetCreator()}, Producer = {docInfo.GetProducer()}");
                
                // GetCatalog() 会读取 trailer 的 /Root 字典,这是文档的入口点
                PdfCatalog catalog = pdfDocument.GetCatalog();
                Console.WriteLine($"文档目录 (来自 /Root): 页面模式 = {catalog.GetPageMode()}, 页面布局 = {catalog.GetPageLayout()}");

                // --- 方法 2: 直接访问和遍历 Trailer 字典本身 (底层操作) ---
                Console.WriteLine("\n=== 直接访问 Trailer 字典的原始键值对 ===");
                
                // 使用 GetTrailer() 直接获取 Trailer 字典对象
                PdfDictionary trailer = pdfDocument.GetTrailer();

                if (trailer != null)
                {
                    // 遍历 Trailer 字典中的所有条目
                    foreach (var key in trailer.KeySet())
                    {
                        PdfObject value = trailer.Get(key); // 值 (可能是数字、引用等)

                        Console.WriteLine($"键: {key}, 值: {value}, 值的类型: {value.GetType().Name}");
                    }

                    // 你也可以直接获取特定的键
                    Console.WriteLine("\n--- 单独获取 Trailer 中的关键值 ---");
                    PdfObject size = trailer.Get(PdfName.Size);
                    PdfObject root = trailer.Get(PdfName.Root);
                    PdfObject info = trailer.Get(PdfName.Info);
                    PdfObject id = trailer.Get(PdfName.ID);

                    Console.WriteLine($"大小 (Size): {size}");
                    Console.WriteLine($"根对象引用 (Root): {root}");
                    Console.WriteLine($"信息字典引用 (Info): {info}");
                    Console.WriteLine($"文件ID (ID): {id}");
                }
                else
                {
                    Console.WriteLine("无法获取 Trailer 字典。");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"读取 PDF 时发生错误: {ex.Message}");
        }
    }

    public static void Main(string[] args)
    {
        // 请将 "C:\\path\\to\\your\\document.pdf" 替换为你的 PDF 文件路径
        string pdfPath = "C:\\path\\to\\your\\document.pdf"; 
        InspectPdfTrailer(pdfPath);
    }
}

程序运行效果

解读 Trailer 的输出

当你运行上面的代码并查看“直接访问 Trailer 字典”部分的输出时,你会看到类似下面的内容:

键: /Size, 值: 25, 值的类型: PdfNumber
键: /Root, 值: 23 0 R, 值的类型: PdfIndirectReference
键: /Info, 值: 1 0 R, 值的类型: PdfIndirectReference
键: /ID, 值: [<0DDB5968...>, <F3C3B2A6...>], 值的类型: PdfArray

这里是对这些关键条目的解释:

总结

  1. 常规需求: 如果我们只是想获取作者、标题、页面内容等信息,使用 iText 的高级 API(GetDocumentInfo(), GetCatalog(), GetPage() 等)就足够了,它们在后台为你处理了 trailer 的解析。
  2. 底层分析: 如果你需要检查 trailer 的所有原始条目,或者查找可能存在的非标准字段,或者想验证 PDF 结构,那么使用 pdfDocument.GetTrailer() 是最直接和强大的方法。

上面的代码提供了两种,我们可以根据具体需求选择使用。

到此这篇关于C#使用iText获取PDF的trailer数据的代码示例的文章就介绍到这了,更多相关C# iText获取PDF的trailer数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文