文档半岛外围网上直营>>Aspose.PDF使用教程>>Aspose.PDF功能演示:使用 C# 以编程方式从 PDF 表格中提取数据
Aspose.PDF功能演示:使用 C# 以编程方式从 PDF 表格中提取数据
PDF已成为众多领域中使用最广泛的文档格式之一。在各种情况下,它用于生成数据以表格形式显示的发票。在这种情况下,可能需要解析 PDF 以编程方式从表中读取数据。为此,本文介绍了如何使用 C# 从 PDF 表中提取数据。
- 在 C# 中从 PDF 表格中提取数据
- 从页面的特定区域提取表格
为了从 PDF 文件中的表格中提取数据,我们将使用Aspose.PDF for .NET,它是一个强大的 API,提供了广泛的 PDF 操作功能。点击下方按钮可下载试用。
在 C# 中从 PDF 表格中提取数据
以下是使用 C# 从 PDF 表格中提取数据的步骤。
- 使用Document类加载 PDF 文档。
- 使用Document.Pages集合循环浏览 PDF 中的页面。
- 在每次迭代中,初始化TableAbsorber对象并使用TableAbsorber.Visit(Page)方法访问所选页面。
- 在嵌套循环中,遍历TableAbsorber.TableList集合中的表列表。
- 对于每个AbsorbedTable集合,在迭代通过行的集合AbsorbedTable.RowList。
- 对于每个AbsorbedRow收集,迭代通过在细胞收集AbsorbedRow.CellList。
- 最后,遍历TextFragments每个集合AbsorbedCell和打印文本。
以下代码示例展示了如何在 C# 中从 PDF 表格中提取文本。
// Load source PDF document
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document("sample.pdf");
// Loop through the pages
foreach (var page in pdfDocument.Pages)
{
// Create a table absorber and visit the page
Aspose.Pdf.Text.TableAbsorber absorber = new Aspose.Pdf.Text.TableAbsorber();
absorber.Visit(page);
// Loop through each absorbed table
foreach (AbsorbedTable table in absorber.TableList)
{
Console.WriteLine("Table");
// Loop through each row in the table
foreach (AbsorbedRow row in table.RowList)
{
// Loop through each cell in the row
foreach (AbsorbedCell cell in row.CellList)
{
// Loop through the text fragments
foreach (TextFragment fragment in cell.TextFragments)
{
var sb = new StringBuilder();
foreach (TextSegment seg in fragment.Segments)
sb.Append(seg.Text);
Console.Write($"{sb.ToString()}|");
}
}
Console.WriteLine();
}
}
}
从页面的特定区域提取表格
以下是使用 C# 从 PDF 页面的特定部分提取表格的步骤。
- 使用Document类加载 PDF 文档。
- 从Document.Pages集合中选择所需的页面。
- 提取页面的 Square 注释。
- 初始化TableAbsorber对象并使用TableAbsorber.Visit(Page)方法访问页面。
- 在嵌套循环中,遍历TableAbsorber.TableList集合中的表列表。
-
如果该表在该区域中,则执行以下步骤。
- 通过迭代行的集合AbsorbedTable.RowList。
- 对于每个AbsorbedRow收集,迭代通过在细胞收集AbsorbedRow.CellList。
- 最后,遍历TextFragments每个集合AbsorbedCell和打印文本。
以下代码示例展示了如何从 PDF 页面的特定区域提取表格。
// Load source PDF document
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document("sample.pdf");
// Select the page and extract its square annotation
var page = pdfDocument.Pages[1];
var squareAnnotation =
page.Annotations.FirstOrDefault(ann => ann.AnnotationType == Annotations.AnnotationType.Square)
as Annotations.SquareAnnotation;
// Create table absorber and visit the page
Aspose.Pdf.Text.TableAbsorber absorber = new Aspose.Pdf.Text.TableAbsorber();
absorber.Visit(page);
// Loop through each absorbed table in the list
foreach (AbsorbedTable table in absorber.TableList)
{
var isInRegion = (squareAnnotation.Rect.LLX < table.Rectangle.LLX) && (squareAnnotation.Rect.LLY < table.Rectangle.LLY) && (squareAnnotation.Rect.URX > table.Rectangle.URX) &&
(squareAnnotation.Rect.URY > table.Rectangle.URY);
if (isInRegion)
{
// Loop through each row of the table
foreach (AbsorbedRow row in table.RowList)
{
// Loop through each cell in the row
foreach (AbsorbedCell cell in row.CellList)
{
// Loop through the text fragments and print the text
foreach (TextFragment fragment in cell.TextFragments)
{
var sb = new StringBuilder();
foreach (TextSegment seg in fragment.Segments)
{
sb.Append(seg.Text);
}
var text = sb.ToString();
Console.Write($"{text}|");
}
}
Console.WriteLine();
}
}
}
如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线