Getting started
Activate a license, open a PDF, count its pages, render a page to PNG and extract its text with NexPDF for Go.
This example opens a PDF, prints the page count, renders the first page to a PNG file and prints the text of every page. It runs without a license key too: viewing is always allowed, and rendered pages then carry a watermark.
Install the SDK
go mod init quickstart
go get github.com/nexdevit/nexpdf-go@v1.1.0Set CGO_CFLAGS and CGO_LDFLAGS to the native library as described in
Installation.
Write the program
package main
import (
"fmt"
"log"
"os"
nexpdf "github.com/nexdevit/nexpdf-go"
)
func main() {
// 1. Activate the license once per process (optional for viewing).
if file := os.Getenv("NEXPDF_LICENSE"); file != "" {
key, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}
if _, err := nexpdf.ActivateLicense(string(key), "server", "com.acme.quickstart"); err != nil {
log.Fatal(err)
}
}
// 2. Open the document.
path := "sample.pdf"
if len(os.Args) > 1 {
path = os.Args[1]
}
doc, err := nexpdf.Open(path)
if err != nil {
log.Fatal(err)
}
defer doc.Close()
// 3. Page count and size.
count, err := doc.PageCount()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s: %d pages, first page %.0f x %.0f pt\n", path, count, doc.PageWidth(0), doc.PageHeight(0))
// 4. Render the first page at 144 dpi.
png, err := doc.RenderPagePNG(0, 144)
if err != nil {
log.Fatal(err)
}
if err := os.WriteFile("page1.png", png, 0o644); err != nil {
log.Fatal(err)
}
// 5. Extract the text of every page.
for i := 0; i < int(count); i++ {
text, err := doc.ExtractPageText(i)
if err != nil {
log.Fatal(err)
}
fmt.Printf("--- page %d ---\n%s\n", i+1, text)
}
}What the code does
nexpdf.ActivateLicense(keyOrXML, platform, appID)accepts the license XML or the bare key. The app id must be one the key was issued for. See License.nexpdf.Open(path, password...)opens a PDF. Word, Excel and PowerPoint files are converted when the license includesconversion; JPEG, PNG and TIFF pictures become one page each.PageWidth(i)andPageHeight(i)are the displayed size in points: crop box, rotation and/UserUnitapplied.RenderPagePNG(i, dpi)returns PNG bytes. 72 dpi is one pixel per point.ExtractPageText(i)returns the page text in reading order.