Text Extraction in JavaScript PDF Library

23 Jul 202618 minutes to read

The JavaScript PDF library allows you to extract text from a particular page or from the entire PDF document. The PdfDataExtractor class from the @syncfusion/ej2-pdf-data-extract add-on package supports four extraction modes:

  • Basic text extraction — Retrieve plain text content.
  • Page-range extraction — Retrieve plain text from a defined start and end page.
  • Layout-based text extraction — Preserve the visual layout of the source document.
  • Bounds-based text extraction — Retrieve per-line, per-word, and per-character (glyph) information, including position (bounds), font, size, style, and color.

NOTE

The @syncfusion/ej2-pdf-data-extract add-on package also powers the redaction features available in the JavaScript PDF Library.

Working with basic text extraction

This example demonstrates how to extract plain text from a PDF document using the PdfDataExtractor class. Basic text extraction retrieves text content from the entire PDF document.

import { PdfDocument } from '@syncfusion/ej2-pdf';
import { PdfDataExtractor } from '@syncfusion/ej2-pdf-data-extract';

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
let extractor: PdfDataExtractor = new PdfDataExtractor(document);
// Extract text content from the PDF document.
let text: string = extractor.extractText();
// Save the document
document.save('Output.pdf');
// Close the document
document.destroy();
// Load an existing PDF document
var document = new ej.pdf.PdfDocument(data);
// Initialize a new instance of the PdfDataExtractor class
var extractor = new ej.pdfdataextract.PdfDataExtractor(document);
// Extract text content from the PDF document
var text = extractor.extractText();
// Save the document
document.save('Output.pdf');
// Close the document
document.destroy();

Extract text from specific page range in a PDF document

This example demonstrates how to extract text from a PDF document by specifying a start and end page index. This approach allows you to retrieve text content from a defined range of pages for processing or analysis.

import { PdfDocument } from '@syncfusion/ej2-pdf';
import { PdfDataExtractor } from '@syncfusion/ej2-pdf-data-extract';

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
let extractor: PdfDataExtractor = new PdfDataExtractor(document);
// Extract text content from the specified page range
let text: string = extractor.extractText({ startPageIndex: 0, endPageIndex: document.pageCount - 1 });
// Release document resources
document.destroy();
// Load an existing PDF document
var document = new ej.pdf.PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
var extractor = new ej.pdfdataextract.PdfDataExtractor(document);
// Extract text content from the specified page range
var text = extractor.extractText({ startPageIndex: 0, endPageIndex: document.pageCount - 1 });
// Release document resources
document.destroy();

Working with layout-based text extraction

This example demonstrates how to extract text from a PDF document using the PdfDataExtractor class with layout-based options. Layout-based extraction preserves the visual structure of the source document, including line breaks and spacing.

import { PdfDocument } from '@syncfusion/ej2-pdf';
import { PdfDataExtractor } from '@syncfusion/ej2-pdf-data-extract';

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
let extractor: PdfDataExtractor = new PdfDataExtractor(document);
// Extract text from the PDF page based on its layout
let text: string = extractor.extractText({ isLayout: true });
// Release document resources
document.destroy();
// Load an existing PDF document
var document = new ej.pdf.PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
var extractor = new ej.pdfdataextract.PdfDataExtractor(document);
// Extract text from the PDF page based on its layout
var text = extractor.extractText({ isLayout: true });
// Release document resources
document.destroy();

NOTE

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

Text extraction with bounds

The following sections describe how to extract text along with positional and typographic information using the extractTextLines method. The method returns a hierarchical collection of TextLine, TextWord, and TextGlyph objects.

Working with lines

This example demonstrates how to extract text from a PDF page based on individual lines. The extractTextLines method returns a collection of TextLine objects, allowing precise access to text content line by line.

import { PdfDocument } from '@syncfusion/ej2-pdf';
import { PdfDataExtractor, TextLine, TextWord, TextGlyph, PdfFontStyle, Rectangle } from '@syncfusion/ej2-pdf-data-extract';

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
let extractor: PdfDataExtractor = new PdfDataExtractor(document);
// Extract `TextLine` objects from the PDF document
let textLines: Array<TextLine> = extractor.extractTextLines({ startPageIndex: 0, endPageIndex: document.pageCount - 1 });
// Iterate through each text line in the collection
textLines.forEach((textLine: TextLine) => {
    // Gets the bounds of the text line
    let lineBounds: Rectangle = textLine.bounds;
    // Gets the single line of extracted text from the PDF page
    let line: string = textLine.text;
    // Gets the page index of the text line extracted
    let pageIndex: number = textLine.pageIndex;
    // Gets the collection of text words extracted from a specified page in a PDF document
    let words: TextWord[] = textLine.words;
    // Gets the name of the font used for a particular line of text
    let fontName: string = textLine.fontName;
    // Gets the font style used for a particular line of text
    let fontStyle: PdfFontStyle = textLine.fontStyle;
    // Gets the font size used for a particular line of text
    let fontSize: number = textLine.fontSize;
});
// Release document resources
document.destroy();
// Load an existing PDF document
var document = new ej.pdf.PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
var extractor = new ej.pdfdataextract.PdfDataExtractor(document);
// Extract `TextLine` objects from the PDF document
var textLines = extractor.extractTextLines({ startPageIndex: 0, endPageIndex: document.pageCount - 1 });
// Iterate through each text line in the collection
textLines.forEach((textLine) => {
    // Gets the bounds of the text line
    var lineBounds = textLine.bounds;
    // Gets the single line of extracted text from the PDF page
    var line = textLine.text;
    // Gets the page index of the text line extracted
    var pageIndex = textLine.pageIndex;
    // Gets the collection of text words extracted from a specified page in a PDF document
    var words = textLine.words;
    // Gets the name of the font used for a particular line of text
    var fontName = textLine.fontName;
    // Gets the font style used for a particular line of text
    var fontStyle = textLine.fontStyle;
    // Gets the font size used for a particular line of text
    var fontSize = textLine.fontSize;
});
// Release document resources
document.destroy();

Working with words

This example demonstrates how to extract words from a PDF document using the extractTextLines method. Each line contains a collection of TextWord objects.

import { PdfDocument } from '@syncfusion/ej2-pdf';
import { PdfDataExtractor, TextLine, TextWord, TextGlyph, PdfFontStyle, Rectangle } from '@syncfusion/ej2-pdf-data-extract';

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
let extractor: PdfDataExtractor = new PdfDataExtractor(document);
// Extract `TextLine` objects from the PDF document
let textLines: Array<TextLine> = extractor.extractTextLines({ startPageIndex: 0, endPageIndex: document.pageCount - 1 });
textLines.forEach((textLine: TextLine) => {
    textLine.words.forEach((textWord: TextWord) => {
        // Gets the bounds of the text word
        let wordBounds: Rectangle = textWord.bounds;
        // Gets the single word of extracted text from the PDF page
        let word: string = textWord.text;
        // Gets the collection of text glyphs extracted from a specified page in a PDF document
        let glyphs: TextGlyph[] = textWord.glyphs;
        // Gets the name of the font used for a particular word
        let wordFontName: string = textWord.fontName;
        // Gets the style of the font used for a particular word
        let wordFontStyle: PdfFontStyle = textWord.fontStyle;
        // Gets the size of the font used for a particular word
        let wordFontSize: number = textWord.fontSize;
    });
});
// Release document resources
document.destroy();
// Load an existing PDF document
var document = new ej.pdf.PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
var extractor = new ej.pdfdataextract.PdfDataExtractor(document);
// Extract `TextLine` objects from the PDF document
var textLines = extractor.extractTextLines({ startPageIndex: 0, endPageIndex: document.pageCount - 1 });
textLines.forEach((textLine) => {
    textLine.words.forEach((textWord) => {
        // Gets the bounds of the text word
        var wordBounds = textWord.bounds;
        // Gets the single word of extracted text from the PDF page
        var word = textWord.text;
        // Gets the collection of text glyphs extracted from a specified page in a PDF document
        var glyphs = textWord.glyphs;
        // Gets the name of the font used for a particular word
        var wordFontName = textWord.fontName;
        // Gets the style of the font used for a particular word
        var wordFontStyle = textWord.fontStyle;
        // Gets the size of the font used for a particular word
        var wordFontSize = textWord.fontSize;
    });
});
// Release document resources
document.destroy();

Working with characters

You can retrieve a single character and its properties, including bounds, font name, font size, and text color, using the extractTextLines method. Refer to the code sample below.

import { PdfDocument, PdfColor, Rectangle } from '@syncfusion/ej2-pdf';
import { PdfDataExtractor, TextLine, TextWord, TextGlyph, PdfFontStyle } from '@syncfusion/ej2-pdf-data-extract';

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
let extractor: PdfDataExtractor = new PdfDataExtractor(document);
// Extract `TextLine` objects from the PDF document
let textLines: Array<TextLine> = extractor.extractTextLines({ startPageIndex: 0, endPageIndex: document.pageCount - 1 });
textLines.forEach((textLine: TextLine) => {
    textLine.words.forEach((textWord: TextWord) => {
        textWord.glyphs.forEach((textGlyph: TextGlyph) => {
            // Gets the bounds of the text glyph
            let glyphBounds: Rectangle = textGlyph.bounds;
            // Gets the single character of extracted text from the PDF page
            let character: string = textGlyph.text;
            // Gets the font size used for a particular character of the text
            let fontSize: number = textGlyph.fontSize;
            // Gets the name of the font used for a particular character of the text
            let fontName: string = textGlyph.fontName;
            // Gets the font style used for a particular character of the text
            let fontStyle: PdfFontStyle = textGlyph.fontStyle;
            // Gets the text color of the text glyph
            let color: PdfColor = textGlyph.color;
            // Gets the value indicating whether the glyph is rotated or not
            let isRotated: boolean = textGlyph.isRotated;
        });
    });
});
// Release document resources
document.destroy();
// Load an existing PDF document
var document = new ej.pdf.PdfDocument(data);
// Initialize a new instance of the `PdfDataExtractor` class
var extractor = new ej.pdfdataextract.PdfDataExtractor(document);
// Extract `TextLine` objects from the PDF document
var textLines = extractor.extractTextLines({ startPageIndex: 0, endPageIndex: document.pageCount - 1 });
textLines.forEach((textLine) => {
    textLine.words.forEach((textWord) => {
        textWord.glyphs.forEach((textGlyph) => {
            // Gets the bounds of the text glyph
            var glyphBounds = textGlyph.bounds;
            // Gets the single character of extracted text from the PDF page
            var character = textGlyph.text;
            // Gets the font size used for a particular character of the text
            var fontSize = textGlyph.fontSize;
            // Gets the name of the font used for a particular character of the text
            var fontName = textGlyph.fontName;
            // Gets the font style used for a particular character of the text
            var fontStyle = textGlyph.fontStyle;
            // Gets the text color of the text glyph
            var color = textGlyph.color;
            // Gets the value indicating whether the glyph is rotated or not
            var isRotated = textGlyph.isRotated;
        });
    });
});
// Release document resources
document.destroy();

Additional Resources