2020-08-27 21:45:09 +00:00
|
|
|
//
|
|
|
|
// Copyright 2020 FoxyUtils ehf. All rights reserved.
|
|
|
|
//
|
|
|
|
// This is a commercial product and requires a license to operate.
|
|
|
|
// A trial license can be obtained at https://unidoc.io
|
|
|
|
//
|
|
|
|
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by the UniDoc End User License Agreement
|
|
|
|
// terms that can be accessed at https://unidoc.io/eula/
|
|
|
|
|
|
|
|
// Package fjson provides support for loading PDF form field data from JSON data/files.
|
2021-01-26 01:31:56 +00:00
|
|
|
package fjson ;import (_dd "encoding/json";_dc "github.com/unidoc/unipdf/v3/core";_e "github.com/unidoc/unipdf/v3/model";_g "io";_dg "os";);
|
2020-11-23 22:15:56 +00:00
|
|
|
|
2021-01-26 01:31:56 +00:00
|
|
|
// FieldData represents form field data loaded from JSON file.
|
|
|
|
type FieldData struct{_f []fieldValue };
|
2020-11-23 22:15:56 +00:00
|
|
|
|
2021-01-26 01:31:56 +00:00
|
|
|
// JSON returns the field data as a string in JSON format.
|
|
|
|
func (_gfa FieldData )JSON ()(string ,error ){_dce ,_cab :=_dd .MarshalIndent (_gfa ._f ,"","\u0020\u0020\u0020\u0020");return string (_dce ),_cab ;};type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2021-01-26 01:31:56 +00:00
|
|
|
// Options lists allowed values if present.
|
|
|
|
Options []string `json:"options,omitempty"`;};
|
2020-10-12 14:17:59 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// LoadFromPDF loads form field data from a PDF.
|
2021-01-26 01:31:56 +00:00
|
|
|
func LoadFromPDF (rs _g .ReadSeeker )(*FieldData ,error ){_ga ,_ff :=_e .NewPdfReader (rs );if _ff !=nil {return nil ,_ff ;};if _ga .AcroForm ==nil {return nil ,nil ;};var _eg []fieldValue ;_ed :=_ga .AcroForm .AllFields ();for _ ,_ef :=range _ed {var _a []string ;_c :=make (map[string ]struct{});_bf ,_fc :=_ef .FullName ();if _fc !=nil {return nil ,_fc ;};if _fee ,_cb :=_ef .V .(*_dc .PdfObjectString );_cb {_eg =append (_eg ,fieldValue {Name :_bf ,Value :_fee .Decoded ()});continue ;};var _ae string ;for _ ,_ddc :=range _ef .Annotations {_ca ,_af :=_dc .GetName (_ddc .AS );if _af {_ae =_ca .String ();};_dge ,_cf :=_dc .GetDict (_ddc .AP );if !_cf {continue ;};_fcc ,_ :=_dc .GetDict (_dge .Get ("\u004e"));for _ ,_edb :=range _fcc .Keys (){_fef :=_edb .String ();if _ ,_bb :=_c [_fef ];!_bb {_a =append (_a ,_fef );_c [_fef ]=struct{}{};};};_db ,_ :=_dc .GetDict (_dge .Get ("\u0044"));for _ ,_ag :=range _db .Keys (){_fed :=_ag .String ();if _ ,_cd :=_c [_fed ];!_cd {_a =append (_a ,_fed );_c [_fed ]=struct{}{};};};};_cdd :=fieldValue {Name :_bf ,Value :_ae ,Options :_a };_eg =append (_eg ,_cdd );};_bba :=FieldData {_f :_eg };return &_bba ,nil ;};
|
2021-01-07 14:20:10 +00:00
|
|
|
|
2021-01-26 01:31:56 +00:00
|
|
|
// LoadFromPDFFile loads form field data from a PDF file.
|
|
|
|
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_efbe ,_fb :=_dg .Open (filePath );if _fb !=nil {return nil ,_fb ;};defer _efbe .Close ();return LoadFromPDF (_efbe );};
|
2020-10-05 19:28:24 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// LoadFromJSON loads JSON form data from `r`.
|
2021-01-26 01:31:56 +00:00
|
|
|
func LoadFromJSON (r _g .Reader )(*FieldData ,error ){var _ge FieldData ;_fe :=_dd .NewDecoder (r ).Decode (&_ge ._f );if _fe !=nil {return nil ,_fe ;};return &_ge ,nil ;};
|
2020-10-05 19:28:24 +00:00
|
|
|
|
2021-01-26 01:31:56 +00:00
|
|
|
// FieldValues implements model.FieldValueProvider interface.
|
|
|
|
func (_df *FieldData )FieldValues ()(map[string ]_dc .PdfObject ,error ){_fa :=make (map[string ]_dc .PdfObject );for _ ,_bbf :=range _df ._f {if len (_bbf .Value )> 0{_fa [_bbf .Name ]=_dc .MakeString (_bbf .Value );};};return _fa ,nil ;};
|
2021-01-07 14:20:10 +00:00
|
|
|
|
2021-01-26 01:31:56 +00:00
|
|
|
// LoadFromJSONFile loads form field data from a JSON file.
|
|
|
|
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_gd ,_b :=_dg .Open (filePath );if _b !=nil {return nil ,_b ;};defer _gd .Close ();return LoadFromJSON (_gd );};
|