本文介绍在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. |
环境配置
- 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
表格内容提取结果:
其他注意事项:
1. 代码中的PDF文件以及生成的.txt文件路径为F:\VS2017Project\ExtractTable\bin\Debug\sample.pdf和F:\VS2017Project\ ExtractTable\bin\Debug\ExtractedTable.txt。文件路径也可以自定义为其他路径。
2.注意使用的dll文件版本。低于7.10.4的其他版本不支持提取表格。
—End—