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 ¶
- type FieldInfo
- type FieldType
- type FieldUpdate
- type FlattenInfo
- type Flattener
- type Reader
- type Writer
- func (w *Writer) ApplyUpdatesToDict(field *FieldInfo, dict *parser.Dictionary) *parser.Dictionary
- func (w *Writer) GetFieldsToUpdate() ([]*FieldInfo, error)
- func (w *Writer) GetUpdates() map[string]interface{}
- func (w *Writer) HasUpdates() bool
- func (w *Writer) SetFieldValue(name string, value interface{}) error
- func (w *Writer) ValidateFieldValue(name string, value interface{}) error
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
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 ¶
NewFlattener creates a new form flattener.
func (*Flattener) CanFlatten ¶
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 (*Reader) GetFieldByName ¶
GetFieldByName returns a specific field by its fully qualified name.
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 (*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 ¶
GetFieldsToUpdate returns field info for all fields that have updates.
func (*Writer) GetUpdates ¶
GetUpdates returns all pending field updates.
func (*Writer) HasUpdates ¶
HasUpdates returns true if there are pending updates.
func (*Writer) SetFieldValue ¶
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 ¶
ValidateFieldValue validates a value against the field type.