Last active
June 10, 2021 15:05
-
-
Save aspose-com-gists/16e1a2087c91cfb4f0fa6cddd047fa02 to your computer and use it in GitHub Desktop.
Extract Data from PDF Tables in C# | https://blog.aspose.com/2021/06/10/extract-table-from-pdf-using-csharp/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read the article: https://blog.aspose.com/2021/06/10/extract-table-from-pdf-using-csharp/