Use text.Plain to read PDF text. The sample note.pdf says “Payment received.” Image-only scans need OCR first.
go get recursiveai.org/go/onepdfGo 1.24+ required (download: Go 1.26.8).
package main import ( "fmt" "log" "recursiveai.org/go/onepdf" "recursiveai.org/go/onepdf/text" ) func main() { doc, err := onepdf.OpenFile("note.pdf") if err != nil { log.Fatal(err) } defer doc.Close() plain, err := text.Plain(doc) if err != nil { log.Fatal(err) } fmt.Println(plain) }
The example is the full program below: unzip it and go run .. To run only the code above, paste it into main.go, run go mod init example and go get recursiveai.org/go/onepdf, then go run ..
go run . prints:
Payment receivedThe full program
The example adds text.Extract and walks page.Words() to print the position of each word from the same input.
package main import ( "fmt" "log" "recursiveai.org/go/onepdf" "recursiveai.org/go/onepdf/text" ) func main() { doc, err := onepdf.OpenFile("note.pdf") if err != nil { log.Fatal(err) } defer doc.Close() plain, err := text.Plain(doc) if err != nil { log.Fatal(err) } fmt.Println(plain) pages, err := text.Extract(doc) if err != nil { log.Fatal(err) } for i, page := range pages { for word := range page.Words() { box := word.Bounds fmt.Printf("page %d: %q [%.1f %.1f %.1f %.1f]\n", i+1, word.Text, box.X0, box.Y0, box.X1, box.Y1) } } }
go run . prints:
Payment received page 1: "Payment" [72.0 717.6 119.4 730.8] page 1: "received" [122.7 717.6 168.0 730.8]
The four numbers are [x0 y0 x1 y1]: left, bottom, right, and top in points, measured from the bottom left of the upright page. An image-only page can open successfully but yield no words; successful extraction isn’t evidence that there was a text layer.
Variations
Keep the text in the order it was drawn. Replace the text.Plain call with:
plain, err := text.Plain(doc, text.WithMode(text.ContentOrder))
It still prints Payment received for this one-line file. Drawing order can differ from the order a reader would follow on a page with multiple columns.
Related recipes
The short answer
Open the PDF with onepdf.OpenFile and call text.Plain(doc) to make the existing text searchable in your Go program. Use text.Extract(doc) if you also need page boundaries and the position of each word. If you only need the words and positions, skip the call to Plain; the example uses both calls to show the two forms of output.
How do I extract text from a scanned PDF?
Check whether the PDF already has a text layer. A scanned page might be an image on its own or an image with hidden text from an earlier OCR pass. text.Plain reads existing PDF text; it doesn’t recognize letters in pixels. If a scan has no text layer, run OCR (optical character recognition) first and use the OCR output or its resulting searchable PDF.
Try selecting a word in a viewer. If you can select and copy the text but the result is wrong, compare it with the document visually: a hidden OCR layer can contain recognition errors. Don’t treat an empty string as an unreadable-file error; check the Go error separately, then handle empty results at the document or page level.
How do I get the coordinates of words in a PDF?
Iterate page.Words() and keep each word.Bounds together with its page number. Plain text alone doesn’t tell you where the amounts in an invoice sat or whether a word appeared in a margin. For example, both words in the sample have a bottom edge of 717.6, but the second starts to the right of the first. You can use rectangles like these to find words in a particular area of a page.
The coordinates are from the upright PDF page, not pixels in a screenshot. If you’re placing a box on an image of the same page, scale the points to that image’s size and account for the image’s top-left origin. A rectangle is a location, not a field name: if you’re extracting a value next to a label, check both the word’s box and the text around it.