Skip to content
Reading and Extracting

Reading and Extracting

Reading a file starts with model.NewPdfReader, and everything that pulls content out of a page goes through the extractor package. You build an extractor for a single page and ask it for what you want:

ex, err := extractor.New(page)
if err != nil {
    return err
}

text, err := ex.ExtractText()

There is no document-level extractor, so anything covering a whole file is a loop over pages.

What comes back depends on the extraction mode, which is set through extractor.NewWithOptions. Layout mode is the default: it infers paragraphs, reading order and tables, and it is the only mode where Marks() and Tables() are populated. Plain mode reads the content streams in order without any of that inference, which makes it the fastest and often the most complete when layout mode duplicates or drops text. Grid mode places words on a fixed-width character grid, close to the output of pdftotext -layout.

A PDF stores glyphs at coordinates, not paragraphs and tables, so all of that structure is reconstructed rather than read. When the output looks wrong, the mode is the first thing to change.

Last updated on