Extract text areas

GroupDocs.Parser provides the functionality to extract text areas from documents by the following methods:

IEnumerable<PageTextArea> GetTextAreas();
IEnumerable<PageTextArea> GetTextAreas(PageTextAreaOptions options);
IEnumerable<PageTextArea> GetTextAreas(int pageIndex);
IEnumerable<PageTextArea> GetTextAreas(int pageIndex, PageTextAreaOptions options);

The methods return a collection of PageTextArea objects:

MemberDescription
PageThe page that contains the text area.
RectangleThe rectangular area on the page that contains the text area.
TextThe value of the text area.
BaseLineThe base line of the text area.
TextStyleThe text style of the text area.
AreasThe collection of child text areas.

Text area represents a rectangular page area with a text. Text area can be simple or composite. The simple text area contains only a text and Areas property is always an empty collection (not null). The composite text area doesn’t have its own text. Text property is calculated by its children texts which are contained in Areas property.

Extract text areas

Here are the steps to extract text areas from the whole document:

  • Instantiate Parser object for the initial document;
  • Call GetTextAreas method and obtain collection of PageTextArea objects;
  • Check if collection isn’t null (text areas extraction is supported for the document);
  • Iterate through the collection and get rectangles and text.

The following example shows how to extract all text areas from the whole document:

// Create an instance of Parser class
using(Parser parser = new Parser(filePath))
{
    // Extract text areas
    IEnumerable<PageTextArea> areas = parser.GetTextAreas();
    // Check if text areas extraction is supported
    if(areas == null)
    {
        Console.WriteLine("Page text areas extraction isn't supported");
        return;
    }

    // Iterate over page text areas
    foreach(PageTextArea a in areas)
    {
        // Print a page index, rectangle and text area value:
        Console.WriteLine(string.Format("Page: {0}, R: {1}, Text: {2}", a.Page.Index, a.Rectangle, a.Text));
    }
}

Extract text areas from a document page

Here are the steps to extract text areas from a document page:

  • Instantiate Parser object for the initial document;
  • Call Features.TextAreas property to check if text areas extraction is supported for the document;
  • Call GetTextAreas(int) method with the page index and obtain collection of PageTextArea objects;
  • Check if collection isn’t null (text areas extraction is supported for the document);
  • Iterate through the collection and get rectangles and text.

The following example shows how to extract text areas from a document page:

// Create an instance of Parser class
using(Parser parser = new Parser(filePath))
{
    // Check if the document supports text areas extraction
    if(!parser.Features.TextAreas)
    {
        Console.WriteLine("Document isn't supports text areas extraction.");
        return;
    }

    // Get the document info
    IDocumentInfo documentInfo = parser.GetDocumentInfo();
    // Check if the document has pages
    if(documentInfo.PageCount == 0)
    {
        Console.WriteLine("Document hasn't pages.");
        return;
    }

    // Iterate over pages
    for(int pageIndex = 0; pageIndex < documentInfo.PageCount; pageIndex++)
    {
        // Print a page number 
        Console.WriteLine(string.Format("Page {0}/{1}", pageIndex + 1, documentInfo.PageCount));

        // Iterate over page text areas
        // We ignore null-checking as we have checked text areas extraction feature support earlier
        foreach(PageTextArea a in parser.GetTextAreas(pageIndex))
        {
            // Print a rectangle and text area value:
            Console.WriteLine(string.Format("R: {0}, Text: {1}", a.Rectangle, a.Text));
        }
    }
}

Extract text areas with options

PageTextAreaOptions parameter is used to customize text areas extraction process. This class has the following members:

MemberDescription
RectangleThe rectangular area that contains a text area.
ExpressionThe regular expression.
MatchCaseThe value that indicates whether a text case isn’t ignored.
UniteSegmentsThe value that indicates whether segments are united.
IgnoreFormattingThe value that indicates whether text formatting is ignored.

Here are the steps to extract text areas from the upper-left corner:

The following example shows how to extract only text areas with digits from the upper-left corner:

// Create an instance of Parser class
using(Parser parser = new Parser(filePath))
{
    // Create the options which are used for text area extraction
    PageTextAreaOptions options = new PageTextAreaOptions("[0-9]+", new Rectangle(new Point(0, 0), new Size(300, 100)));

    // Extract text areas which contain only digits from the upper-left corner of a page:
    IEnumerable<PageTextArea> areas = parser.GetTextAreas(options);
    // Check if text areas extraction is supported
    if(areas == null)
    {
        Console.WriteLine("Page text areas extraction isn't supported");
        return;
    }

    // Iterate over page text areas
    foreach(PageTextArea a in areas)
    {
        // Print a page index, rectangle and text area value:
        Console.WriteLine(string.Format("Page: {0}, R: {1}, Text: {2}", a.Page.Index, a.Rectangle, a.Text));
    }
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document parser App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.