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.
|
2020-12-06 13:03:03 +00:00
|
|
|
package fjson ;import (_ac "encoding/json";_c "github.com/unidoc/unipdf/v3/core";_d "github.com/unidoc/unipdf/v3/model";_e "io";_b "os";);type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
|
2020-11-23 22:15:56 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// Options lists allowed values if present.
|
|
|
|
Options []string `json:"options,omitempty"`;};
|
2020-11-23 22:15:56 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// FieldData represents form field data loaded from JSON file.
|
|
|
|
type FieldData struct{_cd []fieldValue };
|
2020-11-23 22:15:56 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// FieldValues implements model.FieldValueProvider interface.
|
|
|
|
func (_dc *FieldData )FieldValues ()(map[string ]_c .PdfObject ,error ){_cdb :=make (map[string ]_c .PdfObject );for _ ,_aca :=range _dc ._cd {if len (_aca .Value )> 0{_cdb [_aca .Name ]=_c .MakeString (_aca .Value );};};return _cdb ,nil ;};
|
2020-11-23 22:15:56 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// LoadFromPDFFile loads form field data from a PDF file.
|
|
|
|
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_aagf ,_cag :=_b .Open (filePath );if _cag !=nil {return nil ,_cag ;};defer _aagf .Close ();return LoadFromPDF (_aagf );};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// JSON returns the field data as a string in JSON format.
|
|
|
|
func (_cf FieldData )JSON ()(string ,error ){_ee ,_egb :=_ac .MarshalIndent (_cf ._cd ,"","\u0020\u0020\u0020\u0020");return string (_ee ),_egb ;};
|
2020-10-12 14:17:59 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// LoadFromPDF loads form field data from a PDF.
|
|
|
|
func LoadFromPDF (rs _e .ReadSeeker )(*FieldData ,error ){_ea ,_ba :=_d .NewPdfReader (rs );if _ba !=nil {return nil ,_ba ;};if _ea .AcroForm ==nil {return nil ,nil ;};var _ca []fieldValue ;_dg :=_ea .AcroForm .AllFields ();for _ ,_be :=range _dg {var _ff []string ;_ag :=make (map[string ]struct{});_dd ,_fb :=_be .FullName ();if _fb !=nil {return nil ,_fb ;};if _gb ,_dga :=_be .V .(*_c .PdfObjectString );_dga {_ca =append (_ca ,fieldValue {Name :_dd ,Value :_gb .Decoded ()});continue ;};var _ddf string ;for _ ,_eb :=range _be .Annotations {_ddb ,_eg :=_c .GetName (_eb .AS );if _eg {_ddf =_ddb .String ();};_bee ,_dgd :=_c .GetDict (_eb .AP );if !_dgd {continue ;};_cc ,_ :=_c .GetDict (_bee .Get ("\u004e"));for _ ,_fe :=range _cc .Keys (){_ec :=_fe .String ();if _ ,_cac :=_ag [_ec ];!_cac {_ff =append (_ff ,_ec );_ag [_ec ]=struct{}{};};};_bb ,_ :=_c .GetDict (_bee .Get ("\u0044"));for _ ,_bd :=range _bb .Keys (){_aag :=_bd .String ();if _ ,_fd :=_ag [_aag ];!_fd {_ff =append (_ff ,_aag );_ag [_aag ]=struct{}{};};};};_gg :=fieldValue {Name :_dd ,Value :_ddf ,Options :_ff };_ca =append (_ca ,_gg );};_cg :=FieldData {_cd :_ca };return &_cg ,nil ;};
|
2020-10-05 19:28:24 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// LoadFromJSON loads JSON form data from `r`.
|
|
|
|
func LoadFromJSON (r _e .Reader )(*FieldData ,error ){var _ad FieldData ;_aa :=_ac .NewDecoder (r ).Decode (&_ad ._cd );if _aa !=nil {return nil ,_aa ;};return &_ad ,nil ;};
|
2020-10-05 19:28:24 +00:00
|
|
|
|
2020-12-06 13:03:03 +00:00
|
|
|
// LoadFromJSONFile loads form field data from a JSON file.
|
|
|
|
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_aaa ,_f :=_b .Open (filePath );if _f !=nil {return nil ,_f ;};defer _aaa .Close ();return LoadFromJSON (_aaa );};
|