Extract formatted text from documents

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Parser MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-parser -- dnx GroupDocs.Parser.Mcp --yes
Folders, licensing and more →

GroupDocs.Parser allows to extract formatted text from documents for those cases when simple plain text is not enough and you need to keep formatting like text style, table layout etc.

This feature allows to extract text with integrated HTML tags or Markdown syntax. Even PlainText mode allows to convert your documents to high quality text with integrated ASCII formatting symbols for tables, lists etc.

Extract formatted text from documents

To extract a formatted text from documents simply call the getFormattedText(options) method:

parser.getFormattedText(options); // returns TextReader or null

This method returns an instance of the TextReader class with an extracted text, or null if formatted text extraction isn’t supported for the document. FormattedTextOptions has the following constructor:

new groupdocs.FormattedTextOptions(mode);

The FormattedTextMode enumeration has the following members:

MemberDescription
FormattedTextMode.HtmlHTML format.
FormattedTextMode.MarkdownMarkdown format.
FormattedTextMode.PlainTextPlain text format.

TextReader has the following members:

MemberDescription
readLine()Reads a line of characters from the text reader and returns the data as a string.
readToEnd()Reads all characters from the current position to the end of the text reader and returns them as one string.
close()Releases resources used by the reader.

Here are the steps to extract a HTML formatted text from the document:

  • Instantiate the Parser object for the initial document;
  • Instantiate FormattedTextOptions with HTML text mode;
  • Call the getFormattedText(options) method and obtain the TextReader object;
  • Check if reader isn’t null (formatted text extraction is supported for the document);
  • Read a text from reader.

The following example shows how to extract a document text as HTML text:

const groupdocs = require('@groupdocs/groupdocs.parser');

// Create an instance of Parser class
const parser = new groupdocs.Parser('sample.docx');
try {
  // Extract a formatted text into the reader
  const options = new groupdocs.FormattedTextOptions(groupdocs.FormattedTextMode.Html);
  const reader = parser.getFormattedText(options);
  // If formatted text extraction isn't supported, a reader is null
  if (reader === null) {
    console.log("Formatted text extraction isn't supported");
  } else {
    try {
      // Print a formatted text from the document
      console.log(reader.readToEnd());
    } finally {
      reader.close();
    }
  }
} finally {
  parser.close();
}

process.exit(0);

More resources

Advanced usage topics

To learn more about document data extraction features and get familiar how to extract text, images, forms and more, please refer to the advanced usage section.

Free online document parser App

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

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