在 IronOCR 中减少 PDF 文件大小
Curtis Chau
更新: 2026年7月6日
如何减小 IronOCR 输出 PDF 的文件大小?
IronOCR 会自动将检测到的低质量(低于 150DPI)的输入图像放大,以确保准确的读取结果。
如果检测到 DPI 低于 150,则 TargetDPI(默认 225DPI)定义 PDF 渲染的 DPI。 这与手动设置TargetDPI = 225相同。
为了减少输出文件的大小,您可以设置更低的TargetDPI,这将创建更小的PDF。 但是,设置得太低可能会影响 OCR 性能,因此保持平衡至关重要。
建议值分别为 96、72、48。
// Example of reducing PDF output file size by lowering the DPI
// Example 1: Reducing DPI to 96
using IronOcr; // Import IronOCR namespace
var Ocr = new IronTesseract(); // Initialize IronTesseract for OCR operations
using (var Input = new OcrInput()) // Create OCR input object
{
Input.TargetDPI = 96; // Set the desired DPI; 96 is used for smaller output size
Input.LoadPdf("example.pdf", Password: "password"); // Load input PDF (with optional password)
var Result = Ocr.Read(Input); // Perform OCR on the input
Console.WriteLine(Result.Text); // Output recognized text to the console
}
// Example 2: Another way to set DPI
var ocr = new IronTesseract();
using (var ocrInput = new OcrInput()) // Create a new OCR input object
{
ocrInput.TargetDPI = 72; // Set render DPI to 72
ocrInput.LoadPdf("img/Input.pdf"); // Load the PDF
var ocrResult = ocr.Read(ocrInput); // Read and process the PDF
ocrResult.SaveAsSearchablePdf(@"Output.pdf"); // Save result to a searchable PDF
}' Example of reducing PDF output file size by lowering the DPI
' Example 1: Reducing DPI to 96
Imports IronOcr ' Import IronOCR namespace
Dim Ocr = New IronTesseract() ' Initialize IronTesseract for OCR operations
Using Input = New OcrInput() ' Create OCR input object
Input.TargetDPI = 96 ' Set the desired DPI; 96 is used for smaller output size
Input.LoadPdf("example.pdf", Password:="password") ' Load input PDF (with optional password)
Dim Result = Ocr.Read(Input) ' Perform OCR on the input
Console.WriteLine(Result.Text) ' Output recognized text to the console
End Using
' Example 2: Another way to set DPI
Dim ocr = New IronTesseract()
Using ocrInput = New OcrInput() ' Create a new OCR input object
ocrInput.TargetDPI = 72 ' Set render DPI to 72
ocrInput.LoadPdf("img/Input.pdf") ' Load the PDF
Dim ocrResult = ocr.Read(ocrInput) ' Read and process the PDF
ocrResult.SaveAsSearchablePdf("Output.pdf") ' Save result to a searchable PDF
End Using' Example of reducing PDF output file size by lowering the DPI
' Example 1: Reducing DPI to 96
Imports IronOcr ' Import IronOCR namespace
Dim Ocr = New IronTesseract() ' Initialize IronTesseract for OCR operations
Using Input = New OcrInput() ' Create OCR input object
Input.TargetDPI = 96 ' Set the desired DPI; 96 is used for smaller output size
Input.LoadPdf("example.pdf", Password:="password") ' Load input PDF (with optional password)
Dim Result = Ocr.Read(Input) ' Perform OCR on the input
Console.WriteLine(Result.Text) ' Output recognized text to the console
End Using
' Example 2: Another way to set DPI
Dim ocr = New IronTesseract()
Using ocrInput = New OcrInput() ' Create a new OCR input object
ocrInput.TargetDPI = 72 ' Set render DPI to 72
ocrInput.LoadPdf("img/Input.pdf") ' Load the PDF
Dim ocrResult = ocr.Read(ocrInput) ' Read and process the PDF
ocrResult.SaveAsSearchablePdf("Output.pdf") ' Save result to a searchable PDF
End Using要禁用自动放大功能,请使用TargetDPI = 0。 这将使 IronOCR 按原样读取输入文件,忽略 TargetDPI 值。
有关更多信息,请参见API:IronOCR API 参考
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。