当前位置:网站首页>C#/VB.NET 从PDF中提取表格
C#/VB.NET 从PDF中提取表格
2022-08-03 10:56:00 【InfoQ】
程序环境:
从PDF中提取表格具体步骤:
- 实例化PdfDocument类的对象并调用PdfDocument.LoadFromFile()方法加载文档。
- 通过 PdfTableExtractor.ExtractTable(intpageIndex) 方法提取指定页面中的表格。
- 通过 PdfTable.GetText(int rowIndex, intcolumnIndex) 方法将获取具体行和列中的单元格文本内容。
- 将获取的表格内容保存为TXT文件。
完整代码:
using Spire.Pdf;
using Spire.Pdf.Utilities;
using System.IO;
using System.Text;
namespace ExtractTable
{
class Program
{
static void Main(string[] args)
{
//实例化PdfDocument类的对象
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.LoadFromFile("编程语言1.pdf");
//创建StringBuilder类的对象
StringBuilder builder = new StringBuilder();
//实例化PdfTableExtractor类的对象
PdfTableExtractor extractor = new PdfTableExtractor(pdf);
//声明PdfTable类的表格数组
PdfTable[] tableLists;
//遍历PDF页面
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);
//写入文本到StringBuilder容器
builder.Append(text + " ");
}
builder.Append("\r\n");
}
}
}
}
//保存提取的表格内容为txt文档
File.WriteAllText("提取表格.txt", builder.ToString());
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Utilities
Imports System.IO
Imports System.Text
Namespace ExtractTable
Class Program
Private Shared Sub Main(args As String())
'实例化PdfDocument类的对象
Dim pdf As New PdfDocument()
'加载PDF文档
pdf.LoadFromFile("编程语言1.pdf")
'创建StringBuilder类的对象
Dim builder As New StringBuilder()
'实例化PdfTableExtractor类的对象
Dim extractor As New PdfTableExtractor(pdf)
'声明PdfTable类的表格数组
Dim tableLists As PdfTable()
'遍历PDF页面
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)
'写入文本到StringBuilder容器
builder.Append(text & Convert.ToString(" "))
Next
builder.Append(vbCr & vbLf)
Next
Next
End If
Next
'保存提取的表格内容为txt文档
File.WriteAllText("提取表格.txt", builder.ToString())
End Sub
End Class
End Namespace
效果图

边栏推荐
猜你喜欢
APENFT FOUNDATION官宣2022艺术梦想基金主题征集
[LeetCode—Question 2 Sum of Two Numbers Detailed Code Explanation ] The source code is attached, which can be copied directly
训练双塔检索模型,可以不用query-doc样本了?明星机构联合发文
如何改变sys_guid() 返回值类型
MySQL数据库实战(1)
袋鼠云思枢:数驹 DTengine,助力企业构建高效的流批一体数据湖计算平台
鸿蒙第三次
STM32+OLED显示屏制作指针式电子钟
Web Server 设置缓存响应字段的一些推荐方案
孙宇晨式“溢价逻辑”:不局限眼前,为全人类的“星辰大海”大胆下注
随机推荐
Classical Architecture and Memory Classification of Embedded Software Components
With strong network, China mobile to calculate excitation surging energy network construction
Matplotlib
MATLAB programming and application 2.7 Structural data and unit data
STM32入门开发 介绍SPI总线、读写W25Q64(FLASH)(硬件+模拟时序)
LeetCode_二分搜索_简单_367.有效的完全平方数
如何将Oracle/MySQL中的数据迁移到GBase 8c中?
混合型界面:对话式UI的未来
STM32+OLED显示屏制作指针式电子钟
程序员架构修炼之道:如何设计出可持续演进的系统架构?
Dry goods!A highly structured and sparse linear transformation called Deformable Butterfly (DeBut)
Win10/11 删除文件资源管理器左侧栏目文件夹
机器学习概述
Question G: Word Analysis ← Questions for the second provincial competition of the 11th Blue Bridge Cup Competition
MapReduce中ETL数据清洗案例
build --repot
How to make self-introduction
机器学习(公式推导与代码实现)--sklearn机器学习库
MATLAB程序设计与应用 2.7 结构数据与单元数据
ARIMA实现(亲测可用)