在 IronOCR 中减少 PDF 文件大小

This article was translated from English: Does it need improvement?
Translated
View the article in English

如何减小 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.AddPdf("example.pdf", "password"); // Add 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.AddPdf("img/Input.pdf", 72); // Add PDF with the specified DPI of 72
    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
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.AddPdf("example.pdf", "password"); // Add 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.AddPdf("img/Input.pdf", 72); // Add PDF with the specified DPI of 72
    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

Private 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.AddPdf("example.pdf", "password") ' Add 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 As New OcrInput() ' Create a new OCR input object
	ocrInput.AddPdf("img/Input.pdf", 72) ' Add PDF with the specified DPI of 72
	Dim ocrResult = ocr.Read(ocrInput) ' Read and process the PDF

	ocrResult.SaveAsSearchablePdf("Output.pdf") ' Save result to a searchable PDF
End Using
$vbLabelText   $csharpLabel

要禁用自动放大,请使用TargetDPI = 0 。 这将使 IronOCR 按原样读取输入文件,忽略 TargetDPI 值。

更多信息请参阅 API: IronOCR API 参考

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 5,167,857 | Version: 2025.11 刚刚发布