C# 提取PDF中的表格詳情

1、簡單介紹

本文介紹在C#程序中(附VB.NET代碼)提取PDF中的表格的方法,調用Spire.PDF for .NET提供的提取表格的 類 以及 方法 等來獲取表格單元格中的文本內容;代碼內容中涉及到的主要類及方法歸納如下表,供參考:

類型

描述

PdfDocument Class

Represents a pdf document model.

PdfDocument.LoadFromFile(string filename) Method

Loads a PDF document.

PdfTableExtractor Class

Represents the PDF table extractor.

PdfTable Class

Defines a PDF table.

PdfTableExtractor. ExtractTable(int pageIndex) Method

Extracts table from page.

PdfTable.GetText(int rowIndex,int columnIndex) Method

Gets Text in cell.

File.WriteAllText() Method

Saves extracted text in table to a .txt file.

2、環境配置

  • Visual Studio 2017
  •   .net framework 4.6.1
  •   PDF測試文檔
  •   庫:Spire.PDF for .NET 7.10.4

引用dll文件的2種方法:

方法1 :通過 NuGet 安裝。

【步驟】

鼠標右鍵點擊“引用”,“管理NuGet程序包”,

點擊“瀏覽”,在搜索框中輸入,點擊“安裝”,

或者使用PM控制臺安裝:

PM>Install-Package Spire.PDF -Version 7.10.4 

方法2 :手動添加引用。

【步驟】

鼠標右鍵點擊“引用”,“添加引用”,

點擊“瀏覽”,“瀏覽”,將本地路徑下的dll文件(需提前 下載 到本地,並解壓)添加到引用列表

 

點擊OK,完成引用:

代碼示例
C#
using Spire.Pdf;
using Spire.Pdf.Utilities;
using System.IO;
using System.Text;

namespace ExtractTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //加載PDF文檔
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(“sample.pdf”);
            StringBuilder builder = new StringBuilder();

            //抽取表格
            PdfTableExtractor extractor = new PdfTableExtractor(pdf);
            PdfTable[] tableLists = null;
            for (int pageIndex = 0; pageIndex < pdf.Pages.Count; pageIndex++)
            {
                tableLists = extractor.ExtractTable(pageIndex);
                if (tableLists != null && tableLists.Length > 0)
                {
                    foreach (PdfTable table in tableLists)
                    {
                        int row = table.GetRowCount();
                        int column = table.GetColumnCount();
                        for (int i = 0; i < row; i++)
                        {
                            for (int j = 0; j < column; j++)
                            {
                                string text = table.GetText(i, j);
                                builder.Append(text + ” “);
                            }
                            builder.Append(“\r\n”);
                        }
                    }
                }
            }

            //保存提取的表格內容到txt文檔
            File.WriteAllText(“ExtractedTable.txt”, builder.ToString());
        }
    }
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Utilities
Imports System.IO
Imports System.Text

Namespace ExtractTable
    Class Program
        Private Shared Sub Main(args As String())
            ‘加載PDF文檔
            Dim pdf As New PdfDocument()
            pdf.LoadFromFile(“sample.pdf”)
            Dim builder As New StringBuilder()

            ‘抽取表格
            Dim extractor As New PdfTableExtractor(pdf)
            Dim tableLists As PdfTable() = Nothing
            For pageIndex As Integer = 0 To pdf.Pages.Count – 1
                tableLists = extractor.ExtractTable(pageIndex)
                If tableLists IsNot Nothing AndAlso tableLists.Length > 0 Then
                    For Each table As PdfTable In tableLists
                        Dim row As Integer = table.GetRowCount()
                        Dim column As Integer = table.GetColumnCount()
                        For i As Integer = 0 To row – 1
                            For j As Integer = 0 To column – 1
                                Dim text As String = table.GetText(i, j)
                                builder.Append(text & Convert.ToString(” “))
                            Next
                            builder.Append(vbCr & vbLf)
                        Next
                    Next
                End If
            Next

            ‘保存提取的表格內容到txt文檔
            File.WriteAllText(“ExtractedTable.txt”, builder.ToString())
        End Sub
    End Class
End Namespace

表格內容提取結果:

其他註意事項:
代碼中的PDF文件以及生成的.txt文件路徑為 F:\VS2017Project\ExtractTable\bin\Debug\sample.pdf 和 F:\VS2017Project\ ExtractTable\bin\Debug\ExtractedTable.txt。文件路徑也可以自定義為其他路徑。
註意使用的dll文件版本。低於7.10.4的其他版本不支持提取表格。

推薦閱讀: