- 在 C# 中从 PDF 表格中提取数据
- 从页面的特定区域提取表格
为了从 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 documentAspose.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 pageAspose.Pdf.Text.TableAbsorber absorber = new Aspose.Pdf.Text.TableAbsorber();absorber.Visit(page);// Loop through each absorbed tableforeach (AbsorbedTable table in absorber.TableList){Console.WriteLine("Table");// Loop through each row in the tableforeach (AbsorbedRow row in table.RowList){// Loop through each cell in the rowforeach (AbsorbedCell cell in row.CellList){ // Loop through the text fragmentsforeach (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 documentAspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document("sample.pdf"); // Select the page and extract its square annotationvar page = pdfDocument.Pages[1];var squareAnnotation =page.Annotations.FirstOrDefault(ann => ann.AnnotationType == Annotations.AnnotationType.Square)as Annotations.SquareAnnotation;// Create table absorber and visit the pageAspose.Pdf.Text.TableAbsorber absorber = new Aspose.Pdf.Text.TableAbsorber();absorber.Visit(page);// Loop through each absorbed table in the listforeach (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 tableforeach (AbsorbedRow row in table.RowList){// Loop through each cell in the rowforeach (AbsorbedCell cell in row.CellList){// Loop through the text fragments and print the textforeach (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的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!