IRONSOFTWAREHOME

如何使用 C# 阅读文档中的表格

Curtis Chau
Curtis Chau
Updated: 2026年6月29日

IronOCR使C#开发人员能够使用高级机器学习模型从PDF和图像中的表格中提取数据,处理具备基本单元格的简单表格和具有合并单元格的复杂结构(如发票)使用ReadDocumentAdvanced方法。

使用普通 Tesseract 从表格中提取数据具有挑战性,因为文本通常位于单元格中,并且稀疏地分布在文档中。 不过,我们的库包括一个经过训练和微调的机器学习模型,可以准确地检测和提取表格数据。 无论是处理财务报告、库存清单还是发票数据,IronOCR 都能提供高效解析结构化数据的工具。

对于简单表格,依靠使用标准OcrInput的直接表格检测。 对于更复杂的结构,我们的独家ReadDocumentAdvanced方法提供了强有力的结果,有效地解析表格并交付数据。 这种先进的方法利用机器学习来理解表格布局、合并单元格和复杂的格式,而传统的 OCR 通常很难做到这一点。

快速入门:一次调用提取复杂表格单元

在几分钟内立即启动——此示例展示了如何使用单个IronOCR调用通过ReadDocumentAdvanced从复杂文档中获取详细的表格单元格数据。
它通过加载 PDF、应用高级表格检测并直接返回单元格信息列表来展示易用性。

  1. 1Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. 2复制并运行这段代码。

    var cells = new IronTesseract().ReadDocumentAdvanced(new OcrInput().LoadPdf("invoiceTable.pdf")).Tables.First().CellInfos;
    C#
  3. 3部署到您的生产环境中进行测试

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

以下步骤指导您开始使用 IronOCR 读取表格:


如何从简单表格中提取数据?

ReadDataTables属性设置为true可启用使用Tesseract的表格检测。 这种方法适用于单元格边界清晰、无合并单元格的基本表格。 我创建了一个简单的表格 PDF 来测试这个功能,你可以从这里下载:' simple-table.pdf '。 可以使用此方法检测没有合并单元格的简单表格。 对于更复杂的表格,请参考下面介绍的方法。

标准表格检测方法对以下方面尤为有效:

  • 电子表格输出
  • 具有一致行/列结构的基本数据表
  • 包含表格数据的报告
  • 简单的库存清单

如果在一般情况下使用 PDF OCR 文本提取,此方法可与 IronOCR 更广泛的文档处理功能无缝集成。

using IronOcr;
using System;
using System.Data;

// Instantiate OCR engine
var ocr = new IronTesseract();

// Enable table detection
ocr.Configuration.ReadDataTables = true;

using var input = new OcrPdfInput("simple-table.pdf");
var result = ocr.Read(input);

// Retrieve the data
var table = result.Tables[0].DataTable;

// Print out the table data
foreach (DataRow row in table.Rows)
{
    foreach (var item in row.ItemArray)
    {
        Console.Write(item + "\t");
    }
    Console.WriteLine();
}

如何阅读复杂的发票表格?

在商业环境中,发票是比较常见的复杂表格之一。 发票是包含行和列数据的复杂表格,通常具有合并单元格、不同列宽和嵌套结构。 使用IronOCR,我们利用ReadDocumentAdvanced方法有效地处理它们。 该过程包括扫描文档、识别表格结构和提取数据。 在本例中,我们使用"invoiceTable"文件来展示 IronOCR 如何检索发票中的所有信息。

ReadDocumentAdvanced方法要求同时安装IronOcr.Extensions.AdvancedScan包和基础IronOCR包。 该扩展提供先进的机器学习功能,专门针对复杂的文档布局进行训练。

[{我:(
在 .NET Framework 上使用高级扫描功能需要项目在 x64 架构上运行。 进入项目配置,取消选中"首选 32 位"选项即可实现此目的。 请在以下故障排除指南中了解更多信息:"Advanced Scan on .NET Framework"。
)}]

using IronOcr;
using System.Linq;

// Instantiate OCR engine
var ocr = new IronTesseract();

using var input = new OcrInput();
input.LoadPdf("invoiceTable.pdf");

// Perform OCR
var result = ocr.ReadDocumentAdvanced(input);

var cellList = result.Tables.First().CellInfos;

该方法将文档中的文本数据分为两类:一类有边框,另一类没有边框。 对于带边框的内容,库会根据表格的结构将其进一步细分为小节。 该方法擅长处理

  • 具有不同描述的发票细列项目
  • 多栏价格细目
  • 发货和账单地址块
  • 税金和总额计算部分
  • 页眉和页脚信息

结果如下所示。 由于此方法侧重于边框所包含的信息,任何跨越多行的合并单元格都将被视为单个单元格。

提取的数据是什么样的?

Iron Software OCR提取来自运输发票的表格数据,并转化为结构化分层格式

如何组织和处理提取的表格单元格?

在当前实现中,提取的单元格尚未正确组织。 但是,每个单元格都包含有价值的信息,例如 X 和 Y 坐标、尺寸等等。 利用这些数据,我们可以创建一个用于各种用途的辅助类。 单元信息包括

  • 精确的 X/Y 坐标定位
  • 宽度和高度尺寸
  • 文本内容
  • 信心评分
  • 单元格关系

通过这些详细信息,您可以以编程方式重建表格结构,并应用自定义逻辑进行数据提取。 您还可以使用这些坐标来定义特定区域,以便在后续操作中进行有针对性的 OCR 处理。

以下是一些基本辅助方法:

using System;
using System.Collections.Generic;
using System.Linq;

// A helper class to process table data by sorting cells based on coordinates
public static class TableProcessor
{
    // Method to organize cells by their coordinates (Y top to bottom, X left to right)
    public static List<CellInfo> OrganizeCellsByCoordinates(List<CellInfo> cells)
    {
        // Sort cells by Y (top to bottom), then by X (left to right)
        var sortedCells = cells
            .OrderBy(cell => cell.CellRect.Y)
            .ThenBy(cell => cell.CellRect.X)
            .ToList();

        return sortedCells;
    }

    // Example method demonstrating how to process multiple tables
    public static void ProcessTables(Tables tables)
    {
        foreach (var table in tables)
        {
            var sortedCells = OrganizeCellsByCoordinates(table.CellInfos);

            Console.WriteLine("Organized Table Cells:");

            // Initialize previous Y coordinate
            int previousY = sortedCells.Any() ? sortedCells.First().CellRect.Y : 0;

            foreach (var cell in sortedCells)
            {
                // Print a new line if the Y-coordinate changes, indicating a new row
                if (Math.Abs(cell.CellRect.Y - previousY) > cell.CellRect.Height * 0.8)
                {
                    Console.WriteLine();  // Start a new row
                    previousY = cell.CellRect.Y;
                }

                // Print the cell text followed by a tab
                Console.Write($"{cell.CellText}\t");
            }

            Console.WriteLine("\n--- End of Table ---");  // End of a table
        }
    }

    // Method to extract a specific row by the given index
    public static List<CellInfo> ExtractRowByIndex(TableInfo table, int rowIndex)
    {
        if (table == null || table.CellInfos == null || !table.CellInfos.Any())
        {
            throw new ArgumentException("Table is empty or invalid.");
        }

        var sortedCells = OrganizeCellsByCoordinates(table.CellInfos);
        List<List<CellInfo>> rows = new List<List<CellInfo>>();

        // Group cells into rows based on Y coordinates
        int previousY = sortedCells.First().CellRect.Y;
        List<CellInfo> currentRow = new List<CellInfo>();

        foreach (var cell in sortedCells)
        {
            if (Math.Abs(cell.CellRect.Y - previousY) > cell.CellRect.Height * 0.8)
            {
                // Store the completed row and start a new one
                rows.Add(new List<CellInfo>(currentRow));
                currentRow.Clear();

                previousY = cell.CellRect.Y;
            }

            currentRow.Add(cell);
        }

        // Add the last row if it wasn't added yet
        if (currentRow.Any())
        {
            rows.Add(currentRow);
        }

        // Retrieve the specified row
        if (rowIndex < 0 || rowIndex >= rows.Count)
        {
            throw new IndexOutOfRangeException($"Row index {rowIndex} is out of range.");
        }

        return rows[rowIndex];
    }
}

表提取的最佳实践

在 IronOCR 中进行表格提取时,请考虑以下最佳实践:

1.文档质量:更高分辨率的文档能产生更好的效果。 对于扫描文件,请确保至少为 300 DPI。

2.预处理:对于质量较差或表格倾斜的文档,可考虑在处理前使用 IronOCR 的图像校正功能。

3.性能:对于包含多个表格的大型文档,请考虑使用多线程和异步支持来并行处理页面。

4.输出选项:提取表格数据后,您可以以各种格式导出结果。 了解有关数据输出选项以及如何从处理过的文档中创建可搜索 PDF 的更多信息。

5.流处理:对于网络应用程序或处理内存中文档的应用场景,请考虑使用 OCR for PDF streams 来避免文件系统操作。

摘要

IronOCR 通过基于 Tesseract 的标准检测和先进的机器学习方法提供强大的表格提取功能。 对于简单表格,标准方法效果很好,而ReadDocumentAdvanced方法在处理复杂文档(如发票)方面表现出色。 利用提供的辅助方法,您可以组织和处理提取的数据,以满足您的特定需求。

探索更多 IronOCR 功能,以增强您的文档处理工作流,并在您的 .NET 应用程序中充分发挥光学字符识别的潜力。

常见问题解答

如何用 C# 从 PDF 和图像中提取表格数据?

IronOCR 使 C# 开发人员能够使用先进的机器学习模型从 PDF 和图像中提取表格数据。对于简单的表格,可使用 OcrInput 类,并将 ReadDataTables 属性设置为 true。对于合并单元格的复杂表格,使用 ReadDocumentAdvanced 方法可获得更准确的结果。

简单表格提取和复杂表格提取有什么区别?

IronOCR 中的简单表格提取使用 Tesseract 的 ReadDataTables 属性,对于单元格边界清晰的基本表格效果很好。复杂的表格提取需要使用 ReadDocumentAdvanced 方法,该方法使用机器学习来处理合并单元格、发票和复杂格式。

如何从复杂的表格中快速提取数据?

在一次调用中使用 IronOCR 的 ReadDocumentAdvanced 方法:var cells = new IronTesseract().ReadDocumentAdvanced(new IronOcrInput().LoadPdf('invoiceTable.pdf')).Tables.First().CellInfos; 这将利用机器学习来理解表格布局和复杂格式。

哪些类型的文档最适合使用简单表格检测?

IronOCR 的简单表格检测方法在电子表格导出、具有一致行/列结构的基本数据表、包含表格数据的报告以及没有合并单元格的简单库存清单方面特别有效。

如何启用基本表格的表格检测?

要在 IronOCR 中启用基本表的表格检测功能,请将 ReadDataTables 属性设置为 true。这将使用 Tesseract 的表格检测功能,对于单元格边界清晰、无合并单元格的表格效果很好。

库能否处理布局复杂的发票和财务报告?

是的,IronOCR 的 ReadDocumentAdvanced 方法专门用于处理发票和财务报告等复杂文档。它使用经过训练的机器学习模型来检测和提取具有合并单元格和复杂格式的表格中的数据。

Is it possible to organize extracted table cells based on coordinates using IronOCR?

Yes, IronOCR provides detailed information for each extracted cell, including its coordinates. You can use this data to organize cells programmatically by sorting them based on X and Y coordinates, allowing for precise reconstruction of the table structure.

What document quality is recommended for optimal results when using IronOCR for table extraction?

For optimal results, it is recommended to use documents with a minimum resolution of 300 DPI. Higher quality documents improve the accuracy of the OCR process and result in better table extraction outcomes.

Can IronOCR process tables in multi-page documents effectively?

Yes, IronOCR can process tables in multi-page documents effectively. It supports parallel processing using multithreading and async support to handle large documents with multiple tables efficiently.

What output options does IronOCR provide after extracting table data?

IronOCR provides several output options after extracting table data, including exporting results in various formats and creating searchable PDFs. Explore its data output options to suit your needs.

Curtis Chau
技术作家

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

...
阅读更多

准备开始了吗?

Nuget Downloads 6,236,385版本:2026.9刚刚发布

立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronOcr
nuget.org/packages/IronOcr/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索"IronOCR"
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

或在这里下载Windows安装程序。

  1. 下载并解压IronOCR到你的解决方案目录中的~/Libs位置
  2. 在Visual Studio解决方案资源管理器中,右键点击引用。选择浏览,“IronOCR.dll”

许可证售价$999

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户