NexPDF for Go
Render, extract, edit, convert, sign and validate PDFs from Go with the NexPDF engine, in-process through cgo.
NexPDF for Go (github.com/nexdevit/nexpdf-go, package nexpdf) calls the native NexPDF engine
in-process through cgo. There is no other Go dependency.
Every function of the C ABI is available. Functions that take a document are methods of
*nexpdf.Document; the others are package functions. Options and results are the same JSON strings
as in every NexPDF SDK, and you parse them with encoding/json.
package main
import (
"fmt"
"log"
nexpdf "github.com/nexdevit/nexpdf-go"
)
func main() {
doc, err := nexpdf.Open("contract.pdf")
if err != nil {
log.Fatal(err)
}
defer doc.Close()
n, _ := doc.PageCount()
fmt.Println(n, doc.PageWidth(0), doc.PageHeight(0))
text, _ := doc.ExtractPageText(0)
fmt.Println(text)
}Requirements
| Go | 1.21 or later, with CGO_ENABLED=1 and a C compiler |
| Operating systems | Linux (x86_64, arm64), macOS (Apple silicon and Intel) |
| Native library | libnexpdf_ffi.so or libnexpdf_ffi.dylib (shared), or libnexpdf_ffi.a (static), from the release |
| Dependencies | None besides the native library |
How it works
*Documentwraps a native document handle. Close it withClose(), usually withdefer; an unreachable document is also freed by a finalizer.Closeis safe to call more than once.- Every call returns
(value, error). Errors are*nexpdf.Errorwith the engine's message inMessage; a rejected license also sets the negativeCode. A method of a closed document returns an error. - Byte buffers are
[]byteand page lists[]uint32. A string parameter that is""is passed to the engine as NULL, its default (for example "keep this value" inSetMetadata). - Each call locks its OS thread for its duration, so the engine's per-thread error is always the one
of that call. A
*Documentis not safe for concurrent use: use one document from one goroutine at a time. Different documents can be used from different goroutines, anddoc.Copy()gives an independent copy.
Getting started
Installation
go get, cgo flags and native library placement.
Your first document
Open a PDF, render a page and extract its text.
Add your license key
Activate a key and handle license errors.
Running without a key
View-only mode, the watermark and trial keys.
Guides
Opening and saving
Files, bytes, passwords, pictures, HTML and Office input.
Rendering
Pages and thumbnails to PNG, coordinates.
Document structure
Metadata, bookmarks, page labels, layers, attachments.
Text extraction
Text, Markdown, positioned blocks, passages for LLMs.
Search
Find text and its rectangles.
OCR
Make scanned pages searchable, in any script.
Structured extraction
Tables, key-values, schema extraction with your LLM, classification.
Barcodes
Read QR, Data Matrix, PDF417 and linear codes; draw barcodes.
Annotations
Markup, shapes, ink, stamps, measurements, XFDF.
Forms
Fill, create, calculate, flatten, import and export.
XFA forms
Run FormCalc and JavaScript, lay out dynamic forms, convert to AcroForm, flatten.
Comment threads
Replies and review states on annotations.
Real-time collaboration
Sync pages, annotations, comments, form values and bookmarks.
Editing
Merge, split, reorder, crop; edit text and images.
Compare and optimise
Diff versions, shrink files, control memory.
Word-level comparison
Inserted, deleted, replaced and moved text; annotated PDF.
Security
Encryption, permissions, watermarks, Bates numbers.
Redaction
Remove content for good by area, text or pattern.
Signatures
PAdES signing, external signers, verification.
Long-term signatures
PAdES B-LTA, document and archive timestamps.
Conversion
PDF to Word, Excel, PowerPoint, HTML and more.
Templates
PDFs from Word, HTML and PDF templates and JSON data.
PDF/A, PDF/X and PDF/UA
Archiving, print and accessibility.
PDF/A and PDF/UA validation
PDF/A and PDF/UA-1 and -2 with clause ids; convert to PDF/A or PDF/UA.