排除 IronPDF 中的 PDF 解析错误
This article was translated from English: Does it need improvement?
TranslatedView the article in English
尝试使用 IronPDF 解析 PDF 文档时,您可能会遇到以下异常:
Unhandled exception. System.IO.IOException: PDF can not be opened as a PDF. May be an incorrect file type or a password is required.
Please check file for corruption or validity by a 3rd party at: https://www.pdf-online.com/osa/validate.aspx加载 PDF 文档到 PdfDocument 对象时,通常会使用以下代码出现此错误:
// Attempt to load a PDF document from a file
var pdf = PdfDocument.FromFile("Test.pdf");// Attempt to load a PDF document from a file
var pdf = PdfDocument.FromFile("Test.pdf");' Attempt to load a PDF document from a file
Dim pdf = PdfDocument.FromFile("Test.pdf")$vbLabelText $csharpLabel
可能原因
- 密码保护的 PDF
- 所有者密码:此异常最常见的原因是 PDF 被 所有者密码保护。 当 PDF 拥有所有者密码时,它会限制某些操作,如编辑或复制。 要加载此类文档,您必须提供密码。
- 损坏的 PDF
- PDF 文件可能已损坏或不是有效的 PDF 格式。 损坏的文件可能导致 IronPDF 在解析过程中失败。
解决方案
- 处理密码保护的 PDF
如果您知道 PDF 的密码,可以将它传递给 PdfDocument.FromFile() 方法:
// Load a password-protected PDF by specifying the owner password
var pdf = PdfDocument.FromFile("test.pdf", "ownerpassword");// Load a password-protected PDF by specifying the owner password
var pdf = PdfDocument.FromFile("test.pdf", "ownerpassword");' Load a password-protected PDF by specifying the owner password
Dim pdf = PdfDocument.FromFile("test.pdf", "ownerpassword")$vbLabelText $csharpLabel
您还可以在 Adobe Acrobat 中通过检查文件属性来验证文件是否受密码保护。 有关标识 PDF 安全状态的更多信息,请参阅此 文章。
- 处理损坏的 PDF
要检查 PDF 文件是否损坏,请使用第三方 验证工具。 如果文件损坏,IronPDF 将无法解析它。
如果这些故障排除步骤不能解决问题,请提交 工程支持请求,以获取我们的支持工程师的进一步帮助。
准备开始了吗?
Nuget 下载 16,685,821 | 版本: 2025.12 刚刚发布






