使用C#代码检查 PDF 是否受密码保护并确认正确的密码

PDF 的密码保护是一种常见的安全机制,用于限制访问并防止未经授权的修改。在处理 PDF 文件之前,确认其是否设置了密码保护非常重要。如果文档受保护,只有验证正确的密码后,才能成功解锁文件,从而顺利进行查看、编辑或内容提取等操作。

本文将介绍如何使用 C# 以及 Spire.PDF for .NET 库来判断 PDF 是否启用了密码保护,并演示验证正确密码的具体方法。

安装 Spire.PDF for .NET

首先,需要将 Spire.PDF for .NET 安装并引入到你的 .NET 项目中。你可以选择以下任一方式添加所需的 DLL 文件作为项目引用:

  • 从官方下载链接获取 DLL 文件,并手动添加到项目中;

  • 通过 NuGet 安装,这是更推荐、也更便捷的方式。

完成安装后,即可在项目中使用 Spire.PDF for .NET 提供的相关功能。

cs 复制代码
PM> Install-Package Spire.PDF

使用 C# 检查 PDF 是否受密码保护

Spire.PDF for .NET 提供了 PdfDocument.IsPasswordProtected(string fileName) 方法,可用于判断指定的 PDF 文件是否启用了密码保护。

具体示例代码如下:

cs 复制代码
using Spire.Pdf;
using System.IO;

namespace CheckIfPdfIsProtected
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 指定输入和输出文件路径
            string pdfPath = "sample.pdf";
            string resultFilePath = "verification_results.txt";

            // 检查 PDF 文件是否受密码保护
            bool isProtected = PdfDocument.IsPasswordProtected(pdfPath);

            // 创建 StreamWriter,将结果写入文本文件
            using (StreamWriter writer = new StreamWriter(resultFilePath))
            {
                // 将验证结果写入文本文件
                string resultMessage = isProtected ? "该 PDF 文件已设置密码保护。" : "该 PDF 文件未设置密码保护。";
                writer.WriteLine(resultMessage);
            }
        }
    }
}

使用 C# 确认 PDF 的正确密码

Spire.PDF for .NET 并未提供直接用于验证密码是否正确的方法,但可以通过尝试使用指定密码打开 PDF 文件来实现这一目的。如果密码不正确,程序将会抛出异常。

具体示例代码如下:

cs 复制代码
using Spire.Pdf;
using System;
using System.IO;

namespace DetermineTheCorrectPasswordOfPdf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 指定输入和输出文件路径
            string pdfPath = "sample.pdf";
            string resultFilePath = "verification_results.txt";

            // 检查 PDF 文件是否受密码保护
            bool isProtected = PdfDocument.IsPasswordProtected(pdfPath);

            // 创建一个用于测试的候选密码数组
            string[] passwords = new string[5] { "password1", "password2", "password3", "admin123", "test" };

            // 创建 StreamWriter,将结果写入文本文件
            using (StreamWriter writer = new StreamWriter(resultFilePath))
            {
                // 如果 PDF 受密码保护,则开始测试密码
                if (isProtected)
                {
                    // 遍历候选密码数组
                    for (int passwordcount = 0; passwordcount < passwords.Length; passwordcount++)
                    {
                        try
                        {
                            // 创建 PdfDocument 对象,并尝试使用当前密码加载 PDF 文档
                            PdfDocument doc = new PdfDocument();
                            doc.LoadFromFile(pdfPath, passwords[passwordcount]);

                            // 如果加载成功,说明密码正确
                            writer.WriteLine("密码 " + passwords[passwordcount] + " 正确");
                        }
                        catch
                        {
                            // 如果抛出异常,说明密码不正确
                            writer.WriteLine("密码 " + passwords[passwordcount] + " 不正确");
                        }
                    }
                }
                else
                {
                    // 如果 PDF 未设置密码保护,则在结果中说明
                    writer.WriteLine("该 PDF 文件未设置密码保护。");
                }
            }

            Console.WriteLine("验证结果已保存至:" + resultFilePath);
            Console.ReadKey();
        }
    }
}

获取免费许可证

如需在不受评估限制的情况下完整体验 Spire.PDF for .NET 的全部功能,你可以申请一份有效期为 30 天的免费试用许可证

相关推荐
张彦峰ZYF4 小时前
从“全量 OCR”到按页智能路由:pdf-inspector 与企业 PDF 解析架构的工程化重构
人工智能·pdf·ocr·ai agent·pdf-inspector
SamChan906 小时前
Python批量处理PDF踩坑实录:中文编码、字体内嵌、多栏排版与内存占用
python·单片机·ai·pdf·机器翻译
一念春风7 小时前
PDF浏览器(WPF C# )
pdf
麻花地1 天前
MinerU 文档解析全指南:从 magic-pdf 到 3.4.5,PDF→Markdown 开源利器深度实战
pdf·开源
2601_967760781 天前
PDF 转图片再转回总翻车?2026 国内免费实测
pdf
zhlx28351 天前
小初高教材 PDF 批量获取|中小学电子课本下载器,支持批量下载带书签 PDF 教材[Windows]
pdf
张某布响丸辣1 天前
Node 16 下用 pdfjs + @napi-rs/canvas 把中文 PDF 渲染成图片
pdf·node·字体映射
weixin_493503672 天前
Vue3 前端生成 PDF:会员证书与活动签到表的三种打印方案与踩坑记录
前端·pdf·状态模式
、如果2 天前
PDF图片文字提取零依赖方案:pymupdf+AI视觉实战
人工智能·数据分析·pdf·图片提取·文字提取·skills
SamChan902 天前
PDF翻译后的格式完整性校验:用Python自动比对译文与原文档的表格与段落结构
开发语言·python·ai·pdf·机器翻译