Working with Text Extraction

23 Jul 202624 minutes to read

Essential® PDF allows you to extract the text from a particular page or the entire PDF document. The extracted text can include the line, word, and glyph details, along with the font, color, and bounding-rectangle information needed for redaction, search, indexing, and accessibility scenarios.

Watch the following video for a quick overview of extracting text from a PDF document in .NET using the PDF Library.

Working with basic text extraction

You can extract the text from a page by using the ExtractText method in the PdfPageBase class. The method returns the visible text as a single string. The text is returned in the order in which it is written in the document stream, which may not match the visual reading order shown in a PDF reader application.

The following code snippet explains how to extract the text from a page.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Load the first page.
PdfPageBase page = loadedDocument.Pages[0];

//Extract text from first page.
string extractedText = page.ExtractText();
//Close the document.
loadedDocument.Close(true);
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

//Load an existing PDF.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Load the first page.
PdfPageBase page = loadedDocument.Pages[0];

//Extract text from first page.
string extractedText = page.ExtractText();
//Close the document
loadedDocument.Close(true);
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing

'Load an existing PDF.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
'Load the first page.
Dim page As PdfPageBase = loadedDocument.Pages(0)

'Extract the text from the first page.
Dim extractedText As String = page.ExtractText()
'Close the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

NOTE

In this method, the text is extracted in the order in which it is written in the document stream and may not be in the order in which it is viewed in the PDF reader application.
Extracting text from the PDF document pages does not load the entire document content into memory.

The following code illustrates how to extract the text from the entire PDF document:

using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Load the page collection.
PdfLoadedPageCollection loadedPages = loadedDocument.Pages;

string extractedText = string.Empty;
//Extract text from each page of the loaded PDF document.
foreach (PdfLoadedPage loadedPage in loadedPages)
{
    extractedText += loadedPage.ExtractText();
}
//Close the document.
loadedDocument.Close(true);
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

// Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Load the page collection.
PdfLoadedPageCollection loadedPages = loadedDocument.Pages;

string extractedText = string.Empty;
// Extract text from each page of the loaded PDF document.
foreach (PdfLoadedPage loadedPage in loadedPages)
{
    extractedText += loadedPage.ExtractText();
}
// Close the document.
loadedDocument.Close(true);
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing

' Load an existing PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
' Load the page collection.
Dim loadedPages As PdfLoadedPageCollection = loadedDocument.Pages

Dim extractedText As String = String.Empty
' Extract text from each page of the loaded PDF document.
For Each loadedPage As PdfLoadedPage In loadedPages
    extractedText &= loadedPage.ExtractText()
Next loadedPage
' Close the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

Working with layout-based text extraction

You can extract text from a PDF page while preserving its visual layout by using the ExtractText(bool) overload. Pass true to extract text in the same order as displayed in the reader application; pass false (the default) to use the underlying stream order.

Refer to the following code snippet to extract the text with layout.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

//Load the PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Load the first page.
PdfPageBase page = loadedDocument.Pages[0];

//Extract text from the first page in the displayed layout.
string extractedText = page.ExtractText(true);
//Close the document.
loadedDocument.Close(true);
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

//Load an existing PDF.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Load the first page.
PdfPageBase page = loadedDocument.Pages[0];

//Extract text from the first page in the displayed layout.
string extractedText = page.ExtractText(true);
//Close the document.
loadedDocument.Close(true);
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing

' Load an existing PDF.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
' Load the first page.
Dim page As PdfPageBase = loadedDocument.Pages(0)
' Extract text from the first page in the displayed layout.
Dim extractedText As String = page.ExtractText(True)
' Close the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

NOTE

Layout-based text extraction may take additional processing time when compared to the normal extraction mode.

Text Extraction with bounds

The bounds-based text extraction API returns the bounding rectangle, content, and font details for each line, word, or glyph in the page. Use the API that matches your target framework:

  • In .NET Core, use the TextLineCollection overloads.
  • In .NET Framework (Windows Forms, WPF), use the TextLines / List<TextLine> overloads.

Working with Lines

You can get the lines and their properties that contain text by using TextLine. Each TextLine exposes a Bounds rectangle, a Text string, and a WordCollection list of TextWord objects.

Refer to the following code sample.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

// Load the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Get the first page of the loaded PDF document.
PdfPageBase page = loadedDocument.Pages[0];
TextLineCollection lineCollection = new TextLineCollection();

// Extract text from the first page.
string extractedText = page.ExtractText(out lineCollection);
// Get each line from the collection.
foreach (TextLine line in lineCollection.TextLine)
{
    // Get the bounds of the line.
    RectangleF lineBounds = line.Bounds;
    // Get the text in the line.
    string text = line.Text;
}
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using System.Drawing;

// Load the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Get the first page of the loaded PDF document.
PdfPageBase page = loadedDocument.Pages[0];
TextLines lineCollection = new TextLines();

// Extract text from the first page.
string extractedText = page.ExtractText(out lineCollection);
// Get a specific line from the collection.
TextLine line = lineCollection[0];
// Get the bounds of the line.
RectangleF lineBounds = line.Bounds;
// Get the text in the line.
string text = line.Text;
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing

' Load the existing PDF document.
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
' Get the first page of the loaded PDF document.
Dim page As PdfPageBase = loadedDocument.Pages(0)
Dim lineCollection As TextLines = New TextLines()

' Extract text from the first page.
Dim extractedText As String = page.ExtractText(lineCollection)
' Get a specific line from the collection.
Dim line As TextLine = lineCollection(0)
' Get the bounds of the line.
Dim lineBounds As RectangleF = line.Bounds
' Get the text in the line.
Dim text As String = line.Text

You can download a complete working sample from GitHub.

Working with words

You can get the single word and its properties by using TextWord. Each TextWord exposes the bounds, text, and a Glyphs collection of TextGlyph objects.

Refer to the following code sample.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

// Load the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Get the first page of the loaded PDF document.
PdfPageBase page = loadedDocument.Pages[0];
TextLineCollection lineCollection = new TextLineCollection();

// Extract text from the first page.
string extractedText = page.ExtractText(out lineCollection);
// Get each line from the collection.
foreach (TextLine line in lineCollection.TextLine)
{
    // Get the bounds of the line.
    RectangleF lineBounds = line.Bounds;
    // Get the text in the line.
    string text = line.Text;
    // Get the collection of words in the line.
    List<TextWord> textWordCollection = line.WordCollection;
}
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using System.Drawing;

// Load the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Get the first page of the loaded PDF document.
PdfPageBase page = loadedDocument.Pages[0];
TextLines lineCollection = new TextLines();

// Extract text from the first page.
string extractedText = page.ExtractText(out lineCollection);
// Get a specific line from the collection.
TextLine line = lineCollection[0];
// Get the bounds of the line.
RectangleF lineBounds = line.Bounds;
// Get the text in the line.
string text = line.Text;
// Get the collection of words in the line.
List<TextWord> textWordCollection = line.WordCollection;
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing

' Load the existing PDF document.
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
' Get the first page of the loaded PDF document.
Dim page As PdfPageBase = loadedDocument.Pages(0)
Dim lineCollection As TextLines = New TextLines()

' Extract text from the first page.
Dim extractedText As String = page.ExtractText(lineCollection)
' Get a specific line from the collection.
Dim line As TextLine = lineCollection(0)
' Get the bounds of the line.
Dim lineBounds As RectangleF = line.Bounds
' Get the text in the line.
Dim text As String = line.Text
' Get the collection of words in the line.
Dim textWordCollection As List(Of TextWord) = line.WordCollection

You can download a complete working sample from GitHub.

Working with characters

You can retrieve a single glyph (character) and its properties, including bounds, font name, font size, font style, and text color, using the TextGlyph class. Each TextGlyph is exposed through the Glyphs collection of a TextWord.

Refer to the code sample below.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

// Load the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Get the first page of the loaded PDF document.
PdfPageBase page = loadedDocument.Pages[0];
TextLineCollection lineCollection = new TextLineCollection();

// Extract text from the first page.
string extractedText = page.ExtractText(out lineCollection);
// Get a specific line from the collection.
TextLine line = lineCollection.TextLine[0];
// Get the collection of words in the line.
List<TextWord> textWordCollection = line.WordCollection;
// Get a word from the collection using an index.
TextWord textWord = textWordCollection[0];
// Get the glyph details of the word.
List<TextGlyph> textGlyphCollection = textWord.Glyphs;

// Get a character from the word.
TextGlyph textGlyph = textGlyphCollection[0];
// Get the bounds of the character.
RectangleF glyphBounds = textGlyph.Bounds;
// Get the font name of the character.
string glyphFontName = textGlyph.FontName;
// Get the font size of the character.
float glyphFontSize = textGlyph.FontSize;
// Get the font style of the character.
FontStyle glyphFontStyle = textGlyph.FontStyle;
// Get the character in the word.
char glyphText = textGlyph.Text;
// Get the color of the character.
Color glyphColor = textGlyph.TextColor;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using System.Drawing;

// Load the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Get the first page of the loaded PDF document.
PdfPageBase page = loadedDocument.Pages[0];
TextLines lineCollection = new TextLines();

// Extract text from the first page.
string extractedText = page.ExtractText(out lineCollection);
// Get a specific line from the collection.
TextLine line = lineCollection[0];
// Get the collection of words in the line.
List<TextWord> textWordCollection = line.WordCollection;
// Get a word from the collection using an index.
TextWord textWord = textWordCollection[0];
// Get the glyph details of the word.
List<TextGlyph> textGlyphCollection = textWord.Glyphs;

// Get a character from the word.
TextGlyph textGlyph = textGlyphCollection[0];
// Get the bounds of the character.
RectangleF glyphBounds = textGlyph.Bounds;
// Get the font name of the character.
string glyphFontName = textGlyph.FontName;
// Get the font size of the character.
float glyphFontSize = textGlyph.FontSize;
// Get the font style of the character.
FontStyle glyphFontStyle = textGlyph.FontStyle;
// Get the character in the word.
char glyphText = textGlyph.Text;
// Get the color of the character.
Color glyphColor = textGlyph.TextColor;
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing

' Load the existing PDF document.
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
' Get the first page of the loaded PDF document.
Dim page As PdfPageBase = loadedDocument.Pages(0)
Dim lineCollection As New TextLines()

' Extract text from the first page.
Dim extractedText As String = page.ExtractText(lineCollection)
' Get a specific line from the collection.
Dim line As TextLine = lineCollection(0)
' Get the collection of words in the line.
Dim textWordCollection As List(Of TextWord) = line.WordCollection
' Get a word from the collection using an index.
Dim textWord As TextWord = textWordCollection(0)
' Get the glyph details of the word.
Dim textGlyphCollection As List(Of TextGlyph) = textWord.Glyphs

' Get a character from the word.
Dim textGlyph As TextGlyph = textGlyphCollection(0)
' Get the bounds of the character.
Dim glyphBounds As RectangleF = textGlyph.Bounds
' Get the font name of the character.
Dim glyphFontName As String = textGlyph.FontName
' Get the font size of the character.
Dim glyphFontSize As Single = textGlyph.FontSize
' Get the font style of the character.
Dim glyphFontStyle As FontStyle = textGlyph.FontStyle
' Get the character in the word.
Dim glyphText As Char = textGlyph.Text
' Get the color of the character.
Dim glyphColor As Color = textGlyph.TextColor

You can download a complete working sample from GitHub.

NOTE

In .NET Framework, use the ExtractText(out List<TextData>) or ExtractText(out List<TextLine>) method to extract text with metadata from a PDF.
For .NET Core, the equivalent method is ExtractText(out TextLineCollection), which provides a unified structure for handling the extracted text data.

Find Text

The FindText method on the PdfLoadedDocument class locates text within a PDF document. The method returns both the page number and the rectangular coordinates of the identified text occurrences, which is useful for highlighting, redaction, or building search features.

The following code example demonstrates the use of the FindText method.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;

//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Returns the page number and the rectangle positions of the text matches.
Dictionary<int, List<RectangleF>> matchRects = new Dictionary<int, List<RectangleF>>();
loadedDocument.FindText("document", out matchRects);
//Close the document.
loadedDocument.Close(true);
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using System.Drawing;

//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Returns the page number and the rectangle positions of the text matches.
Dictionary<int, List<RectangleF>> matchRects = new Dictionary<int, List<RectangleF>>();
loadedDocument.FindText("document", out matchRects);
//Close the document.
loadedDocument.Close(true);
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing

'Load an existing PDF document.
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Returns the page number and the rectangle positions of the text matches.
Dim matchRects As Dictionary(Of Integer, List(Of RectangleF)) = New Dictionary(Of Integer, List(Of RectangleF))()
loadedDocument.FindText("document", matchRects)
'Close the document.
loadedDocument.Close(True)

You can download a complete working sample from GitHub.

FindText Module API Reference

Method Return Type Description
FindText(List<string> searchItems, out TextSearchResultCollection searchResult) bool Searches for a list of text strings (searchItems) across the entire document, storing the results in searchResult.
FindText(List<string> searchItems, out TextSearchResultCollection searchResult, bool enableMultiThreading) bool Searches for a list of text strings with multi-threading enabled, storing the results in searchResult.
FindText(List<string> searchItems, int pageIndex, out List<MatchedItem> searchResults) bool Searches for text strings on a specific page (pageIndex), returning matches in searchResults.
FindText(List<string> searchItems, int pageIndex, TextSearchOptions textSearchOption, out List<MatchedItem> searchResults) bool Searches on a specific page with search options, returning matches in searchResults.
FindText(List<string> searchItems, TextSearchOptions textSearchOption, out TextSearchResultCollection searchResult) bool Searches with custom options, storing results in searchResult.
FindText(List<string> searchItems, TextSearchOptions textSearchOption, out TextSearchResultCollection searchResult, bool enableMultiThreading) bool Performs a multi-threaded search with options, saving results in searchResult.
FindText(List<TextSearchItem> searchItems, out TextSearchResultCollection searchResult) bool Searches using TextSearchItem objects, storing results in searchResult.
FindText(List<TextSearchItem> searchItems, out TextSearchResultCollection searchResult, bool enableMultiThreading) bool Performs a multi-threaded search using TextSearchItem objects, storing results in searchResult.
FindText(List<TextSearchItem> searchItems, int pageIndex, out List<MatchedItem> searchResults) bool Searches using TextSearchItem on a specific page, returning MatchedItem results.
FindText(string text, out Dictionary<int, List<RectangleF>> matchRect) bool Finds a text string and returns match rectangles for all pages in matchRect.
FindText(string text, int index, out List<RectangleF> matchRect) bool Finds a text string on a specific page (index), returning rectangles in matchRect.

Troubleshooting and FAQ’s

The ExtractText API retrieves only the text that is visibly rendered on the page’s graphics layer. It does not extract the values contained in interactive form fields such as text boxes, combo boxes, or buttons. This is because form field data resides in the PDF’s interactive form (AcroForm) structure, which is separate from the page’s content stream.

To retrieve form field values, you have two recommended options:

  1. Flatten form fields: Convert interactive form fields into static page content, embedding their values directly into the PDF’s text stream. After flattening, any text extraction process (such as ExtractText) includes these values.

  2. Iterate through form fields directly: Access each form field in the PDF’s form collection and read its value programmatically. This approach provides the most accurate and structured method for extracting form data.