解決IronPDF中PDF解析錯誤
This article was translated from English: Does it need improvement?
Translated
View 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 下載 20,296,129 | 版本: 2026.7 剛剛發布

