mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-05 19:30:30 +08:00
Working on higher level handling of forms
This commit is contained in:
parent
57c617ca3e
commit
50cc03bcf9
83
pdf/forms.go
83
pdf/forms.go
@ -29,6 +29,7 @@ type PdfAcroForm struct {
|
|||||||
XFA PdfObject
|
XFA PdfObject
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Used when loading forms from PDF files.
|
||||||
func (r *PdfReader) newPdfAcroFormFromDict(d PdfObjectDictionary) (*PdfAcroForm, error) {
|
func (r *PdfReader) newPdfAcroFormFromDict(d PdfObjectDictionary) (*PdfAcroForm, error) {
|
||||||
acroForm := PdfAcroForm{}
|
acroForm := PdfAcroForm{}
|
||||||
|
|
||||||
@ -84,9 +85,38 @@ func (r *PdfReader) newPdfAcroFormFromDict(d PdfObjectDictionary) (*PdfAcroForm,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *PdfAcroForm) ToPdfObject() PdfObject {
|
func (this *PdfAcroForm) ToPdfObject() PdfObject {
|
||||||
|
dict := PdfObjectDictionary{}
|
||||||
|
|
||||||
|
if this.Fields != nil {
|
||||||
|
d["Fields"] = this.Fields.ToPdfObject()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if this.NeedAppearances != nil {
|
||||||
|
d["NeedAppearances"] = this.NeedAppearances
|
||||||
|
}
|
||||||
|
if this.SigFlags != nil {
|
||||||
|
d["SigFlags"] = this.SigFlags
|
||||||
|
}
|
||||||
|
if this.CO != nil {
|
||||||
|
d["CO"] = this.CO
|
||||||
|
}
|
||||||
|
if this.DR != nil {
|
||||||
|
d["DR"] = this.DR
|
||||||
|
}
|
||||||
|
if this.DA != nil {
|
||||||
|
d["DA"] = this.DA
|
||||||
|
}
|
||||||
|
if this.Q != nil {
|
||||||
|
d["Q"] = this.Q
|
||||||
|
}
|
||||||
|
if this.XFA != nil {
|
||||||
|
d["XFA"] = this.XFA
|
||||||
|
}
|
||||||
|
|
||||||
|
return &d
|
||||||
|
}
|
||||||
|
|
||||||
|
// Used when loading fields from PDF files.
|
||||||
func (r *PdfReader) newPdfFieldDict(d PdfObjectDictionary) (*PdfField, error) {
|
func (r *PdfReader) newPdfFieldDict(d PdfObjectDictionary) (*PdfField, error) {
|
||||||
field := PdfField{}
|
field := PdfField{}
|
||||||
|
|
||||||
@ -171,7 +201,7 @@ type PdfField struct {
|
|||||||
// In a terminal field, the Kids array ordinarily shall refer to one or more separate widget annotations that are associated
|
// In a terminal field, the Kids array ordinarily shall refer to one or more separate widget annotations that are associated
|
||||||
// with this field. However, if there is only one associated widget annotation, and its contents have been merged into the field
|
// with this field. However, if there is only one associated widget annotation, and its contents have been merged into the field
|
||||||
// dictionary, Kids shall be omitted.
|
// dictionary, Kids shall be omitted.
|
||||||
Kids PdfObject
|
Kids PdfObject // Kids can be array of other fields or widgets (PdfObjectConvertible)
|
||||||
T PdfObject
|
T PdfObject
|
||||||
TU PdfObject
|
TU PdfObject
|
||||||
TM PdfObject
|
TM PdfObject
|
||||||
@ -185,33 +215,36 @@ type PdfField struct {
|
|||||||
|
|
||||||
func (this *PdfField) ToPdfObject() {
|
func (this *PdfField) ToPdfObject() {
|
||||||
// If Kids refer only to a single pdf widget annotation widget, then can merge it in.
|
// If Kids refer only to a single pdf widget annotation widget, then can merge it in.
|
||||||
|
dict := PdfObjectDictionary{}
|
||||||
|
|
||||||
|
if this.Parent != nil {
|
||||||
|
dict["Parent"] = this.Parent.ToPdfObjectReference()
|
||||||
|
// ToPdfObjectAsReference
|
||||||
|
// ToDirectPdfObject
|
||||||
|
}
|
||||||
|
if this.Kids != nil {
|
||||||
|
dict["Kids"] = this.Kids
|
||||||
}
|
}
|
||||||
|
|
||||||
type PdfFieldVariableText struct {
|
if this.T != nil {
|
||||||
PdfField
|
dict["T"] = this.T
|
||||||
DA PdfObject
|
|
||||||
Q PdfObject
|
|
||||||
DS PdfObject
|
|
||||||
RV PdfObject
|
|
||||||
}
|
}
|
||||||
|
if this.TU != nil {
|
||||||
type PdfFieldText struct {
|
dict["TU"] = this.Tu
|
||||||
PdfField
|
|
||||||
// Text field
|
|
||||||
MaxLen PdfObject // inheritable
|
|
||||||
}
|
}
|
||||||
|
if this.TM != nil {
|
||||||
type PdfFieldChoice struct {
|
dict["TM"] = this.TM
|
||||||
PdfField
|
}
|
||||||
// Choice fields.
|
if this.Ff != nil {
|
||||||
Opt PdfObject
|
dict["Ff"] = this.Ff
|
||||||
TI PdfObject
|
}
|
||||||
I PdfObject
|
if this.V != nil {
|
||||||
|
dict["V"] = this.V
|
||||||
|
}
|
||||||
|
if this.DV != nil {
|
||||||
|
dict["DV"] = this.DV
|
||||||
|
}
|
||||||
|
if this.AA != nil {
|
||||||
|
dict["AA"] = this.AA
|
||||||
}
|
}
|
||||||
|
|
||||||
type PdfFieldSignature struct {
|
|
||||||
PdfField
|
|
||||||
// Signature fields (Table 232).
|
|
||||||
Lock PdfObject
|
|
||||||
SV PdfObject
|
|
||||||
}
|
}
|
||||||
|
@ -420,12 +420,17 @@ func (this *PdfReader) LoadForms() error {
|
|||||||
common.Log.Debug("Has Acro forms")
|
common.Log.Debug("Has Acro forms")
|
||||||
// Load it.
|
// Load it.
|
||||||
|
|
||||||
|
acroForm, err := this.newPdfAcroFormFromDict(formsDict)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
this.fields := this.buildFieldTree(obj)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursive build form field tree.
|
// Recursive build form field tree.
|
||||||
func (this *PdfReader) buildFieldTree(obj PdfObject) (*PdfOutlineTreeNode, error) {
|
func (this *PdfReader) buildFieldTree(obj PdfObject) (*[]*PdfField, error) {
|
||||||
// Describe how to do this first by hand.
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
obj, err = this.traceToObject(obj)
|
obj, err = this.traceToObject(obj)
|
||||||
|
|
||||||
@ -441,6 +446,14 @@ func (this *PdfReader) buildFieldTree(obj PdfObject) (*PdfOutlineTreeNode, error
|
|||||||
|
|
||||||
// Field dict? Check kids.
|
// Field dict? Check kids.
|
||||||
var isTerminal bool
|
var isTerminal bool
|
||||||
|
|
||||||
|
subtypObj, hasType := (*d)["Subtype"].(*PdfObjectName)
|
||||||
|
if hasType && *subtypObj == "Widget" {
|
||||||
|
widget := this.newPdfAnnotationWidgetFromDict(d)
|
||||||
|
node.Kids = widget
|
||||||
|
isterminal = true
|
||||||
|
}
|
||||||
|
|
||||||
kidsObj, hasKids := (*d)["Kids"]
|
kidsObj, hasKids := (*d)["Kids"]
|
||||||
if hasKids {
|
if hasKids {
|
||||||
kidsObj, err = this.traceToObject(kidsObj)
|
kidsObj, err = this.traceToObject(kidsObj)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user