Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/16e1a2087c91cfb4f0fa6cddd047fa02 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/16e1a2087c91cfb4f0fa6cddd047fa02 to your computer and use it in GitHub Desktop.
// 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();
}
}
}
// 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();
}
}
}
@aspose-com-gists
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment