Documentation
¶
Index ¶
- func AddAnnotation(pdfBytes []byte, pageNumber int, cfg AnnotationConfig, password []byte, ...) ([]byte, error)
- func ConvertToPDFA(pdfBytes []byte, password []byte, conformance string) ([]byte, error)
- func CropPage(pdfBytes []byte, pageNumber int, rect [4]float64, password []byte, ...) ([]byte, error)
- func DecryptPDF(pdfBytes []byte, password []byte, verbose bool) ([]byte, error)
- func DeletePage(pdfBytes []byte, pageNumber int, password []byte, verbose bool) ([]byte, error)
- func DeletePages(pdfBytes []byte, pageNumbers []int, password []byte, verbose bool) ([]byte, error)
- func DuplicatePage(pdfBytes []byte, pageNumber, copies int, password []byte, verbose bool) ([]byte, error)
- func EncryptPDF(pdfBytes []byte, userPassword, ownerPassword []byte, verbose bool) ([]byte, error)
- func ExtractPages(pdfBytes []byte, pageNumbers []int, password []byte, verbose bool) ([]byte, error)
- func InsertBlankPage(pdfBytes []byte, position int, width, height float64, password []byte, ...) ([]byte, error)
- func Linearize(pdfBytes []byte, password []byte) ([]byte, error)
- func MergePDFs(pdfBytesList [][]byte, passwords [][]byte, verbose bool) ([]byte, error)
- func Redact(pdfBytes []byte, boxes []RedactBox, password []byte) ([]byte, error)
- func RedactMetadata(pdfBytes []byte, password []byte, verbose bool) ([]byte, error)
- func ReorderPages(pdfBytes []byte, order []int, password []byte, verbose bool) ([]byte, error)
- func Repair(pdfBytes []byte, password []byte) ([]byte, error)
- func ReplaceImage(pdfBytes []byte, imageRef string, newImageData []byte, format string, ...) ([]byte, error)
- func RotateAllPages(pdfBytes []byte, angle int, password []byte, verbose bool) ([]byte, error)
- func RotatePage(pdfBytes []byte, pageNumber, angle int, password []byte, verbose bool) ([]byte, error)
- func SetBookmarks(pdfBytes []byte, bookmarks []BookmarkEntry, password []byte, verbose bool) ([]byte, error)
- func SetMetadata(pdfBytes []byte, meta MetadataUpdate, password []byte, verbose bool) ([]byte, error)
- func SetPageSize(pdfBytes []byte, pageNumber int, width, height float64, password []byte, ...) ([]byte, error)
- func SplitPDF(pdfBytes []byte, pageRanges []PageRange, password []byte, verbose bool) ([][]byte, error)
- func SplitPDFByPageCount(pdfBytes []byte, pagesPerPDF int, password []byte, verbose bool) ([][]byte, error)
- func StampAllPages(pdfBytes []byte, stamp TextStamp, password []byte, verbose bool) ([]byte, error)
- func StampPageNumbers(pdfBytes []byte, opts PageNumberOptions, password []byte, verbose bool) ([]byte, error)
- func StampText(pdfBytes []byte, pageNumber int, stamp TextStamp, password []byte, ...) ([]byte, error)
- type AnnotationConfig
- type AnnotationType
- type BookmarkEntry
- type MetadataUpdate
- type PDFManipulator
- func (m *PDFManipulator) ApplyRedactions(boxes []RedactBox) error
- func (m *PDFManipulator) DeletePage(pageNumber int) error
- func (m *PDFManipulator) DeletePages(pageNumbers []int) error
- func (m *PDFManipulator) InsertPage(pageNumber int, pageObjNum int, pageContent []byte) error
- func (m *PDFManipulator) Rebuild() ([]byte, error)
- func (m *PDFManipulator) RotateAllPages(angle int) error
- func (m *PDFManipulator) RotatePage(pageNumber int, angle int) error
- func (m *PDFManipulator) StampAllPages(stamp TextStamp) error
- func (m *PDFManipulator) StampPageNumbers(opts PageNumberOptions) error
- func (m *PDFManipulator) StampText(pageNumber int, stamp TextStamp) error
- type PageNumberOptions
- type PageNumberPosition
- type PageRange
- type Permissions
- type RedactBox
- type TextStamp
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddAnnotation ¶ added in v1.5.0
func AddAnnotation(pdfBytes []byte, pageNumber int, cfg AnnotationConfig, password []byte, verbose bool) ([]byte, error)
AddAnnotation adds a single annotation to the given page (1-based) of pdfBytes.
func ConvertToPDFA ¶ added in v1.5.0
ConvertToPDFA converts a PDF to PDF/A format.
conformance selects the conformance level:
- "1b" → PDF/A-1b (default when empty)
- "2b" → PDF/A-2b
- "3b" → PDF/A-3b
PDF/A cannot be encrypted, so if the input PDF is encrypted it is first decrypted using password. The output is always unencrypted.
func CropPage ¶ added in v1.5.0
func CropPage(pdfBytes []byte, pageNumber int, rect [4]float64, password []byte, verbose bool) ([]byte, error)
CropPage sets /CropBox on the specified page (1-based). rect is [llx lly urx ury] in PDF user units (points).
func DecryptPDF ¶ added in v1.5.0
DecryptPDF produces a fully decrypted, plaintext PDF from an encrypted one. It verifies the password, decrypts every stream and string in the document, removes the /Encrypt dict and all encryption metadata, and returns clean bytes that can be opened by any PDF tool without a password.
Returns an error if the password is wrong or the PDF cannot be parsed. If the PDF is already unencrypted the original bytes are returned unchanged.
func DeletePage ¶ added in v1.5.0
DeletePage removes a single page from pdfBytes (1-based page number).
func DeletePages ¶ added in v1.5.0
DeletePages removes multiple pages from pdfBytes (1-based page numbers).
func DuplicatePage ¶ added in v1.5.0
func DuplicatePage(pdfBytes []byte, pageNumber, copies int, password []byte, verbose bool) ([]byte, error)
DuplicatePage inserts copies copies of pageNumber immediately after it (1-based).
func EncryptPDF ¶ added in v1.5.0
EncryptPDF applies AES-128 (V=4, R=4) encryption to an existing unencrypted PDF. It is the write-side counterpart to encrypt.DecryptPDF.
userPassword is required to open the document; ownerPassword grants full control. Either may be nil or empty (no password for that role).
Returns an error if the input PDF is already encrypted or cannot be parsed.
func ExtractPages ¶
func ExtractPages(pdfBytes []byte, pageNumbers []int, password []byte, verbose bool) ([]byte, error)
ExtractPages extracts specific pages from a PDF and returns a new PDF containing only those pages, with all their dependencies (fonts, images, content streams, etc.) fully copied and object references remapped. pageNumbers is 1-based.
func InsertBlankPage ¶ added in v1.5.0
func InsertBlankPage(pdfBytes []byte, position int, width, height float64, password []byte, verbose bool) ([]byte, error)
InsertBlankPage inserts a blank page at position (1-based; 0 = append at end). width and height are in PDF user units (points).
func Linearize ¶ added in v1.5.0
Linearize rewrites a PDF for fast web delivery ("Fast Web View"). It:
- flattens all incremental updates into a single revision
- reorders objects so the catalog and first page appear early in the file
- adds a /Linearized parameter dictionary as the first object
The /H (hint stream) field is omitted; PDF readers fall back to sequential reading gracefully when hints are absent. For encrypted PDFs supply the password; the output will be unencrypted (linearization requires plaintext).
func MergePDFs ¶
MergePDFs merges multiple PDFs into a single PDF Returns a new PDF with all pages from all input PDFs
func Redact ¶ added in v1.5.0
Redact permanently removes content within the specified regions and replaces each area with an opaque black rectangle. It:
- rewrites page content streams to suppress text/path operators inside boxes
- zeros out image XObject streams whose placement overlaps a box
- zeros out annotation objects (text, links, highlights, etc.) whose /Rect overlaps a box
XMP metadata and /Info dictionary entries are not cleared by this call; call RedactMetadata separately for document-level metadata.
Note: PDF incremental updates may contain prior revisions. Supply a clean (already-repaired/flattened) source PDF for full forensic redaction; or call Repair first.
func RedactMetadata ¶ added in v1.5.0
RedactMetadata removes document metadata from pdfBytes:
- Clears the /Info object (replaces its content with an empty dict).
- Removes the /Metadata stream from the document catalog.
- Drops the InfoRef from the rebuilt trailer.
func ReorderPages ¶ added in v1.5.0
ReorderPages reorders pages according to order, which is a 1-based slice of page numbers representing the desired output order. order must be a permutation of [1..pageCount]: same length as the current page count, each page number appearing exactly once.
func Repair ¶ added in v1.5.0
Repair rebuilds a PDF into a clean single-revision form:
- flattens all incremental updates into one revision
- removes orphaned cross-reference entries
- rebuilds the xref table from scratch
- preserves all content, resources, and metadata
It does not validate or fix content-stream operators. For encrypted PDFs supply the password; the output will be unencrypted.
func ReplaceImage ¶ added in v1.5.0
func ReplaceImage(pdfBytes []byte, imageRef string, newImageData []byte, format string, password []byte, verbose bool) ([]byte, error)
ReplaceImage replaces an image XObject in a PDF.
imageRef identifies the image either by its XObject resource name (e.g. "Im1", "Img0") or by the raw object number as a decimal string (e.g. "7").
format must be "jpeg", "png", or "raw".
- "jpeg": newImageData must be a JPEG file; the stream is stored with /Filter/DCTDecode.
- "png": newImageData must be a PNG file; the image is decoded and stored as raw deflate-compressed RGB pixels with /Filter/FlateDecode.
- "raw": the stream bytes are replaced as-is; the existing filter is preserved.
func RotateAllPages ¶ added in v1.5.0
RotateAllPages rotates every page in pdfBytes by angle degrees (90, 180, or 270).
func RotatePage ¶ added in v1.5.0
func RotatePage(pdfBytes []byte, pageNumber, angle int, password []byte, verbose bool) ([]byte, error)
RotatePage rotates a single page in pdfBytes by angle degrees (90, 180, or 270).
func SetBookmarks ¶ added in v1.5.0
func SetBookmarks(pdfBytes []byte, bookmarks []BookmarkEntry, password []byte, verbose bool) ([]byte, error)
SetBookmarks replaces all bookmarks in pdfBytes with the given tree. If bookmarks is empty or nil, all existing bookmarks are removed.
func SetMetadata ¶ added in v1.5.0
func SetMetadata(pdfBytes []byte, meta MetadataUpdate, password []byte, verbose bool) ([]byte, error)
SetMetadata updates the /Info dictionary of a PDF. For each field in meta that is non-empty the corresponding /Info entry is created or replaced. Fields that are empty strings are left unchanged.
func SetPageSize ¶ added in v1.5.0
func SetPageSize(pdfBytes []byte, pageNumber int, width, height float64, password []byte, verbose bool) ([]byte, error)
SetPageSize sets /MediaBox on the specified page (1-based). The new MediaBox is [0 0 width height].
func SplitPDF ¶
func SplitPDF(pdfBytes []byte, pageRanges []PageRange, password []byte, verbose bool) ([][]byte, error)
SplitPDF splits a PDF into multiple PDFs based on page ranges pageRanges is a slice of page number ranges, e.g., []PageRange{{1, 3}, {4, 5}} Returns a slice of PDF bytes, one for each range
func SplitPDFByPageCount ¶
func SplitPDFByPageCount(pdfBytes []byte, pagesPerPDF int, password []byte, verbose bool) ([][]byte, error)
SplitPDFByPageCount splits a PDF into multiple PDFs with a fixed number of pages each Returns a slice of PDF bytes
func StampAllPages ¶ added in v1.5.0
StampAllPages adds the same text overlay to every page of pdfBytes.
func StampPageNumbers ¶ added in v1.5.0
func StampPageNumbers(pdfBytes []byte, opts PageNumberOptions, password []byte, verbose bool) ([]byte, error)
StampPageNumbers adds automatic page numbers to all pages of pdfBytes.
Types ¶
type AnnotationConfig ¶ added in v1.5.0
type AnnotationConfig struct {
Type AnnotationType
Rect [4]float64 // [llx lly urx ury] in page coordinates
Contents string // visible text (Text, FreeText) or tooltip (Link)
// Link-specific: set URI for external links, or DestPage (1-based) for internal.
URI string
DestPage int
// Colour (RGB 0–1); zero value = default for type.
Color [3]float64
// Opacity 0–1; 0 means use default (fully opaque).
Opacity float64
}
AnnotationConfig describes an annotation to be added to a page.
type AnnotationType ¶ added in v1.5.0
type AnnotationType string
AnnotationType identifies the PDF annotation subtype.
const ( AnnotText AnnotationType = "Text" AnnotLink AnnotationType = "Link" AnnotFreeText AnnotationType = "FreeText" AnnotHighlight AnnotationType = "Highlight" AnnotUnderline AnnotationType = "Underline" AnnotStrikeOut AnnotationType = "StrikeOut" )
type BookmarkEntry ¶ added in v1.5.0
type BookmarkEntry struct {
Title string // displayed text
Page int // 1-based destination page (0 = no destination)
Children []BookmarkEntry // nested entries
}
BookmarkEntry describes one entry in the PDF outline (bookmark) tree.
type MetadataUpdate ¶ added in v1.5.0
type MetadataUpdate struct {
Title string
Author string
Subject string
Keywords string
Creator string
Producer string
}
MetadataUpdate holds the fields to update in the /Info dictionary. An empty string means "leave unchanged".
type PDFManipulator ¶
type PDFManipulator struct {
// contains filtered or unexported fields
}
PDFManipulator provides functions to modify existing PDFs
func NewPDFManipulator ¶
func NewPDFManipulator(pdfBytes []byte, password []byte, verbose bool) (*PDFManipulator, error)
NewPDFManipulator creates a new PDF manipulator from existing PDF bytes
func (*PDFManipulator) ApplyRedactions ¶ added in v1.5.0
func (m *PDFManipulator) ApplyRedactions(boxes []RedactBox) error
ApplyRedactions modifies the manipulator in-place, applying redaction to the specified page regions. Call Rebuild() afterwards to emit the modified PDF.
func (*PDFManipulator) DeletePage ¶
func (m *PDFManipulator) DeletePage(pageNumber int) error
DeletePage deletes a specific page from the PDF pageNumber is 1-based (first page is 1)
func (*PDFManipulator) DeletePages ¶
func (m *PDFManipulator) DeletePages(pageNumbers []int) error
DeletePages deletes multiple pages from the PDF pageNumbers is 1-based and should be sorted in descending order to avoid index shifts
func (*PDFManipulator) InsertPage ¶
func (m *PDFManipulator) InsertPage(pageNumber int, pageObjNum int, pageContent []byte) error
InsertPage inserts a page at a specific position pageNumber is 1-based (first page is 1, 0 means insert at end) The page to insert must be from another PDF (use ExtractPages first)
func (*PDFManipulator) Rebuild ¶
func (m *PDFManipulator) Rebuild() ([]byte, error)
Rebuild rebuilds the PDF with modified objects and returns the new PDF bytes
func (*PDFManipulator) RotateAllPages ¶
func (m *PDFManipulator) RotateAllPages(angle int) error
RotateAllPages rotates all pages by the given angle
func (*PDFManipulator) RotatePage ¶
func (m *PDFManipulator) RotatePage(pageNumber int, angle int) error
RotatePage rotates a specific page by the given angle (90, 180, or 270 degrees) pageNumber is 1-based (first page is 1)
func (*PDFManipulator) StampAllPages ¶ added in v1.5.0
func (m *PDFManipulator) StampAllPages(stamp TextStamp) error
StampAllPages adds the same text overlay to every page.
func (*PDFManipulator) StampPageNumbers ¶ added in v1.5.0
func (m *PDFManipulator) StampPageNumbers(opts PageNumberOptions) error
StampPageNumbers adds page numbers to all pages.
type PageNumberOptions ¶ added in v1.5.0
type PageNumberOptions struct {
FontName string // standard font; default "Helvetica"
FontSize float64 // default 10
Format string // printf-style; one %d = page, two %d = (page, total); default "%d"
Position PageNumberPosition // default BottomCenter
MarginX float64 // extra inset from page edge, default 36 pt
MarginY float64 // extra inset from page edge, default 36 pt
R, G, B float64 // fill colour, 0–1 each; default black
}
PageNumberOptions configures automatic page-number stamping.
type PageNumberPosition ¶ added in v1.5.0
type PageNumberPosition int
PageNumberPosition controls where page numbers are placed.
const ( BottomCenter PageNumberPosition = iota BottomRight BottomLeft TopCenter TopRight TopLeft )
type PageRange ¶
type PageRange struct {
Start int // First page number (1-based)
End int // Last page number (1-based, inclusive)
}
PageRange represents a range of pages (1-based, inclusive)
type Permissions ¶ added in v1.5.0
type Permissions struct {
Print bool
Modify bool
Copy bool
AddAnnotations bool
FillForms bool
ExtractForAccess bool
Assemble bool
PrintHighQuality bool
}
Permissions describes the permission flags encoded in the /P field of an encrypted PDF's /Encrypt dictionary.
func GetPermissions ¶ added in v1.5.0
func GetPermissions(pdfBytes []byte, password []byte) (*Permissions, error)
GetPermissions returns the permission flags for pdfBytes. For unencrypted PDFs all flags are returned as true. For encrypted PDFs the /P integer from the /Encrypt dictionary is decoded per the PDF specification.
type RedactBox ¶ added in v1.5.0
type RedactBox struct {
Page int // 1-indexed page number
Rect [4]float64 // [llx lly urx ury] in PDF user space (points from bottom-left)
}
RedactBox specifies a rectangular region to redact on a particular page.
type TextStamp ¶ added in v1.5.0
type TextStamp struct {
Text string // text to render
FontName string // standard Type1 font name; default "Helvetica"
FontSize float64 // in points; default 12
X, Y float64 // position in points from bottom-left of the page
R, G, B float64 // fill colour, 0–1 each; default black
Angle float64 // rotation in degrees, counter-clockwise; default 0
Opacity float64 // 0 = transparent, 1 = opaque; default 1
}
TextStamp describes a text overlay to paint onto a page.