Extract Paragraph from PDF C#
Extract Text from PDF document in Paragraphs form
We can get text from a PDF document by searching a particular text (using “plain text” or “regular expressions”) from a single page or whole document, or we can get the complete text of a single page, range of pages or complete document. However, in some cases, you require to extract paragraphs from a PDF document or text in the form of Paragraphs. We have implemented functionality for searching sections and paragraphs in the text of PDF document pages. We have introduced ParagraphAbsorber Class (like TextFragmentAbsorber and TextAbsorber), which can be used to extract paragraphs from PDF documents. There are two following ways in which you can use ParagraphAbsorber:
By drawing the border of sections and paragraphs of text on PDF page:
The following code snippet also work with Aspose.PDF.Drawing library.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractParagraph()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Text();
Document document = new Document(dataDir + "input.pdf");
Page page = document.Pages[2];
ParagraphAbsorber absorber = new ParagraphAbsorber();
absorber.Visit(page);
PageMarkup markup = absorber.PageMarkups[0];
foreach (MarkupSection section in markup.Sections)
{
DrawRectangleOnPage(section.Rectangle, page);
foreach (MarkupParagraph paragraph in section.Paragraphs)
{
DrawPolygonOnPage(paragraph.Points, page);
}
}
document.Save(dataDir + "output_out.pdf");
}
private static void DrawRectangleOnPage(Rectangle rectangle, Page page)
{
page.Contents.Add(new Operators.GSave());
page.Contents.Add(new Operators.ConcatenateMatrix(1, 0, 0, 1, 0, 0));
page.Contents.Add(new Operators.SetRGBColorStroke(0, 1, 0));
page.Contents.Add(new Operators.SetLineWidth(2));
page.Contents.Add(
new Operators.Re(rectangle.LLX,
rectangle.LLY,
rectangle.Width,
rectangle.Height));
page.Contents.Add(new Operators.ClosePathStroke());
page.Contents.Add(new Operators.GRestore());
}
private static void DrawPolygonOnPage(Point[] polygon, Page page)
{
page.Contents.Add(new Operators.GSave());
page.Contents.Add(new Operators.ConcatenateMatrix(1, 0, 0, 1, 0, 0));
page.Contents.Add(new Operators.SetRGBColorStroke(0, 0, 1));
page.Contents.Add(new Operators.SetLineWidth(1));
page.Contents.Add(new Operators.MoveTo(polygon[0].X, polygon[0].Y));
for (int i = 1; i < polygon.Length; i++)
{
page.Contents.Add(new Operators.LineTo(polygon[i].X, polygon[i].Y));
}
page.Contents.Add(new Operators.LineTo(polygon[0].X, polygon[0].Y));
page.Contents.Add(new Operators.ClosePathStroke());
page.Contents.Add(new Operators.GRestore());
}
By iterating through paragraphs collection and get the text of them:
The following code snippet also work with Aspose.PDF.Drawing library.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Open an existing PDF file
Document document = new Document(dataDir + "input.pdf");
// Instantiate ParagraphAbsorber
ParagraphAbsorber absorber = new ParagraphAbsorber();
absorber.Visit(document);
foreach (PageMarkup markup in absorber.PageMarkups)
{
int i = 1;
foreach (MarkupSection section in markup.Sections)
{
int j = 1;
foreach (MarkupParagraph paragraph in section.Paragraphs)
{
StringBuilder paragraphText = new StringBuilder();
foreach (List<TextFragment> line in paragraph.Lines)
{
foreach (TextFragment fragment in line)
{
paragraphText.Append(fragment.Text);
}
paragraphText.Append("\r\n");
}
paragraphText.Append("\r\n");
Console.WriteLine("Paragraph {0} of section {1} on page {2}:", j, i, markup.Number);
Console.WriteLine(paragraphText.ToString());
j++;
}
i++;
}
}