---
title: "Extract text from a PDF in Go"
url: https://recursiveai.org/onepdf/go/cookbook/extract-text-from-pdf-in-go/
description: "Read a PDF as plain text in Go and get each word’s page coordinates. Includes a runnable example and a sample PDF with a text layer."
version: "1.0.1"
---

# Extract text from a PDF in Go

**onepdf-go** is a free, open-source PDF library for Go from Recursive AI that reads, edits, signs, redacts and lays out PDFs. MIT or Apache-2.0, no third-party dependencies.

[pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onepdf) · [GitHub](https://github.com/recursive-ai-oss/onepdf-go) · [Docs](https://recursiveai.org/onepdf/go/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/go/cookbook/extract-text-from-pdf-in-go/extract-text-from-pdf-in-go.zip)

Use `text.Plain` to read PDF text. The sample `note.pdf` says “Payment received.” Image-only scans need OCR first.

```shell
go get recursiveai.org/go/onepdf
```

Go 1.24+ required (download: Go 1.26.8).

```go
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](https://recursiveai.org/onepdf/go/cookbook/extract-text-from-pdf-in-go/extract-text-from-pdf-in-go.zip) 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:

```text
Payment received
```

## The full program

The example adds `text.Extract` and walks `page.Words()` to print the position of each word from the same input.

```go
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:

```text
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:

```go
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

- [Create a PDF in Go](https://recursiveai.org/onepdf/go/cookbook/create-pdf-in-go/)
- [Merge PDF files in Go](https://recursiveai.org/onepdf/go/cookbook/merge-pdf-files-in-go/)

## 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)](https://en.wikipedia.org/wiki/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.
