在 IronOCR 中縮小 PDF 檔案大小
This article was translated from English: Does it need improvement?
Translated
View the article in English
如何在 IronOCR 中縮小輸出 PDF 的檔案大小?
IronOCR 會自動將偵測到品質較低(低於 150 DPI)的輸入資料進行放大處理,以確保讀取結果的準確性。
若偵測到 DPI 低於 150,TargetDPI(預設值為 225 DPI)將定義 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 參考文件
準備開始了嗎?
Nuget 下載 5,896,332 | 版本: 2026.5 just released

