How to Load a Document in Go
Aspose.PDF FOSS for Go loads a PDF from a file path, an io.Reader, or either source protected by a password, always returning a *Document value to operate on. The library is a Go module installed with go get.
Step-by-Step Guide
Step 1: Install the Package
Add the module to your project:
go get github.com/aspose-pdf-foss/aspose-pdf-foss-for-goVerify the module resolves:
import asposepdf "github.com/aspose-pdf-foss/aspose-pdf-foss-for-go"Step 2: Import Required Classes
import (
pdf "github.com/aspose-pdf-foss/aspose-pdf-foss-for-go"
)Step 3: Load a PDF from a File Path
pdf.Open loads a PDF file and returns a *Document and an error:
doc, err := pdf.Open("testdata/4pages.pdf")
if err != nil {
panic(err)
}
fmt.Println(doc.PageCount(), "pages")Step 4: Load a Password-Protected PDF
pdf.OpenWithPassword opens a file that is encrypted with a user password:
doc, err := pdf.OpenWithPassword("locked.pdf", "secret")
if err != nil {
panic(err)
}
fmt.Println("opened:", doc.PageCount(), "pages")Step 5: Load a PDF from a Stream
pdf.OpenStream reads a PDF from any io.Reader — a network response, an in-memory buffer, or a file opened separately. pdf.OpenStreamWithPassword is the password-protected equivalent for streams:
f, err := os.Open("input.pdf")
if err != nil {
panic(err)
}
defer f.Close()
doc, err := pdf.OpenStream(f)
if err != nil {
panic(err)
}
fmt.Println(doc.PageCount(), "pages")
// For an encrypted stream, open a separate reader and pass a password:
f2, _ := os.Open("locked.pdf")
defer f2.Close()
doc2, err := pdf.OpenStreamWithPassword(f2, "secret")
if err != nil {
panic(err)
}
fmt.Println(doc2.PageCount(), "pages (encrypted)")Step 6: Inspect the Loaded Document
Once loaded, Document.PageCount reports the total number of pages and Document.Page(n) retrieves a single page by its 1-based index:
doc, err := pdf.Open("testdata/4pages.pdf")
if err != nil {
panic(err)
}
fmt.Printf("Page count: %d\n", doc.PageCount())
page, err := doc.Page(1)
if err != nil {
panic(err)
}
size, err := page.Size()
if err != nil {
panic(err)
}
fmt.Printf("Page 1 size: %.0f x %.0f pt\n", size.Width, size.Height)Common Issues and Fixes
OpenWithPassword still fails after I enter what I believe is the correct password
Providing an incorrect password to OpenWithPassword results in a non-nil error. Passwords are case-sensitive — confirm the exact user password set on the source PDF.
OpenStream returns an error on an encrypted PDF
OpenStream cannot open encrypted PDFs. Use OpenStreamWithPassword for password-protected streams instead.
Do I need OpenWithPassword for a PDF that turns out not to be encrypted?
No — opening an unencrypted file through OpenWithPassword is allowed and behaves the same as a plain Open call, so it is safe to use OpenWithPassword when you are not certain a file is encrypted.
Document.Page(n) returns an error after a successful Open
The page number is out of range. Page uses a 1-based index between 1 and Document.PageCount(); call PageCount() first to validate the range.
Frequently Asked Questions
What file formats can I load with Open?
Open, OpenWithPassword, OpenStream, and OpenStreamWithPassword all accept PDF input exclusively.
How do I load a PDF that is already in memory instead of on disk?
Wrap the byte slice in a bytes.Reader (which satisfies io.Reader) and pass it to pdf.OpenStream, or pdf.OpenStreamWithPassword if the bytes represent an encrypted PDF.
How do I load a password-protected PDF?
Call pdf.OpenWithPassword(path, password) for a file path, or pdf.OpenStreamWithPassword(r, password) for a stream.
How do I check how many pages were loaded?
Call Document.PageCount() on the returned *Document.