当前位置:网站首页>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
效果图

边栏推荐
- 全新的Uber App设计
- RecyclerView的item高度自适应
- LeetCode_二分搜索_简单_367.有效的完全平方数
- 在安装GBase 8c数据库的时候,报错显示“Host ips belong to different cluster”。这是为什么呢?有什么解决办法?
- 【AppCube】数字孪生万物可视 | 联接现实世界与数字空间
- Win10/11 删除文件资源管理器左侧栏目文件夹
- 被审稿人吐槽没有novelty!深度学习方向怎么找创新点?
- Cross-chain bridge protocol Nomad suffers hacker attack, losing more than $150 million
- Interview Blitz 71: What's the difference between GET and POST?
- Apache Doris系列之:数据模型
猜你喜欢

怎么在外头使用容器里php命令

跨链桥协议 Nomad 遭遇黑客攻击,损失超 1.5 亿美元

使用.NET简单实现一个Redis的高性能克隆版(一)

后台图库上传功能

从餐桌到太空,孙宇晨的“星辰大海”

servlet生命周期详解--【结合源码】

袋鼠云思枢:数驹 DTengine,助力企业构建高效的流批一体数据湖计算平台

LyScript 实现对内存堆栈扫描

Analysis of the idea of the complete knapsack problem
![[Detailed explanation of binary search plus recursive writing method] with all the code](/img/51/c4960575a59f8ca7f161b310e47b27.png)
[Detailed explanation of binary search plus recursive writing method] with all the code
随机推荐
509. 斐波那契数
优炫数据库在linux平台下服务启动失败的原因
消费者认可度较高 地理标志农产品为啥“香”
自定义实现乘风破浪的小船
synchronized
Web Server 设置缓存响应字段的一些推荐方案
【二分查找详解外加递归写法】附有全部代码
嵌入式软件组件经典架构与存储器分类
Matplotlib
RecyclerView的item高度自适应
Polymorphism in detail (simple implementation to buy tickets system simulation, covering/weight definition, principle of polymorphism, virtual table)
Question G: Word Analysis ← Questions for the second provincial competition of the 11th Blue Bridge Cup Competition
被审稿人吐槽没有novelty!深度学习方向怎么找创新点?
MySQL数据库实战(1)
【输出一个整数的的每一位,由高到低输出。使用递归和不使用递归】
机器比人更需要通证
【TypeScript】为什么要选择 TypeScript?
Win10/11 删除文件资源管理器左侧栏目文件夹
二叉搜索树(搜索二叉树)模拟实现(有递归版本)
干货!一种被称为Deformable Butterfly(DeBut)的高度结构化且稀疏的线性变换