Recursive AI

Redact a word 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.

Find “ALPHA” in input.pdf (“Status ALPHA pending”) with text.Extract, then remove it with redact.Apply.

Terminal
go get recursiveai.org/go/onepdf

In the unzipped folder, run go run . with Go 1.26.8.

GO
package main
 
import (
	"fmt"
	"log"
	"strings"
 
	"recursiveai.org/go/onepdf"
	"recursiveai.org/go/onepdf/redact"
	"recursiveai.org/go/onepdf/text"
)
 
func main() {
	doc := must(onepdf.OpenFile("input.pdf", onepdf.WithEditing()))
	defer doc.Close()
 
	const target = "ALPHA"
	pages := must(text.Extract(doc))
	var areas []redact.Region
	for index, page := range pages {
		for word := range page.Words() {
			if word.Text == target {
				areas = append(areas, redact.Region{Page: index, Rect: word.Bounds})
			}
		}
	}
	if len(areas) == 0 {
		log.Fatal("target word not found")
	}
	result := must(redact.Apply(doc, areas))
	if err := doc.SaveFile("redacted.pdf"); err != nil {
		log.Fatal(err)
	}
 
	saved := must(onepdf.OpenFile("redacted.pdf"))
	defer saved.Close()
	remaining := must(text.Plain(saved))
	if strings.Contains(remaining, target) {
		log.Fatal("target still found in page text")
	}
	fmt.Println("Wrote redacted.pdf")
	fmt.Println("Glyphs removed:", result.GlyphsRemoved)
	fmt.Println("Remaining words:", strings.Join(strings.Fields(remaining), " "))
}
 
func must[T any](value T, err error) T {
	if err != nil {
		log.Fatal(err)
	}
	return value
}

Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onepdf, then go run .; or unzip the example and go run ..

If you paste the code, copy input.pdf from the zip into your project folder, or put your own PDF there named input.pdf. For your own PDF, set target to a word it contains.

The program prints:

Text
Wrote redacted.pdf
Glyphs removed: 7
Remaining words: Status pending

The rewritten PDF keeps “Status” and “pending”; the count is glyphs removed, not words matched. If page content can’t be read completely, redact.Apply refuses to modify the document. Use a full save after redaction: an incremental save would retain the original bytes.

Variations

Mark the cleared area in black. Replace the redact.Apply line to paint the area only after removing the underlying content:

GO
black := redact.WithOverlay(redact.Overlay{})
result := must(redact.Apply(doc, areas, black))

The short answer

To redact a word in a PDF with Go, open the file with onepdf.WithEditing(), get its words and their rectangles with text.Extract, and pass every matching rectangle and page index to redact.Apply. Save to a new file with SaveFile. The sample starts with one known word, not a list of possible spellings or a guess at which data is sensitive.

Is it possible to permanently redact text in a PDF?

Yes. A black rectangle drawn over a PDF does not remove the text below it. That text may still be selectable or searchable. In this recipe, redact.Apply rewrites page content first. The optional black fill is a separate step; it makes the location of the redaction visible, but it is not what removes the text.

An incremental update would append the changes to the original, keeping the old content in the file. SaveFile makes a full rewrite instead and does not copy earlier revisions. Keep the source in a private location and send only the rewritten copy. A full rewrite also breaks existing digital signatures, so create a new signature on the final redacted file if you need one.

Why can’t I find the text I want to redact in my PDF?

First, see whether you can select the text in your viewer. In an image-only scan, the letters are pixels, not PDF text, so a word loop can’t locate them. For a scan you need the area from a visual review or an OCR workflow that supplies coordinates. redact.Apply takes a rectangle and a zero-based page index regardless of how you found it. Its coordinates are in the page’s default user space, before the page’s rotation is applied; don’t pass pixels measured from a screenshot directly.

If the text is selectable but the recipe finds nothing, inspect the tokens you’re matching. Exact equality will not find a substring, punctuation attached to a word, or a target split across tokens. Review the matched areas before writing the file, particularly where the target appears beside text that must remain.

Can I redact a PDF before sharing it without leaving other copies of the text?

Removing page content is not a whole-file search. The same text could be in document metadata, on another page, or inside an attached file. The redaction package offers separate options for metadata terms and for removing attachments, but the sample only matches page text and checks the saved page text. Review the saved copy and its other data before sharing it. A clean visible page alone is not evidence that every copy of the information is gone.

All onepdf Go recipes