Extract data with GroupDocs.Parser

GroupDocs.Parser pulls content out of documents — plain text, formatted text, images, metadata, hyperlinks, tables and barcodes — across PDF, Word, spreadsheets, presentations, email, ebooks and archives. It is the usual first step when feeding documents to a search index or a language model.

Extract text from a document

GetText returns a TextReader. It returns null rather than throwing when the format does not support text extraction, so check it.

using System;
using System.IO;
using GroupDocs.Parser;

using (Parser parser = new Parser("contract.pdf"))
{
    using (TextReader reader = parser.GetText())
    {
        if (reader == null)
        {
            Console.WriteLine("Text extraction is not supported for this format.");
        }
        else
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}

contract.pdf is the sample file used in this example. Click here to download it.

Consulting Services Agreement
Cascade Partners and Aldergrove Manufacturing
This agreement records the terms under which Cascade Partners will deliver the engagement
known internally as Project Northlight. It is issued by Dana Whitfield, Engagement Lead, and
countersigned by Marcus Oyelaran.

1. Scope of work
Cascade Partners will review the current order-to-cash process at Aldergrove Manufacturing,
identify the constraints limiting throughput, and deliver a prioritised remediation plan with an

[TRUNCATED]

Download full output

Extract text as Markdown

Formatted extraction keeps the document’s structure — headings, lists, emphasis — instead of flattening it. Markdown is the format to ask for when the text is destined for a language model.

using System;
using System.IO;
using GroupDocs.Parser;
using GroupDocs.Parser.Options;

using (Parser parser = new Parser("contract.docx"))
{
    using (TextReader reader = parser.GetFormattedText(
        new FormattedTextOptions(FormattedTextMode.Markdown)))
    {
        if (reader == null)
        {
            Console.WriteLine("Formatted extraction is not supported for this format.");
        }
        else
        {
            File.WriteAllText("parser-extract-markdown.md", reader.ReadToEnd());
        }
    }
}

contract.docx is the sample file used in this example. Click here to download it.

**Consulting Services Agreement**

Cascade Partners and Aldergrove Manufacturing

This agreement records the terms under which Cascade Partners will deliver the engagement known internally as Project Northlight. It is issued by Dana Whitfield, Engagement Lead, and countersigned by Marcus Oyelaran.

# **1. Scope of work**

Cascade Partners will review the current order-to-cash process at Aldergrove Manufacturing, identify the constraints limiting throughput, and deliver a prioritised remediation 
[TRUNCATED]

Download full output

Note
Formatted extraction is not available for every format — PDF returns null from GetFormattedText, where plain GetText works fine. That is why the snippet checks the reader instead of assuming it. Word documents support Markdown, HTML and plain-text modes.

Read document information

GetDocumentInfo reports the page count and format before you commit to extracting anything.

using System;
using GroupDocs.Parser;
using GroupDocs.Parser.Options;

using (Parser parser = new Parser("contract.pdf"))
{
    IDocumentInfo info = parser.GetDocumentInfo();

    Console.WriteLine("File type: " + info.FileType);
    Console.WriteLine("Pages: " + info.PageCount);
    Console.WriteLine("Size: " + info.Size + " bytes");
}

contract.pdf is the sample file used in this example. Click here to download it.

File type: Portable Document Format File(.pdf)
Pages: 3
Size: 130412 bytes

Download full output

Learn more

GroupDocs.Parser also extracts images, hyperlinks, tables of contents and barcodes, parses structured data with user-defined templates, reads PDF form values, and reaches into email attachments and ZIP archives.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.