Skip to content

Latest commit

 

History

History
1116 lines (837 loc) · 33.7 KB

File metadata and controls

1116 lines (837 loc) · 33.7 KB

GoPDF2 API Reference

English | 中文

Package gopdfgithub.com/VantageDataChat/GoPDF2


Types

GoPdf

The main struct for creating PDF documents.

type GoPdf struct { /* unexported fields */ }

Config

type Config struct {
    Unit              int                 // UnitPT, UnitMM, UnitCM, UnitIN, UnitPX
    ConversionForUnit float64             // Custom conversion factor (overrides Unit)
    TrimBox           Box                 // Default trim box for all pages
    PageSize          Rect                // Default page size
    Protection        PDFProtectionConfig // Password protection settings
}

Rect

type Rect struct {
    W float64 // Width
    H float64 // Height
}

Point

type Point struct {
    X float64
    Y float64
}

CellOption

type CellOption struct {
    Align        int            // Left|Center|Right|Top|Bottom|Middle
    Border       int            // Left|Top|Right|Bottom|AllBorders
    Float        int            // Right|Bottom
    Transparency *Transparency
    BreakOption  *BreakOption
}

HTMLBoxOption

type HTMLBoxOption struct {
    DefaultFontFamily    string    // Required. Font family when HTML does not specify one.
    DefaultFontSize      float64   // Default font size in points (default 12).
    DefaultColor         [3]uint8  // Default text color {R, G, B}.
    LineSpacing          float64   // Extra line spacing in document units.
    BoldFontFamily       string    // Font family for <b>/<strong>.
    ItalicFontFamily     string    // Font family for <i>/<em>.
    BoldItalicFontFamily string    // Font family for bold+italic.
}

TtfOption

type TtfOption struct {
    UseKerning                bool
    Style                     int               // Regular|Bold|Italic
    OnGlyphNotFound           func(r rune)      // Debug callback when glyph is missing
    OnGlyphNotFoundSubstitute func(r rune) rune // Substitution callback
}

Transparency

type Transparency struct {
    Alpha         float64       // 0.0 (fully transparent) to 1.0 (opaque)
    BlendModeType BlendModeType // e.g. "/Normal", "/Multiply", etc.
}

Predefined Page Sizes

PageSizeA0PageSizeA10, PageSizeA3LandscapePageSizeA10Landscape, PageSizeB0PageSizeB10, PageSizeLetter, PageSizeLetterLandscape, PageSizeLegal, PageSizeLegalLandscape, PageSizeTabloid, PageSizeLedger, PageSizeStatement, PageSizeExecutive, PageSizeFolio, PageSizeQuarto

Unit Constants

UnitPT, UnitMM, UnitCM, UnitIN, UnitPX

Font Style Constants

Regular (0), Italic (1), Bold (2), Underline (4)

Alignment Constants

Left, Right, Top, Bottom, Center, Middle


Document Lifecycle

func (gp *GoPdf) Start(config Config)
func (gp *GoPdf) AddPage()
func (gp *GoPdf) AddPageWithOption(opt PageOption)
func (gp *GoPdf) WritePdf(pdfPath string) error
func (gp *GoPdf) Write(w io.Writer) error
func (gp *GoPdf) WriteTo(w io.Writer) (int64, error)
func (gp *GoPdf) GetBytesPdf() []byte
func (gp *GoPdf) GetBytesPdfReturnErr() ([]byte, error)

Font Management

func (gp *GoPdf) AddTTFFont(family string, ttfpath string) error
func (gp *GoPdf) AddTTFFontWithOption(family string, ttfpath string, option TtfOption) error
func (gp *GoPdf) AddTTFFontByReader(family string, rd io.Reader) error
func (gp *GoPdf) AddTTFFontData(family string, fontData []byte) error
func (gp *GoPdf) SetFont(family string, style string, size interface{}) error
func (gp *GoPdf) SetFontWithStyle(family string, style int, size interface{}) error
func (gp *GoPdf) SetFontSize(fontSize float64) error
func (gp *GoPdf) KernOverride(family string, fn FuncKernOverride) error

Fonts are automatically subsetted — only glyphs used in the document are embedded.


Text

func (gp *GoPdf) Cell(rectangle *Rect, text string) error
func (gp *GoPdf) CellWithOption(rectangle *Rect, text string, opt CellOption) error
func (gp *GoPdf) MultiCell(rectangle *Rect, text string) error
func (gp *GoPdf) MultiCellWithOption(rectangle *Rect, text string, opt CellOption) error
func (gp *GoPdf) Text(text string) error
func (gp *GoPdf) MeasureTextWidth(text string) (float64, error)
func (gp *GoPdf) MeasureCellHeightByText(text string) (float64, error)
func (gp *GoPdf) SplitText(text string, width float64) ([]string, error)
func (gp *GoPdf) SplitTextWithWordWrap(text string, width float64) ([]string, error)
func (gp *GoPdf) IsFitMultiCell(rectangle *Rect, text string) (bool, float64, error)

HTML Rendering

func (gp *GoPdf) InsertHTMLBox(x, y, w, h float64, htmlStr string, opt HTMLBoxOption) (float64, error)

Render HTML content into a rectangular area on the PDF page.

Parameters: x, y (top-left corner), w, h (box size), htmlStr (HTML string), opt (rendering options).

Returns: the Y position after the last rendered content.

Supported tags: <b>, <strong>, <i>, <em>, <u>, <br>, <p>, <div>, <h1>-<h6>, <font>, <span>, <img>, <ul>, <ol>, <li>, <hr>, <center>, <a>, <blockquote>, <sub>, <sup>

Supported inline CSS: color, font-size, font-family, font-weight, font-style, text-decoration, text-align

Color formats: #RGB, #RRGGBB, rgb(r,g,b), CSS named colors

Font size formats: 12pt, 16px, 1.5em, 150%, named sizes (small, medium, large, etc.)


Position

func (gp *GoPdf) SetX(x float64)
func (gp *GoPdf) GetX() float64
func (gp *GoPdf) SetY(y float64)
func (gp *GoPdf) GetY() float64
func (gp *GoPdf) SetXY(x, y float64)
func (gp *GoPdf) Br(h float64)
func (gp *GoPdf) SetNewY(y float64, h float64)
func (gp *GoPdf) SetNewYIfNoOffset(y float64, h float64)
func (gp *GoPdf) SetNewXY(y float64, x, h float64)

Color

func (gp *GoPdf) SetTextColor(r, g, b uint8)
func (gp *GoPdf) SetTextColorCMYK(c, m, y, k uint8)
func (gp *GoPdf) SetStrokeColor(r, g, b uint8)
func (gp *GoPdf) SetFillColor(r, g, b uint8)
func (gp *GoPdf) SetGrayFill(grayScale float64)
func (gp *GoPdf) SetGrayStroke(grayScale float64)

Drawing

func (gp *GoPdf) Line(x1, y1, x2, y2 float64)
func (gp *GoPdf) Oval(x1, y1, x2, y2 float64)
func (gp *GoPdf) Polygon(points []Point, style string)
func (gp *GoPdf) Polyline(points []Point)
func (gp *GoPdf) Sector(cx, cy, r, startDeg, endDeg float64, style string)
func (gp *GoPdf) Rectangle(x0, y0, x1, y1 float64, style string, radius float64, radiusPointNum int) error
func (gp *GoPdf) RectFromUpperLeft(x, y, w, h float64)
func (gp *GoPdf) RectFromUpperLeftWithStyle(x, y, w, h float64, style string)
func (gp *GoPdf) Curve(x0, y0, x1, y1, x2, y2, x3, y3 float64, style string)
func (gp *GoPdf) SetLineWidth(width float64)
func (gp *GoPdf) SetLineType(linetype string)

Polyline draws an open path through a series of points (stroke only, path is not closed). Sector draws a pie/fan shape defined by center, radius, and start/end angles in degrees (counter-clockwise from positive X axis). Style: "D" (stroke), "F" (fill), "DF"/"FD" (both).


Image

func (gp *GoPdf) Image(picPath string, x, y float64, rect *Rect) error
func (gp *GoPdf) ImageFrom(img image.Image, x, y float64, rect *Rect) error
func (gp *GoPdf) ImageByHolder(img ImageHolder, x, y float64, rect *Rect) error
func (gp *GoPdf) ImageByHolderWithOptions(img ImageHolder, opts ImageOptions) error
func ImageHolderByPath(path string) (ImageHolder, error)
func ImageHolderByBytes(b []byte) (ImageHolder, error)
func ImageHolderByReader(r io.Reader) (ImageHolder, error)

Rotation & Transparency

func (gp *GoPdf) Rotate(angle, x, y float64)
func (gp *GoPdf) RotateReset()
func (gp *GoPdf) SetTransparency(transparency Transparency) error
func (gp *GoPdf) ClearTransparency()

Margins

func (gp *GoPdf) SetMargins(left, top, right, bottom float64)
func (gp *GoPdf) Margins() (float64, float64, float64, float64)

Links, Anchors, Header, Footer

func (gp *GoPdf) AddExternalLink(url string, x, y, w, h float64)
func (gp *GoPdf) AddInternalLink(anchor string, x, y, w, h float64)
func (gp *GoPdf) SetAnchor(name string)
func (gp *GoPdf) AddHeader(f func())
func (gp *GoPdf) AddFooter(f func())

Import Existing PDF

func (gp *GoPdf) ImportPage(sourceFile string, pageno int, box string) int
func (gp *GoPdf) UseImportedTemplate(tplid int, x, y, w, h float64)
func (gp *GoPdf) ImportPagesFromSource(source interface{}, box string) error

Open and Modify Existing PDF

func (gp *GoPdf) OpenPDF(pdfPath string, opt *OpenPDFOption) error
func (gp *GoPdf) OpenPDFFromBytes(pdfData []byte, opt *OpenPDFOption) error
func (gp *GoPdf) OpenPDFFromStream(rs *io.ReadSeeker, opt *OpenPDFOption) error

Open an existing PDF and import all pages so that new content can be drawn on top of them. The original page content is preserved as a background; any drawing method (Text, Cell, Image, InsertHTMLBox, Line, etc.) can then overlay new content.

After calling OpenPDF, use SetPage(n) (1-based) to switch pages, draw content, then call WritePdf to save.

OpenPDFOption

type OpenPDFOption struct {
    Box        string               // PDF box: "/MediaBox" (default), "/CropBox", etc.
    Protection *PDFProtectionConfig // Optional password protection for output.
}

Placeholder Text

func (gp *GoPdf) PlaceHolderText(placeHolderName string, placeHolderWidth float64) error
func (gp *GoPdf) FillInPlaceHoldText(placeHolderName string, text string, align int) error

Page Management & Unit Conversion

func (gp *GoPdf) GetNumberOfPages() int
func (gp *GoPdf) SetPage(pageno int) error
func (gp *GoPdf) UnitsToPoints(u float64) float64
func (gp *GoPdf) PointsToUnits(u float64) float64

Miscellaneous

func (gp *GoPdf) SetInfo(info PdfInfo)
func (gp *GoPdf) SetCompressLevel(level int)
func (gp *GoPdf) SetCharSpacing(charSpacing float64) error
func (gp *GoPdf) IsCurrFontContainGlyph(r rune) (bool, error)
func (gp *GoPdf) SaveGraphicsState()
func (gp *GoPdf) RestoreGraphicsState()
func (gp *GoPdf) AddOutline(title string)

FontContainer (Reusable Font Cache)

type FontContainer struct { /* ... */ }
func (fc *FontContainer) AddTTFFont(family string, ttfpath string) error
func (fc *FontContainer) AddTTFFontByReader(family string, rd io.Reader) error
func (fc *FontContainer) AddTTFFontData(family string, fontData []byte) error
func (gp *GoPdf) AddTTFFontFromFontContainer(family string, container *FontContainer) error

Pre-parse fonts once and reuse across multiple GoPdf instances for better performance.


Watermark

func (gp *GoPdf) AddWatermarkText(opt WatermarkOption) error
func (gp *GoPdf) AddWatermarkImage(imgPath string, opacity float64, imgW, imgH float64, angle float64) error
func (gp *GoPdf) AddWatermarkTextAllPages(opt WatermarkOption) error
func (gp *GoPdf) AddWatermarkImageAllPages(imgPath string, opacity float64, imgW, imgH float64, angle float64) error

WatermarkOption

type WatermarkOption struct {
    Text           string    // Watermark text (required)
    FontFamily     string    // Font family (required, must be pre-loaded)
    FontSize       float64   // Font size in points (default 48)
    Angle          float64   // Rotation angle in degrees (default 45)
    Color          [3]uint8  // RGB color (default: light gray)
    Opacity        float64   // 0.0–1.0 (default 0.3)
    Repeat         bool      // Tile across the page
    RepeatSpacingX float64   // Horizontal spacing for tiling (default 150)
    RepeatSpacingY float64   // Vertical spacing for tiling (default 150)
}

Annotations

func (gp *GoPdf) AddAnnotation(opt AnnotationOption)
func (gp *GoPdf) AddTextAnnotation(x, y float64, title, content string)
func (gp *GoPdf) AddHighlightAnnotation(x, y, w, h float64, color [3]uint8)
func (gp *GoPdf) AddFreeTextAnnotation(x, y, w, h float64, text string, fontSize float64)

AnnotationOption

type AnnotationOption struct {
    Type         AnnotationType // AnnotText, AnnotHighlight, AnnotUnderline, AnnotStrikeOut, AnnotSquare, AnnotCircle, AnnotFreeText
    X, Y, W, H  float64        // Annotation rectangle in document units
    Title        string         // Author name (for sticky notes)
    Content      string         // Annotation text content
    Color        [3]uint8       // RGB color (default: yellow)
    Opacity      float64        // 0.0–1.0 (default 1.0)
    Open         bool           // Initially open popup (text annotations)
    FontSize     float64        // Font size for FreeText (default 12)
}

Page Manipulation

func (gp *GoPdf) DeletePage(pageNo int) error
func (gp *GoPdf) DeletePages(pages []int) error
func (gp *GoPdf) CopyPage(pageNo int) (int, error)
func (gp *GoPdf) MovePage(from, to int) error
func ExtractPages(pdfPath string, pages []int, opt *OpenPDFOption) (*GoPdf, error)
func ExtractPagesFromBytes(pdfData []byte, pages []int, opt *OpenPDFOption) (*GoPdf, error)
func MergePages(pdfPaths []string, opt *OpenPDFOption) (*GoPdf, error)
func MergePagesFromBytes(pdfDataSlices [][]byte, opt *OpenPDFOption) (*GoPdf, error)

DeletePages removes multiple pages in a single operation. Pages are 1-based; duplicates are ignored; pages are deleted in reverse order to maintain correct numbering. Cannot delete all pages.

MovePage moves a page from one position to another by reordering via SelectPages.


Page Inspection

func (gp *GoPdf) GetPageSize(pageNo int) (w, h float64, err error)
func (gp *GoPdf) GetAllPageSizes() []PageInfo
func GetSourcePDFPageCount(pdfPath string) (int, error)
func GetSourcePDFPageCountFromBytes(pdfData []byte) (int, error)
func GetSourcePDFPageSizes(pdfPath string) (map[int]PageInfo, error)
func GetSourcePDFPageSizesFromBytes(pdfData []byte) (map[int]PageInfo, error)

PageInfo

type PageInfo struct {
    Width      float64 // Page width in points
    Height     float64 // Page height in points
    PageNumber int     // 1-based page number
}

Paper Size Lookup

func PaperSize(name string) *Rect
func PaperSizeNames() []string

Look up predefined paper sizes by name (case-insensitive). Supported names: a0a10, b0b10, letter, legal, tabloid, ledger, statement, executive, folio, quarto. Append -l for landscape (e.g. a4-l, letter-l). Returns a copy of the Rect; returns nil if name is not recognized.


Page Rotation

func (gp *GoPdf) SetPageRotation(pageNo int, angle int) error
func (gp *GoPdf) GetPageRotation(pageNo int) (int, error)

Set or get the display rotation for a page. angle must be a multiple of 90 (0, 90, 180, 270). This sets the /Rotate entry in the page dictionary — it tells PDF viewers how to display the page but does not modify the page content.


Page CropBox

func (gp *GoPdf) SetPageCropBox(pageNo int, box Box) error
func (gp *GoPdf) GetPageCropBox(pageNo int) (*Box, error)
func (gp *GoPdf) ClearPageCropBox(pageNo int) error

Set, get, or remove the CropBox for a page. The CropBox defines the visible area of the page when displayed or printed. Content outside the CropBox is clipped (hidden) but not removed.

pageNo is 1-based. Box coordinates are in document units (PDF coordinate system where (0,0) is bottom-left).

GetPageCropBox returns nil if no CropBox is set. ClearPageCropBox removes the CropBox, restoring the full MediaBox as the visible area.


Page Reordering

func (gp *GoPdf) SelectPages(pages []int) (*GoPdf, error)
func SelectPagesFromFile(pdfPath string, pages []int, opt *OpenPDFOption) (*GoPdf, error)
func SelectPagesFromBytes(pdfData []byte, pages []int, opt *OpenPDFOption) (*GoPdf, error)

Rearrange pages in a new document. Pages are 1-based and may be repeated. The current document is exported to bytes, then only the selected pages are re-imported in the specified order.


Embedded Files

func (gp *GoPdf) AddEmbeddedFile(ef EmbeddedFile) error

Attach a file to the PDF document. The file appears in the PDF viewer's attachment panel.

EmbeddedFile

type EmbeddedFile struct {
    Name        string    // Display name (required)
    Content     []byte    // File content (required)
    MimeType    string    // MIME type (e.g. "text/plain", "application/pdf")
    Description string    // Optional description
    ModDate     time.Time // Modification date (default: now)
}

Geometry

RectFrom

type RectFrom struct {
    X, Y, W, H float64
}

func (r RectFrom) Contains(px, py float64) bool
func (r RectFrom) ContainsRect(other RectFrom) bool
func (r RectFrom) Intersects(other RectFrom) bool
func (r RectFrom) Intersection(other RectFrom) RectFrom
func (r RectFrom) Union(other RectFrom) RectFrom
func (r RectFrom) IsEmpty() bool
func (r RectFrom) Area() float64
func (r RectFrom) Center() Point
func (r RectFrom) Normalize() RectFrom

A positioned rectangle with geometric operations.

Matrix

type Matrix struct {
    A, B, C, D, E, F float64
}

func IdentityMatrix() Matrix
func TranslateMatrix(tx, ty float64) Matrix
func ScaleMatrix(sx, sy float64) Matrix
func RotateMatrix(degrees float64) Matrix
func (m Matrix) Multiply(other Matrix) Matrix
func (m Matrix) TransformPoint(x, y float64) (float64, float64)
func (m Matrix) IsIdentity() bool

2D affine transformation matrix. Transformation: x' = a*x + c*y + e, y' = b*x + d*y + f.

Distance

func Distance(p1, p2 Point) float64

Euclidean distance between two points.


Content Element CRUD

ContentElementType

type ContentElementType int

const (
    ElementText             ContentElementType
    ElementImage            ContentElementType
    ElementLine             ContentElementType
    ElementRectangle        ContentElementType
    ElementOval             ContentElementType
    ElementPolygon          ContentElementType
    ElementCurve            ContentElementType
    ElementPolyline         ContentElementType
    ElementSector           ContentElementType
    ElementImportedTemplate ContentElementType
    ElementLineWidth        ContentElementType
    ElementLineType         ContentElementType
    // ... and more (ColorRGB, ColorCMYK, Rotate, etc.)
    ElementUnknown          ContentElementType
)

func (t ContentElementType) String() string

ContentElement

type ContentElement struct {
    Index    int                // 0-based position in the page's content cache
    Type     ContentElementType // Element type
    X, Y     float64           // Primary position
    X2, Y2   float64           // Secondary position (lines, ovals)
    Width    float64           // Width (rectangles, images)
    Height   float64           // Height (rectangles, images)
    Text     string            // Text content (text elements only)
    FontSize float64           // Font size (text elements only)
}

Query Methods

func (gp *GoPdf) GetPageElements(pageNo int) ([]ContentElement, error)
func (gp *GoPdf) GetPageElementsByType(pageNo int, elemType ContentElementType) ([]ContentElement, error)
func (gp *GoPdf) GetPageElementCount(pageNo int) (int, error)

Delete Methods

func (gp *GoPdf) DeleteElement(pageNo int, elementIndex int) error
func (gp *GoPdf) DeleteElementsByType(pageNo int, elemType ContentElementType) (int, error)
func (gp *GoPdf) DeleteElementsInRect(pageNo int, rx, ry, rw, rh float64) (int, error)
func (gp *GoPdf) ClearPage(pageNo int) error

Modify Methods

func (gp *GoPdf) ModifyTextElement(pageNo int, elementIndex int, newText string) error
func (gp *GoPdf) ModifyElementPosition(pageNo int, elementIndex int, x, y float64) error

Insert Methods

func (gp *GoPdf) InsertLineElement(pageNo int, x1, y1, x2, y2 float64) error
func (gp *GoPdf) InsertRectElement(pageNo int, x, y, w, h float64, style string) error
func (gp *GoPdf) InsertOvalElement(pageNo int, x1, y1, x2, y2 float64) error
func (gp *GoPdf) InsertElementAt(pageNo int, elementIndex int, newElement ICacheContent) error
func (gp *GoPdf) ReplaceElement(pageNo int, elementIndex int, newElement ICacheContent) error

PDF Version Control

type PDFVersion int

const (
    PDFVersion14 PDFVersion = 14 // PDF 1.4
    PDFVersion15 PDFVersion = 15 // PDF 1.5
    PDFVersion16 PDFVersion = 16 // PDF 1.6
    PDFVersion17 PDFVersion = 17 // PDF 1.7 (default)
    PDFVersion20 PDFVersion = 20 // PDF 2.0
)

func (v PDFVersion) String() string  // "1.7"
func (v PDFVersion) Header() string  // "%PDF-1.7"

func (gp *GoPdf) SetPDFVersion(v PDFVersion)
func (gp *GoPdf) GetPDFVersion() PDFVersion

Garbage Collection

type GarbageCollectLevel int

const (
    GCNone    GarbageCollectLevel = 0 // No-op
    GCCompact GarbageCollectLevel = 1 // Remove null objects, renumber
    GCDedup   GarbageCollectLevel = 2 // Also deduplicate identical objects
)

func (gp *GoPdf) GarbageCollect(level GarbageCollectLevel) int
func (gp *GoPdf) GetObjectCount() int
func (gp *GoPdf) GetLiveObjectCount() int

GarbageCollect removes null/deleted objects left by DeletePage and other operations. Returns the number of objects removed.


Page Labels

type PageLabelStyle string

const (
    PageLabelDecimal    PageLabelStyle = "D" // 1, 2, 3, ...
    PageLabelRomanUpper PageLabelStyle = "R" // I, II, III, ...
    PageLabelRomanLower PageLabelStyle = "r" // i, ii, iii, ...
    PageLabelAlphaUpper PageLabelStyle = "A" // A, B, C, ...
    PageLabelAlphaLower PageLabelStyle = "a" // a, b, c, ...
    PageLabelNone       PageLabelStyle = ""  // No numbering
)

type PageLabel struct {
    PageIndex int            // 0-based page index where this range starts
    Style     PageLabelStyle // Numbering style
    Prefix    string         // Optional prefix (e.g. "Appendix ")
    Start     int            // Starting number (default 1)
}

func (gp *GoPdf) SetPageLabels(labels []PageLabel)
func (gp *GoPdf) GetPageLabels() []PageLabel

Object ID

type ObjID int

func (id ObjID) Index() int     // 0-based array index
func (id ObjID) Ref() int       // 1-based PDF object reference
func (id ObjID) RefStr() string // "5 0 R"
func (id ObjID) IsValid() bool

Typed wrapper for PDF object identifiers, providing type safety over raw int indices.


XMP Metadata

type XMPMetadata struct {
    // Dublin Core
    Title       string
    Creator     []string
    Description string
    Subject     []string
    Rights      string
    Language    string

    // XMP Basic
    CreatorTool string
    CreateDate  time.Time
    ModifyDate  time.Time

    // PDF-specific
    Producer string
    Keywords string
    Trapped  string // "True", "False", "Unknown"

    // PDF/A conformance
    PDFAConformance string // "A", "B", "U"
    PDFAPart        int    // 1, 2, 3

    // Custom properties
    Custom map[string]string
}

func (gp *GoPdf) SetXMPMetadata(meta XMPMetadata)
func (gp *GoPdf) GetXMPMetadata() *XMPMetadata

Embeds an XMP metadata stream in the PDF catalog. Supports Dublin Core, XMP Basic, PDF-specific, and PDF/A conformance properties.


Incremental Save

func (gp *GoPdf) IncrementalSave(originalData []byte, modifiedIndices []int) ([]byte, error)
func (gp *GoPdf) WriteIncrementalPdf(pdfPath string, originalData []byte, modifiedIndices []int) error

Appends only modified objects to the original PDF data as an incremental update. If modifiedIndices is nil, all objects are written. This is significantly faster than a full rewrite for large documents.


Document Cloning

func (gp *GoPdf) Clone() (*GoPdf, error)

Creates a deep copy of the GoPdf instance by serializing and re-importing. The clone is fully independent — changes to one do not affect the other. Header/footer callbacks are not cloned.


Document Scrubbing

func (gp *GoPdf) Scrub(opt ScrubOption)
func DefaultScrubOption() ScrubOption

Remove potentially sensitive data from the PDF. Inspired by PyMuPDF's Document.scrub().

ScrubOption

type ScrubOption struct {
    Metadata      bool // Remove standard PDF metadata (/Info dictionary)
    XMLMetadata   bool // Remove XMP metadata stream
    EmbeddedFiles bool // Remove all embedded file attachments
    PageLabels    bool // Remove page label definitions
}

DefaultScrubOption() returns a ScrubOption with all fields set to true.

After scrubbing, call GarbageCollect(GCCompact) and save to ensure removed data is physically purged.


Optional Content Groups (Layers)

func (gp *GoPdf) AddOCG(ocg OCG) OCG
func (gp *GoPdf) GetOCGs() []OCG

Add PDF layers (Optional Content Groups) for selective visibility. Layers appear in the PDF viewer's layer panel.

OCG

type OCG struct {
    Name     string    // Display name of the layer
    Intent   OCGIntent // Visibility intent ("View" or "Design")
    On       bool      // Initially visible
}

OCGIntent

type OCGIntent string

const (
    OCGIntentView   OCGIntent = "View"   // For viewing purposes
    OCGIntentDesign OCGIntent = "Design" // For design purposes
)

Page Layout & Page Mode

func (gp *GoPdf) SetPageLayout(layout PageLayout)
func (gp *GoPdf) GetPageLayout() PageLayout
func (gp *GoPdf) SetPageMode(mode PageMode)
func (gp *GoPdf) GetPageMode() PageMode

Control how the PDF viewer displays the document when opened.

PageLayout

type PageLayout string

const (
    PageLayoutSinglePage      PageLayout = "SinglePage"      // One page at a time
    PageLayoutOneColumn       PageLayout = "OneColumn"       // Continuous column
    PageLayoutTwoColumnLeft   PageLayout = "TwoColumnLeft"   // Two columns, odd on left
    PageLayoutTwoColumnRight  PageLayout = "TwoColumnRight"  // Two columns, odd on right
    PageLayoutTwoPageLeft     PageLayout = "TwoPageLeft"     // Two pages, odd on left
    PageLayoutTwoPageRight    PageLayout = "TwoPageRight"    // Two pages, odd on right
)

PageMode

type PageMode string

const (
    PageModeUseNone        PageMode = "UseNone"        // No panel (default)
    PageModeUseOutlines    PageMode = "UseOutlines"    // Bookmarks panel
    PageModeUseThumbs      PageMode = "UseThumbs"      // Thumbnails panel
    PageModeFullScreen     PageMode = "FullScreen"     // Full-screen mode
    PageModeUseOC          PageMode = "UseOC"          // Layers panel
    PageModeUseAttachments PageMode = "UseAttachments" // Attachments panel
)

Document Statistics

func (gp *GoPdf) GetDocumentStats() DocumentStats
func (gp *GoPdf) GetFonts() []FontInfo

DocumentStats

type DocumentStats struct {
    PageCount          int        // Total number of pages
    ObjectCount        int        // Total number of PDF objects
    LiveObjectCount    int        // Number of non-null objects
    FontCount          int        // Number of font objects
    ImageCount         int        // Number of image objects
    ContentStreamCount int        // Number of content stream objects
    HasOutlines        bool       // Document has bookmarks
    HasEmbeddedFiles   bool       // Document has attachments
    HasXMPMetadata     bool       // XMP metadata is set
    HasPageLabels      bool       // Page labels are defined
    HasOCGs            bool       // Optional content groups are defined
    PDFVersion         PDFVersion // Configured PDF version
}

FontInfo

type FontInfo struct {
    Family     string // Font family name
    Style      int    // Font style (Regular, Bold, Italic)
    IsEmbedded bool   // Whether the font file is embedded
    Index      int    // Internal object index
}

TOC / Bookmarks

func (gp *GoPdf) GetTOC() []TOCItem
func (gp *GoPdf) SetTOC(items []TOCItem) error

Read and write the document's outline (bookmark) tree. GetTOC returns a flat list with hierarchy levels. SetTOC replaces the entire outline tree.

TOCItem

type TOCItem struct {
    Level  int     // Hierarchy level (1 = top level)
    Title  string  // Bookmark title text
    PageNo int     // 1-based target page number
    Y      float64 // Vertical position on target page (points from top)
}

SetTOC validation rules:

  • The first item must have Level 1.
  • Levels may increase by at most 1 from one item to the next.
  • Returns ErrInvalidTOCLevel on validation failure.

Text Extraction

func ExtractTextFromPage(pdfData []byte, pageIndex int) ([]ExtractedText, error)
func ExtractTextFromAllPages(pdfData []byte) (map[int][]ExtractedText, error)
func ExtractPageText(pdfData []byte, pageIndex int) (string, error)

Extract text content from existing PDF files. These are package-level functions (not methods on GoPdf).

  • ExtractTextFromPage — extracts text items with position, font, and size from a single page (0-based index).
  • ExtractTextFromAllPages — extracts text from all pages, returning a map of page index to text items.
  • ExtractPageText — convenience wrapper that returns all text from a page as a single string.

ExtractedText

type ExtractedText struct {
    Text     string  // The extracted text string
    X        float64 // Horizontal position
    Y        float64 // Vertical position
    FontName string  // PDF font resource name (e.g. "LiberationSerif-Regular")
    FontSize float64 // Font size in points
}

Image Extraction

func ExtractImagesFromPage(pdfData []byte, pageIndex int) ([]ExtractedImage, error)
func ExtractImagesFromAllPages(pdfData []byte) (map[int][]ExtractedImage, error)

Extract image metadata and data from existing PDF files. These are package-level functions.

ExtractedImage

type ExtractedImage struct {
    Name             string  // XObject resource name (e.g. "/Im1")
    Width            int     // Image width in pixels
    Height           int     // Image height in pixels
    BitsPerComponent int     // Bits per color component
    ColorSpace       string  // Color space name (e.g. "DeviceRGB")
    Filter           string  // Compression filter (e.g. "DCTDecode")
    Data             []byte  // Raw image data
    ObjNum           int     // PDF object number
    X, Y             float64 // Position on the page
    DisplayWidth     float64 // Rendered width on the page
    DisplayHeight    float64 // Rendered height on the page
}

func (img *ExtractedImage) GetImageFormat() string

GetImageFormat returns the likely image format based on the filter: "jpeg", "jp2", "tiff", "png", or "raw".


Form Fields (AcroForm)

func (gp *GoPdf) AddFormField(field FormField) error
func (gp *GoPdf) AddTextField(name string, x, y, w, h float64) error
func (gp *GoPdf) AddCheckbox(name string, x, y, size float64, checked bool) error
func (gp *GoPdf) AddDropdown(name string, x, y, w, h float64, options []string) error
func (gp *GoPdf) AddSignatureField(name string, x, y, w, h float64) error
func (gp *GoPdf) GetFormFields() []FormField

Add interactive form fields (widgets) to PDF pages. Fields appear as interactive elements in PDF viewers.

FormFieldType

const (
    FormFieldText      FormFieldType = iota // Text input
    FormFieldCheckbox                       // Checkbox
    FormFieldRadio                          // Radio button
    FormFieldChoice                         // Dropdown/list
    FormFieldButton                         // Push button
    FormFieldSignature                      // Signature
)

FormField

type FormField struct {
    Type        FormFieldType
    Name        string     // Unique field identifier
    X, Y        float64   // Top-left corner position
    W, H        float64   // Width and height
    Value       string     // Default value
    FontFamily  string     // Font family (must be pre-loaded)
    FontSize    float64    // Font size (default 12)
    Options     []string   // Choices for Choice fields
    MaxLen      int        // Max character length for text fields
    Multiline   bool       // Multi-line text input
    ReadOnly    bool       // Non-editable
    Required    bool       // Required field
    Color       [3]uint8   // Text color [R, G, B]
    BorderColor [3]uint8   // Border color [R, G, B]
    FillColor   [3]uint8   // Background fill color [R, G, B]
    HasBorder   bool       // Draw border
    HasFill     bool       // Draw background fill
    Checked     bool       // Initial checkbox state
}

Digital Signatures

func (gp *GoPdf) SignPDF(cfg SignatureConfig, w io.Writer) error
func (gp *GoPdf) SignPDFToFile(cfg SignatureConfig, path string) error
func VerifySignature(pdfData []byte) ([]SignatureVerifyResult, error)
func VerifySignatureFromFile(path string) ([]SignatureVerifyResult, error)

Sign PDF documents with PKCS#7 detached signatures and verify existing signatures.

SignatureConfig

type SignatureConfig struct {
    Certificate      *x509.Certificate   // Signing certificate
    CertificateChain []*x509.Certificate // Optional intermediate chain
    PrivateKey       crypto.Signer       // RSA or ECDSA private key
    Reason           string              // Reason for signing
    Location         string              // Signing location
    ContactInfo      string              // Signer contact info
    Name             string              // Signer name (defaults to cert CN)
    SignTime         time.Time           // Signing time (defaults to now)
    SignatureFieldName string            // Field name (defaults to "Signature1")
    Visible          bool                // Visible signature appearance
    X, Y, W, H      float64             // Visible signature rectangle
    PageNo           int                 // 1-based page number
}

Helper Functions

func LoadCertificateFromPEM(certPath string) (*x509.Certificate, error)
func LoadPrivateKeyFromPEM(keyPath string) (crypto.Signer, error)
func ParseCertificatePEM(data []byte) (*x509.Certificate, error)
func ParsePrivateKeyPEM(data []byte) (crypto.Signer, error)