forms

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package forms provides form field reading and manipulation functionality.

Package forms provides form field reading and manipulation functionality.

Package forms provides form field reading and manipulation functionality.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldInfo

type FieldInfo struct {
	// Name is the fully qualified field name.
	Name string

	// Type is the field type (Tx, Btn, Ch, Sig).
	Type FieldType

	// Value is the current field value.
	Value interface{}

	// DefaultValue is the default field value.
	DefaultValue interface{}

	// Flags is the field flags bitmask (Ff).
	Flags int

	// Rect is the field rectangle [x1, y1, x2, y2].
	Rect [4]float64

	// Options contains choice field options.
	Options []string
}

FieldInfo contains information about a form field.

Example
package main

import (
	"fmt"

	"github.com/coregx/gxpdf/internal/application/forms"
)

func main() {
	// Create a field info struct (normally from Reader)
	field := &forms.FieldInfo{
		Name:         "customer_name",
		Type:         forms.FieldTypeText,
		Value:        "John Doe",
		DefaultValue: "",
		Flags:        2, // Required flag
		Rect:         [4]float64{100, 700, 300, 720},
	}

	fmt.Printf("Field: %s\n", field.Name)
	fmt.Printf("Type: %s\n", field.Type)
	fmt.Printf("Value: %v\n", field.Value)
	fmt.Printf("Required: %v\n", field.Flags&2 != 0)
	fmt.Printf("Width: %.0f\n", field.Rect[2]-field.Rect[0])

}
Output:

Field: customer_name
Type: Tx
Value: John Doe
Required: true
Width: 200
Example (CheckboxField)
package main

import (
	"fmt"

	"github.com/coregx/gxpdf/internal/application/forms"
)

func main() {
	// Checkbox field (button type)
	field := &forms.FieldInfo{
		Name:  "agree_terms",
		Type:  forms.FieldTypeButton,
		Value: "Yes", // "Yes" = checked, "Off" = unchecked
		Flags: 0,
	}

	isChecked := field.Value == "Yes" || field.Value == true
	fmt.Printf("Field: %s\n", field.Name)
	fmt.Printf("Checked: %v\n", isChecked)

}
Output:

Field: agree_terms
Checked: true
Example (ChoiceField)
package main

import (
	"fmt"

	"github.com/coregx/gxpdf/internal/application/forms"
)

func main() {
	// Choice field with dropdown options
	field := &forms.FieldInfo{
		Name:    "country",
		Type:    forms.FieldTypeChoice,
		Value:   "USA",
		Options: []string{"USA", "Canada", "Mexico", "UK", "Germany"},
	}

	fmt.Printf("Field: %s (%s)\n", field.Name, field.Type)
	fmt.Printf("Selected: %v\n", field.Value)
	fmt.Printf("Options: %v\n", field.Options)

}
Output:

Field: country (Ch)
Selected: USA
Options: [USA Canada Mexico UK Germany]

type FieldType

type FieldType string

FieldType represents the type of a form field.

Example
package main

import (
	"fmt"

	"github.com/coregx/gxpdf/internal/application/forms"
)

func main() {
	// Field types correspond to PDF specification
	fmt.Println("Text field type:", forms.FieldTypeText)
	fmt.Println("Button field type:", forms.FieldTypeButton)
	fmt.Println("Choice field type:", forms.FieldTypeChoice)
	fmt.Println("Signature field type:", forms.FieldTypeSignature)

}
Output:

Text field type: Tx
Button field type: Btn
Choice field type: Ch
Signature field type: Sig
const (
	FieldTypeText      FieldType = "Tx"  // Text field
	FieldTypeButton    FieldType = "Btn" // Button (checkbox, radio, pushbutton)
	FieldTypeChoice    FieldType = "Ch"  // Choice (list, combo)
	FieldTypeSignature FieldType = "Sig" // Signature
)

type FieldUpdate

type FieldUpdate struct {
	Name  string
	Value interface{}
}

FieldUpdate represents a form field value update.

type FlattenInfo

type FlattenInfo struct {
	// FieldName is the fully qualified field name.
	FieldName string

	// PageIndex is the 0-based page number where the field appears.
	PageIndex int

	// Rect is the field rectangle [x1, y1, x2, y2].
	Rect [4]float64

	// AppearanceStream is the content stream for the field appearance.
	AppearanceStream []byte

	// Resources contains resources used by the appearance stream.
	Resources *parser.Dictionary
}

FlattenInfo contains information needed to flatten a field.

Example
package main

import (
	"fmt"

	"github.com/coregx/gxpdf/internal/application/forms"
)

func main() {
	// FlattenInfo contains data needed to render a field as static content
	info := &forms.FlattenInfo{
		FieldName:        "signature",
		PageIndex:        0,
		Rect:             [4]float64{100, 100, 300, 150},
		AppearanceStream: []byte("q 1 0 0 1 0 0 cm /Img1 Do Q"),
	}

	fmt.Printf("Field to flatten: %s\n", info.FieldName)
	fmt.Printf("On page: %d\n", info.PageIndex)
	fmt.Printf("Position: (%.0f, %.0f)\n", info.Rect[0], info.Rect[1])
	fmt.Printf("Has appearance: %v\n", len(info.AppearanceStream) > 0)

}
Output:

Field to flatten: signature
On page: 0
Position: (100, 100)
Has appearance: true

type Flattener

type Flattener struct {
	// contains filtered or unexported fields
}

Flattener converts form fields to static page content.

Form flattening removes interactivity by rendering field appearances directly onto pages, making them non-editable.

Example
package main

import (
	"fmt"

	"github.com/coregx/gxpdf/internal/application/forms"
)

func main() {
	// Create flattener (normally with parser.Reader)
	flattener := forms.NewFlattener(nil)

	// Check if flattening is possible
	canFlatten := flattener.CanFlatten()
	fmt.Printf("Can flatten: %v\n", canFlatten)

}
Output:

Can flatten: false

func NewFlattener

func NewFlattener(pdfReader *parser.Reader) *Flattener

NewFlattener creates a new form flattener.

func (*Flattener) CanFlatten

func (f *Flattener) CanFlatten() bool

CanFlatten returns true if the document has fields that can be flattened.

func (*Flattener) GetFlattenInfo

func (f *Flattener) GetFlattenInfo() ([]*FlattenInfo, error)

GetFlattenInfo returns flattening information for all form fields.

This extracts the appearance streams and positions needed to render fields as static content.

func (*Flattener) GetFlattenInfoByName

func (f *Flattener) GetFlattenInfoByName(names ...string) ([]*FlattenInfo, error)

GetFlattenInfoByName returns flattening information for specific fields.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads form fields from a PDF document.

func NewReader

func NewReader(pdfReader *parser.Reader) *Reader

NewReader creates a new form field reader.

func (*Reader) GetFieldByName

func (r *Reader) GetFieldByName(name string) (*FieldInfo, error)

GetFieldByName returns a specific field by its fully qualified name.

func (*Reader) GetFields

func (r *Reader) GetFields() ([]*FieldInfo, error)

GetFields returns all form fields in the document.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer writes form field values to a PDF document.

Example
package main

import (
	"fmt"

	"github.com/coregx/gxpdf/internal/application/forms"
)

func main() {
	// Create writer (normally with parser.Reader)
	writer := forms.NewWriter(nil)

	// Check initial state
	fmt.Printf("Has updates: %v\n", writer.HasUpdates())
	fmt.Printf("Update count: %d\n", len(writer.GetUpdates()))

}
Output:

Has updates: false
Update count: 0

func NewWriter

func NewWriter(pdfReader *parser.Reader) *Writer

NewWriter creates a new form field writer.

func (*Writer) ApplyUpdatesToDict

func (w *Writer) ApplyUpdatesToDict(field *FieldInfo, dict *parser.Dictionary) *parser.Dictionary

ApplyUpdatesToDict applies updates to a field dictionary.

This creates a new dictionary with the /V entry updated. Used internally when writing the modified PDF.

func (*Writer) GetFieldsToUpdate

func (w *Writer) GetFieldsToUpdate() ([]*FieldInfo, error)

GetFieldsToUpdate returns field info for all fields that have updates.

func (*Writer) GetUpdates

func (w *Writer) GetUpdates() map[string]interface{}

GetUpdates returns all pending field updates.

func (*Writer) HasUpdates

func (w *Writer) HasUpdates() bool

HasUpdates returns true if there are pending updates.

func (*Writer) SetFieldValue

func (w *Writer) SetFieldValue(name string, value interface{}) error

SetFieldValue sets a form field value by name.

The value type depends on the field type:

  • Text field: string
  • Checkbox: bool or string (e.g., "Yes", "Off")
  • Radio button: string (option name)
  • Choice field: string or []string (for multi-select)

Returns an error if the field is not found.

func (*Writer) ValidateFieldValue

func (w *Writer) ValidateFieldValue(name string, value interface{}) error

ValidateFieldValue validates a value against the field type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL