在 Linux 上加载 PDF 表单卡住的进程
This article was translated from English: Does it need improvement?
Translated
View the article in English
在Linux上(例如,使用.NET 8的Debian系统),OcrInput.LoadPdf()在加载包含交互式表单字段的某些PDF文件时,可能会无限期挂起。 调用从未返回且进程停止。
Process hangs at OcrInput.LoadPdf() and never proceeds.
某些 PDF 的交互式表单层是触发器。 相同文件在 Windows 上正确加载,其他表单 PDF 可能在 Linux 上加载良好,这使问题不一致,难以在生产中出现前捕获。
解决方案
在传递给 IronOCR 之前 将 PDF 平面化。 平面化将每个表单字段转换为静态页面内容,移除了导致挂起的交互层。
1. 使用 IronPDF 平面化 PDF
加载源文件,将其平面化并保存静态副本:
using IronPdf;
var pdf = PdfDocument.FromFile("ocrpdfform.pdf");
pdf.Flatten();
pdf.SaveAs("flattened_ocrpdfform.pdf");
using IronPdf;
var pdf = PdfDocument.FromFile("ocrpdfform.pdf");
pdf.Flatten();
pdf.SaveAs("flattened_ocrpdfform.pdf");
Imports IronPdf
Dim pdf = PdfDocument.FromFile("ocrpdfform.pdf")
pdf.Flatten()
pdf.SaveAs("flattened_ocrpdfform.pdf")
$vbLabelText
$csharpLabel
Flatten() 会去掉交互式表单字段,留下一个简单的文档,IronOCR可以打开而不会卡住。
2. 对平面化的文件运行 OCR
将LoadPdf()指向展平的副本,然后像往常一样读取:
using var ocrInput = new OcrInput();
ocrInput.LoadPdf("flattened_ocrpdfform.pdf"); // No longer stuck
var ocr = new IronTesseract();
var result = ocr.Read(ocrInput);
Console.WriteLine(result.Text);
using var ocrInput = new OcrInput();
ocrInput.LoadPdf("flattened_ocrpdfform.pdf"); // No longer stuck
var ocr = new IronTesseract();
var result = ocr.Read(ocrInput);
Console.WriteLine(result.Text);
Imports IronTesseract
Using ocrInput As New OcrInput()
ocrInput.LoadPdf("flattened_ocrpdfform.pdf") ' No longer stuck
Dim ocr As New IronTesseract()
Dim result = ocr.Read(ocrInput)
Console.WriteLine(result.Text)
End Using
$vbLabelText
$csharpLabel
除了防止挂起,平面化还提供跨平台一致行为,去除 OCR 不需要的交互元素,并减少文件复杂性以加速处理。
准备开始了吗?
Nuget 下载 6,136,090 | 版本: 2026.7 刚刚发布

