Extract Text from PDFs in iOS

This guide shows how to extract the full text content from a PDF page or an entire PDF document.

If you need more granular control over text extraction, you can refer to our parsing guide, which outlines the available text APIs in greater detail.

Page Text

PSPDFKit’s TextParser class offers a convenient API to get the text of a given PDF page.

The code below will print out the text content of the first page of a PDF document:

let document = ...

// Access text parser for the first page and obtain the complete page text.
guard let parser = document.textParserForPage(at: 0) else {
    print("Text parsing failed.")
    return
}

// Do something with the text.
print(parser.text)

Document Text

You can use the same text API to obtain the textual contents of the entire document.

Note that text parsing can be expensive, especially for larger documents. For best results, invoke parsing on a background thread:

let document = ...

// Spin off a background task to not block the main thread.
DispatchQueue.global(qos: .utility).async {
    // Iterate over all pages and obtain the page text.
    let content: [String] = (0...document.pageCount).compactMap {
        guard let parser = document.textParserForPage(at: $0) else {
            print("Text parsing failed for page \($0).")
            return nil
        }
        return parser.text
    }

    // Do something with the text.
    print(content.joined(separator: "\n"))
}

Reader View

PSPDFKit also offers a built-in UI for viewing the textual content of a PDF in an easy-to-read, single-column view that’s optimized for mobile devices. To learn more about this component, check out the Reader View guide.