如何在 C# 中将结果保存为可搜索的 PDF

使用 IronOCR 在 C# 中保存可搜索 PDF

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

IronOCR 使 C# 开发人员能够使用 OCR 技术将扫描的文档和图像转换为可搜索的 PDF,只需几行代码即可支持以文件、字节或流的形式输出。

可搜索的 PDF,通常被称为 OCR(光学字符识别)PDF,是一种包含扫描图像和机器可读文本的 PDF 文档。 这些 PDF 文件是通过对扫描的纸质文档或图像执行 OCR 功能创建的,它可以识别图像中的文本,并将其转换为可选择和可搜索的文本。

SaveAsSearchablePdf 还可在 ReadScreenShotReadDocumentAdvanced 的结果中使用,支持通过照片和高级文档 OCR 工作流创建可搜索的 PDF。 在将纸质档案数字化或使旧版 PDF 文件可搜索以实现更佳文档管理时,此功能尤为实用。

快速入门:一行导出可搜索 PDF

设置 RenderSearchablePdf = true,在您的输入上运行 Read(...),并调用 SaveAsSearchablePdf(...)。 只需这些步骤,即可使用 IronOCR 生成一份完全可搜索的 PDF 文件。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronOcr

    PM > Install-Package IronOcr
  2. 复制并运行这段代码。

    new IronOcr.IronTesseract { Configuration = { RenderSearchablePdf = true } } .Read(new IronOcr.OcrImageInput("file.jpg")).SaveAsSearchablePdf("searchable.pdf");
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronOCR

    arrow pointer


如何将 OCR 结果导出为可搜索的 PDF?

若要使用 IronOCR 将结果导出为可搜索的 PDF,请将 Configuration.RenderSearchablePdf 属性设置为 true,通过 Read 方法获取 OCR 结果对象,并调用 SaveAsSearchablePdf 并传入输出文件路径。

输入

《哈利·波特》小说中的一页,已扫描为 TIFF 文件并通过 OcrImageInput 加载。 该页面包含密集的印刷文本,是用于测试可搜索 PDF 文本层的理想测试素材。

《哈利·波特》书中展示第八章

potter.tiff:作为OCR输入的扫描小说页面,用于生成带有不可见文本图层的可搜索PDF文件。

:path=/static-assets/ocr/content-code-examples/how-to/searchable-pdf-searchable-pdf.cs
using IronOcr;

// Create the OCR engine: defaults to English with balanced speed and accuracy
IronTesseract ocrTesseract = new IronTesseract();

// Required: without this flag the text overlay layer is not built, and SaveAsSearchablePdf produces a plain image PDF
ocrTesseract.Configuration.RenderSearchablePdf = true;

// Wrap the TIFF in OcrImageInput: handles DPI detection and page layout automatically
using var imageInput = new OcrImageInput("Potter.tiff");
// Run OCR; returns a result containing the recognized text and spatial layout data
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Write the output: the original scanned image is preserved with an invisible text layer on top
ocrResult.SaveAsSearchablePdf("searchablePdf.pdf");
Imports IronOcr

' Create the OCR engine: defaults to English with balanced speed and accuracy
Dim ocrTesseract As New IronTesseract()

' Required: without this flag the text overlay layer is not built, and SaveAsSearchablePdf produces a plain image PDF
ocrTesseract.Configuration.RenderSearchablePdf = True

' Wrap the TIFF in OcrImageInput: handles DPI detection and page layout automatically
Using imageInput As New OcrImageInput("Potter.tiff")
    ' Run OCR; returns a result containing the recognized text and spatial layout data
    Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

    ' Write the output: the original scanned image is preserved with an invisible text layer on top
    ocrResult.SaveAsSearchablePdf("searchablePdf.pdf")
End Using
$vbLabelText   $csharpLabel

输出

searchablePdf.pdf:可搜索的 PDF 输出文件。请选中或搜索任意单词,以验证 OCR 文本层。

生成的 PDF 文件将原始扫描的页面图像嵌入其中,并在每个识别出的单词上方叠加了一层不可见的文本层。 在查看器中选择或搜索任意WORD,以确认文本图层是否存在。

IronOCR 在叠加层中使用了特定字体,这可能会导致渲染后的文本大小与原文存在轻微差异。

在处理多页 TIFF 文件或复杂文档时,IronOCR 会自动处理所有页面并将它们包含在输出中。 该库可自动处理页面排序和文本叠加定位,确保文本到图像的映射准确无误。

如何从照片或高级文档扫描创建可搜索的 PDF?

使用 ReadScreenShotReadDocumentAdvanced 时,还支持导出可搜索的 PDF 文件。 这些方法各自返回的结果类型均支持 SaveAsSearchablePdf

调用这些方法时,可选地传入 ModelType。 默认选项为 Normal,而 Enhanced 虽以牺牲速度为代价,但能提供更高的准确性。

输入

一张带有手绘文字的墙面壁画照片,通过 LoadImage 加载。 该场景包含嵌入真实世界环境中的多个 WORD,因此是使用 ReadPhoto 模型对 Enhanced 进行实操测试的绝佳案例。

用作 ReadPhoto OCR 输入的含文字图片

photo.png:通过 ReadPhoto 并使用增强型模型加载的墙面壁画照片,用于生成可搜索的 PDF 文件。

using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("photo.png");

// ReadPhoto with Enhanced model
OcrPhotoResult photoResult = ocr.ReadPhoto(input, ModelType.Enhanced);
Console.WriteLine(photoResult.Text);

// Save as searchable PDF
byte[] pdfBytes = photoResult.SaveAsSearchablePdf();
File.WriteAllBytes("searchable-photo.pdf", pdfBytes);
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("photo.png");

// ReadPhoto with Enhanced model
OcrPhotoResult photoResult = ocr.ReadPhoto(input, ModelType.Enhanced);
Console.WriteLine(photoResult.Text);

// Save as searchable PDF
byte[] pdfBytes = photoResult.SaveAsSearchablePdf();
File.WriteAllBytes("searchable-photo.pdf", pdfBytes);
Imports IronOcr

Dim ocr As New IronTesseract()
Using input As New OcrInput()
    input.LoadImage("photo.png")

    ' ReadPhoto with Enhanced model
    Dim photoResult As OcrPhotoResult = ocr.ReadPhoto(input, ModelType.Enhanced)
    Console.WriteLine(photoResult.Text)

    ' Save as searchable PDF
    Dim pdfBytes As Byte() = photoResult.SaveAsSearchablePdf()
    File.WriteAllBytes("searchable-photo.pdf", pdfBytes)
End Using
$vbLabelText   $csharpLabel

输出

searchable-photo.pdf:由 ReadPhoto 生成的可搜索 PDF。其文本层支持在任何 PDF 阅读器中进行全文搜索。

生成的可搜索 PDF 文件会在识别出的文字上方叠加一层不可见的文本层。 在 PDF 阅读器中搜索"Milk"会返回 3 个匹配结果,这些结果直接提取自原始照片中的手写文本。

同样的方法也适用于 ReadDocumentAdvanced,它返回 OcrDocAdvancedResult

输入

通过 LoadImage 加载的扫描发票。 它包含结构化字段(供应商名称、明细项和总计),这些字段由 ReadDocumentAdvanced 模型识别,并作为可搜索的文本层嵌入。

用作 ReadDocumentAdvanced OCR 输入的发票文档

invoice.png:扫描的发票已加载到 OcrInput 中,并使用增强型模型传递给 ReadDocumentAdvanced。

using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("invoice.png");

// ReadDocumentAdvanced with Enhanced model
OcrDocAdvancedResult docResult = ocr.ReadDocumentAdvanced(input, ModelType.Enhanced);
byte[] docPdfBytes = docResult.SaveAsSearchablePdf();
File.WriteAllBytes("searchable-doc.pdf", docPdfBytes);
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("invoice.png");

// ReadDocumentAdvanced with Enhanced model
OcrDocAdvancedResult docResult = ocr.ReadDocumentAdvanced(input, ModelType.Enhanced);
byte[] docPdfBytes = docResult.SaveAsSearchablePdf();
File.WriteAllBytes("searchable-doc.pdf", docPdfBytes);
Imports IronOcr

Dim ocr As New IronTesseract()
Using input As New OcrInput()
    input.LoadImage("invoice.png")

    ' ReadDocumentAdvanced with Enhanced model
    Dim docResult As OcrDocAdvancedResult = ocr.ReadDocumentAdvanced(input, ModelType.Enhanced)
    Dim docPdfBytes As Byte() = docResult.SaveAsSearchablePdf()
    File.WriteAllBytes("searchable-doc.pdf", docPdfBytes)
End Using
$vbLabelText   $csharpLabel

输出

searchable-doc.pdf:由 ReadDocumentAdvanced 生成的可搜索 PDF。发票字段可供选择和搜索。

SaveAsSearchablePdf 不支持 ReadPassportReadLicensePlate 的结果,并将抛出 ExtensionAdvancedScanException.

处理多页文档

在处理多页文档的 PDF OCR 操作时,IronOCR 会按顺序处理每一页,并保持原始文档的结构。

输入

Hartwell Capital Management 的一份 11 页年度报告,通过 OcrPdfInput 加载。 通过 PageIndices 范围选取第 1–10 页(索引 0–9),并在单次 Read 调用中进行处理。

multi-page-scan.pdf:一份 11 页的 Hartwell Capital Management 年报,用作多页可搜索 PDF 转换的输入文件。

:path=/static-assets/ocr/content-code-examples/how-to/searchable-pdf-multi-page.cs
using IronOcr;

// Create the OCR engine. RenderSearchablePdf is false by default; no need to set it when using OcrPdfInput directly
var ocrTesseract = new IronTesseract();

// Load pages 1–10 (indices 0–9) only; PageIndices avoids loading and OCR-ing the full document unnecessarily
using var pdfInput = new OcrPdfInput("multi-page-scan.pdf", PageIndices: Enumerable.Range(0, 10));

// Run OCR across all selected pages in order
OcrResult result = ocrTesseract.Read(pdfInput);

// Write the searchable PDF; true = apply the input's image filters to the embedded page images in the output
result.SaveAsSearchablePdf("searchable-multi-page.pdf", true);
Imports IronOcr

' Create the OCR engine. RenderSearchablePdf is false by default; no need to set it when using OcrPdfInput directly
Dim ocrTesseract As New IronTesseract()

' Load pages 1–10 (indices 0–9) only; PageIndices avoids loading and OCR-ing the full document unnecessarily
Using pdfInput As New OcrPdfInput("multi-page-scan.pdf", PageIndices:=Enumerable.Range(0, 10))
    ' Run OCR across all selected pages in order
    Dim result As OcrResult = ocrTesseract.Read(pdfInput)

    ' Write the searchable PDF; true = apply the input's image filters to the embedded page images in the output
    result.SaveAsSearchablePdf("searchable-multi-page.pdf", True)
End Using
$vbLabelText   $csharpLabel

输出

searchable-multi-page.pdf:10 页的可搜索 PDF 输出文件。每页均包含一个用于全文搜索的不可见文本图层。

生成的 PDF 文件共 10 页(源自原始报告的第 1–10 页),每页均包含一个不可见的文本图层,使提取的内容可在任何 PDF 阅读器中被选中并进行搜索。

在创建可搜索 PDF 时如何应用过滤器?

SaveAsSearchablePdf 第二个参数接受一个布尔值,用于控制是否对嵌入式输出应用图像滤镜。 使用图像优化过滤器可以显著提高 OCR 的准确性,尤其是在处理低质量扫描时。

下面的示例应用了灰度滤镜,并将 true 作为第二个参数传递,以便将经过滤镜处理的图像嵌入可搜索的 PDF 输出中。

:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-searchable-pdf.cs
using IronOcr;

// Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
var ocr = new IronTesseract();
var ocrInput = new OcrInput();

// Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf");

// Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale();
// Run OCR on the preprocessed input
OcrResult result = ocr.Read(ocrInput);

// Write the searchable PDF; true = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);
Imports IronOcr

' Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
Dim ocr As New IronTesseract()
Dim ocrInput As New OcrInput()

' Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf")

' Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale()
' Run OCR on the preprocessed input
Dim result As OcrResult = ocr.Read(ocrInput)

' Write the searchable PDF; True = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", True)
$vbLabelText   $csharpLabel

为获得最佳效果,请考虑使用 Filter Wizard 自动确定特定文档类型的最佳过滤器组合。 该工具会分析您的输入,并建议适当的预处理步骤。

如何修复可搜索PDF中的错误字符?

如果 PDF 中的文本在视觉上显示正确,但在搜索或复制时显示损坏的字符,则该问题是由可搜索文本层中使用的默认字体引起的。 默认情况下,SaveAsSearchablePdf 使用 Times New Roman 字体,该字体无法完全支持所有 Unicode 字符。 这会影响包含重音字符或非 ASCII 字符的语言。

要解决这个问题,请提供与 Unicode 兼容的字体文件作为第三个参数:

result.SaveAsSearchablePdf("output.pdf", false, "Fonts/LiberationSerif-Regular.ttf");
result.SaveAsSearchablePdf("output.pdf", false, "Fonts/LiberationSerif-Regular.ttf");
result.SaveAsSearchablePdf("output.pdf", False, "Fonts/LiberationSerif-Regular.ttf")
$vbLabelText   $csharpLabel

您还可以将自定义字体名称指定为第四个参数:

result.SaveAsSearchablePdf("output.pdf", false, "Fonts/LiberationSerif-Regular.ttf", "MyFont");
result.SaveAsSearchablePdf("output.pdf", false, "Fonts/LiberationSerif-Regular.ttf", "MyFont");
result.SaveAsSearchablePdf("output.pdf", False, "Fonts/LiberationSerif-Regular.ttf", "MyFont")
$vbLabelText   $csharpLabel

此修复适用于所有结果类型,包括 OcrPhotoResultOcrDocAdvancedResult,因此无论哪个读取方法产生了该结果,修复均有效。

对于最初使用 Times New Roman 排版的文档,建议使用 Liberation Serif,因为它在度量上是兼容的,可以保留原始的间距和布局。 对于通用多语言用途,Noto Sans 或 DejaVu Sans 都是不错的选择。

在无法写入文件路径的情况下,IronOCR 还支持将可搜索的 PDF 作为字节数组或流返回。


如何将可搜索的 PDF 导出为字节或数据流?

可搜索 PDF 的输出内容也可分别通过 SaveAsSearchablePdfBytesSaveAsSearchablePdfStream 方法处理为字节流或流。 下面的代码示例展示了如何使用这些方法。

:path=/static-assets/ocr/content-code-examples/how-to/searchable-pdf-searchable-pdf-byte-stream.cs
// Return as a byte array: suited for storing in a database or sending in an HTTP response body
byte[] pdfByte = ocrResult.SaveAsSearchablePdfBytes();

// Return as a stream: suited for uploading to cloud storage or piping to another I/O operation without buffering the full file
Stream pdfStream = ocrResult.SaveAsSearchablePdfStream();
' Return as a byte array: suited for storing in a database or sending in an HTTP response body
Dim pdfByte As Byte() = ocrResult.SaveAsSearchablePdfBytes()

' Return as a stream: suited for uploading to cloud storage or piping to another I/O operation without buffering the full file
Dim pdfStream As Stream = ocrResult.SaveAsSearchablePdfStream()
$vbLabelText   $csharpLabel

这些输出选项在与云存储服务、数据库或网络应用程序集成时特别有用,因为文件系统访问可能会受到限制。 以下示例展示了实际应用场景:

using IronOcr;
using System.IO;

public class SearchablePdfExporter
{
    public async Task ProcessAndUploadPdf(string inputPath)
    {
        var ocr = new IronTesseract
        {
            Configuration = { RenderSearchablePdf = true }
        };

        // Process the input
        using var input = new OcrImageInput(inputPath);
        var result = ocr.Read(input);

        // Option 1: Save to database as byte array
        byte[] pdfBytes = result.SaveAsSearchablePdfBytes();
        // Store pdfBytes in database BLOB field

        // Option 2: Upload to cloud storage using stream
        using (Stream pdfStream = result.SaveAsSearchablePdfStream())
        {
            // Upload stream to Azure Blob Storage, AWS S3, etc.
            await UploadToCloudStorage(pdfStream, "searchable-output.pdf");
        }

        // Option 3: Return as web response
        // return File(pdfBytes, "application/pdf", "searchable.pdf");
    }

    private async Task UploadToCloudStorage(Stream stream, string fileName)
    {
        // Cloud upload implementation
    }
}
using IronOcr;
using System.IO;

public class SearchablePdfExporter
{
    public async Task ProcessAndUploadPdf(string inputPath)
    {
        var ocr = new IronTesseract
        {
            Configuration = { RenderSearchablePdf = true }
        };

        // Process the input
        using var input = new OcrImageInput(inputPath);
        var result = ocr.Read(input);

        // Option 1: Save to database as byte array
        byte[] pdfBytes = result.SaveAsSearchablePdfBytes();
        // Store pdfBytes in database BLOB field

        // Option 2: Upload to cloud storage using stream
        using (Stream pdfStream = result.SaveAsSearchablePdfStream())
        {
            // Upload stream to Azure Blob Storage, AWS S3, etc.
            await UploadToCloudStorage(pdfStream, "searchable-output.pdf");
        }

        // Option 3: Return as web response
        // return File(pdfBytes, "application/pdf", "searchable.pdf");
    }

    private async Task UploadToCloudStorage(Stream stream, string fileName)
    {
        // Cloud upload implementation
    }
}
Imports IronOcr
Imports System.IO
Imports System.Threading.Tasks

Public Class SearchablePdfExporter
    Public Async Function ProcessAndUploadPdf(inputPath As String) As Task
        Dim ocr As New IronTesseract With {
            .Configuration = New TesseractConfiguration With {
                .RenderSearchablePdf = True
            }
        }

        ' Process the input
        Using input As New OcrImageInput(inputPath)
            Dim result = ocr.Read(input)

            ' Option 1: Save to database as byte array
            Dim pdfBytes As Byte() = result.SaveAsSearchablePdfBytes()
            ' Store pdfBytes in database BLOB field

            ' Option 2: Upload to cloud storage using stream
            Using pdfStream As Stream = result.SaveAsSearchablePdfStream()
                ' Upload stream to Azure Blob Storage, AWS S3, etc.
                Await UploadToCloudStorage(pdfStream, "searchable-output.pdf")
            End Using

            ' Option 3: Return as web response
            ' Return File(pdfBytes, "application/pdf", "searchable.pdf")
        End Using
    End Function

    Private Async Function UploadToCloudStorage(stream As Stream, fileName As String) As Task
        ' Cloud upload implementation
    End Function
End Class
$vbLabelText   $csharpLabel

性能考虑

在处理大量文件时,请考虑实施多线程 OCR 操作以提高吞吐量。 IronOCR 支持并发处理,允许您同时处理多个文档:

using IronOcr;
using System.Threading.Tasks;
using System.Collections.Concurrent;

public class BatchPdfProcessor
{
    private readonly IronTesseract _ocr;

    public BatchPdfProcessor()
    {
        _ocr = new IronTesseract
        {
            Configuration = 
            {
                RenderSearchablePdf = true,
                // Configure for optimal performance
                Language = OcrLanguage.English
            }
        };
    }

    public async Task ProcessBatchAsync(string[] filePaths)
    {
        var results = new ConcurrentBag<(string source, string output)>();

        await Parallel.ForEachAsync(filePaths, async (filePath, ct) =>
        {
            using var input = new OcrImageInput(filePath);
            var result = _ocr.Read(input);

            string outputPath = Path.ChangeExtension(filePath, ".searchable.pdf");
            result.SaveAsSearchablePdf(outputPath);

            results.Add((filePath, outputPath));
        });

        Console.WriteLine($"Processed {results.Count} files");
    }
}
using IronOcr;
using System.Threading.Tasks;
using System.Collections.Concurrent;

public class BatchPdfProcessor
{
    private readonly IronTesseract _ocr;

    public BatchPdfProcessor()
    {
        _ocr = new IronTesseract
        {
            Configuration = 
            {
                RenderSearchablePdf = true,
                // Configure for optimal performance
                Language = OcrLanguage.English
            }
        };
    }

    public async Task ProcessBatchAsync(string[] filePaths)
    {
        var results = new ConcurrentBag<(string source, string output)>();

        await Parallel.ForEachAsync(filePaths, async (filePath, ct) =>
        {
            using var input = new OcrImageInput(filePath);
            var result = _ocr.Read(input);

            string outputPath = Path.ChangeExtension(filePath, ".searchable.pdf");
            result.SaveAsSearchablePdf(outputPath);

            results.Add((filePath, outputPath));
        });

        Console.WriteLine($"Processed {results.Count} files");
    }
}
Imports IronOcr
Imports System.Threading.Tasks
Imports System.Collections.Concurrent

Public Class BatchPdfProcessor
    Private ReadOnly _ocr As IronTesseract

    Public Sub New()
        _ocr = New IronTesseract With {
            .Configuration = New OcrConfiguration With {
                .RenderSearchablePdf = True,
                ' Configure for optimal performance
                .Language = OcrLanguage.English
            }
        }
    End Sub

    Public Async Function ProcessBatchAsync(filePaths As String()) As Task
        Dim results As New ConcurrentBag(Of (source As String, output As String))()

        Await Task.Run(Sub()
                           Parallel.ForEach(filePaths, Sub(filePath)
                                                           Using input As New OcrImageInput(filePath)
                                                               Dim result = _ocr.Read(input)

                                                               Dim outputPath As String = Path.ChangeExtension(filePath, ".searchable.pdf")
                                                               result.SaveAsSearchablePdf(outputPath)

                                                               results.Add((filePath, outputPath))
                                                           End Using
                                                       End Sub)
                       End Sub)

        Console.WriteLine($"Processed {results.Count} files")
    End Function
End Class
$vbLabelText   $csharpLabel

高级配置选项

对于更高级的应用场景,您可以利用详细的 Tesseract 配置,针对特定文档类型或语言对 OCR 引擎进行微调:

var advancedOcr = new IronTesseract
{
    Configuration = 
    {
        RenderSearchablePdf = true,
        TesseractVariables = new Dictionary<string, object>
        {
            { "preserve_interword_spaces", 1 },
            { "tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" }
        },
        PageSegmentationMode = TesseractPageSegmentationMode.SingleColumn
    },
    Language = OcrLanguage.EnglishBest
};
var advancedOcr = new IronTesseract
{
    Configuration = 
    {
        RenderSearchablePdf = true,
        TesseractVariables = new Dictionary<string, object>
        {
            { "preserve_interword_spaces", 1 },
            { "tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" }
        },
        PageSegmentationMode = TesseractPageSegmentationMode.SingleColumn
    },
    Language = OcrLanguage.EnglishBest
};
Imports IronOcr

Dim advancedOcr As New IronTesseract With {
    .Configuration = New TesseractConfiguration With {
        .RenderSearchablePdf = True,
        .TesseractVariables = New Dictionary(Of String, Object) From {
            {"preserve_interword_spaces", 1},
            {"tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}
        },
        .PageSegmentationMode = TesseractPageSegmentationMode.SingleColumn
    },
    .Language = OcrLanguage.EnglishBest
}
$vbLabelText   $csharpLabel

以下配置选项同样适用于所有三种输出方式:SaveAsSearchablePdfBytesSaveAsSearchablePdfStream。 下方的摘要汇总了所有可搜索的 PDF 处理方法及其对应的输出格式。

摘要

使用 IronOCR 创建可搜索 PDF 既简单又灵活。 无论您需要处理单张图片、多页文档、通过 ReadPhoto 处理的照片,还是通过 ReadDocumentAdvanced 处理的高级文档扫描件,该库都提供了强大的方法,可用于生成多种格式的可搜索 PDF。 请使用 ModelType 参数在标准和增强型机器学习模型之间进行选择,以确保翻译准确性。 以文件、字节或流的形式导出的功能使其能够适应从桌面应用程序到基于云的服务等任何应用程序架构。

对于更复杂的 OCR 场景,请查阅全面的代码示例,或参考 API 文档以获取详细的方法签名和选项。

常见问题解答

如何用 C# 从扫描图像创建可搜索的 PDF?

IronOCR 使从扫描图像创建可搜索 PDF 变得非常简单。只需在配置中将 RenderSearchablePdf 设为 true,在输入图像上使用 Read() 方法,然后调用 SaveAsSearchablePdf() 并指定所需的输出路径即可。IronOcr 将对图像执行 OCR,并生成在原始图像上叠加了可选择、可搜索文本的 PDF。

哪些文件格式可以转换为可搜索的 PDF?

IronOCR 可以将各种图像格式(包括 JPG、PNG、TIFF 和现有 PDF 文档)转换为可搜索的 PDF。该库既支持单页图像,也支持 TIFF 文件等多页文档,能自动处理所有页面,并在输出的可搜索 PDF 中保持正确的页面顺序。

能否将可搜索的 PDF 导出为字节数组或流,而不是文件?

是的,IronOCR 支持导出多种格式的可搜索 PDF。除了使用 SaveAsSearchablePdf() 直接保存到文件外,您还可以将 OCR 结果导出为字节数组或流,从而轻松与网络应用程序、云存储或数据库系统集成,而无需创建临时文件。

创建可搜索 PDF 至少需要多少代码?

使用 IronOCR 创建可搜索 PDF 只需一行代码即可完成: new IronOcr.IronTesseract { Configuration = { RenderSearchablePdf = true }.}.Read(new IronOcr.OcrImageInput("file.jpg")).SaveAsSearchablePdf("searchable.pdf").这展示了 IronOCR 简化的 API 设计。

在可搜索 PDF 中,不可见文本层是如何工作的?

IronOCR 会自动将识别出的文本作为不可见图层定位在 PDF 原始图像之上。这确保了文本与图像的精确映射,使用户能够在保持原始文档视觉外观的同时,对文本进行选择和搜索。IronOCR库通过专用字体和定位算法来实现这一功能。

我能将照片或屏幕截图转换为可搜索的 PDF 文件吗?

是的,ReadPhotoReadScreenShotReadDocumentAdvanced 的处理结果均支持 SaveAsSearchablePdf 功能。每个方法返回的结果类型均支持可搜索 PDF 导出,从而能够轻松地将真实照片、屏幕截图或复杂的文档扫描件转换为可搜索的 PDF 文件。

ModelType 参数的作用是什么?

ModelType 参数用于控制 OCR 所使用的预训练机器学习模型。默认值为 Normal,该模式处理缩放至 960 像素的图像以获得快速结果。Enhanced 模式支持最大 2560 像素的图像,能够保留更精细的细节,并提高高分辨率输入的识别准确率。

为什么我在可搜索 PDF 中复制的或搜索到的字符会显示为乱码?

出现这种情况是因为可搜索文本层使用的默认字体(Times New Roman)无法完全支持所有 Unicode 字符。要解决此问题,请将兼容 Unicode 的字体文件作为 SaveAsSearchablePdf 的第三个参数传入。如果您的文档最初使用 Times New Roman 排版,且发现与其他字体存在间距不一致的情况,请尝试使用 Liberation Serif,因为它具有相同的字形度量,并能保留原始布局。

Curtis Chau
技术作家

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

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

审核者
Jeff Fritz
Jeffrey T. Fritz
首席项目经理 - .NET 社区团队
Jeff 也是 .NET 和 Visual Studio 团队的首席项目经理。他是 .NET Conf 虚拟会议系列的执行制片人,并主持“Fritz and Friends”直播节目,每周两次与观众一起谈论技术并编写代码。Jeff 撰写研讨会、演示文稿并计划包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP 峰会在内的最大型微软开发者活动的内容。
准备开始了吗?
Nuget 下载 5,570,591 | 版本: 2026.4 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronOcr
运行示例 观看您的图像变成可搜索文本。