unipdf/fjson/fjson.go

37 lines
3.2 KiB
Go
Raw Normal View History

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-09-28 23:18:17 +00:00
package fjson ;import (_g "encoding/json";_e "github.com/unidoc/unipdf/v3/core";_ea "github.com/unidoc/unipdf/v3/model";_fd "io";_f "os";);type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
2020-08-27 21:45:09 +00:00
2020-09-28 23:18:17 +00:00
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
2020-08-27 21:45:09 +00:00
2020-09-21 01:20:10 +00:00
// LoadFromPDF loads form field data from a PDF.
2020-09-28 23:18:17 +00:00
func LoadFromPDF (rs _fd .ReadSeeker )(*FieldData ,error ){_c ,_cc :=_ea .NewPdfReader (rs );if _cc !=nil {return nil ,_cc ;};if _c .AcroForm ==nil {return nil ,nil ;};var _fda []fieldValue ;_gb :=_c .AcroForm .AllFields ();for _ ,_bd :=range _gb {var _ab []string ;_gae :=make (map[string ]struct{});_db ,_cg :=_bd .FullName ();if _cg !=nil {return nil ,_cg ;};if _bg ,_fg :=_bd .V .(*_e .PdfObjectString );_fg {_fda =append (_fda ,fieldValue {Name :_db ,Value :_bg .Decoded ()});continue ;};var _bb string ;for _ ,_ead :=range _bd .Annotations {_ad ,_dc :=_e .GetName (_ead .AS );if _dc {_bb =_ad .String ();};_cd ,_ee :=_e .GetDict (_ead .AP );if !_ee {continue ;};_ca ,_ :=_e .GetDict (_cd .Get ("\u004e"));for _ ,_ef :=range _ca .Keys (){_efg :=_ef .String ();if _ ,_dbg :=_gae [_efg ];!_dbg {_ab =append (_ab ,_efg );_gae [_efg ]=struct{}{};};};_ae ,_ :=_e .GetDict (_cd .Get ("\u0044"));for _ ,_efb :=range _ae .Keys (){_ba :=_efb .String ();if _ ,_bf :=_gae [_ba ];!_bf {_ab =append (_ab ,_ba );_gae [_ba ]=struct{}{};};};};_ge :=fieldValue {Name :_db ,Value :_bb ,Options :_ab };_fda =append (_fda ,_ge );};_eaf :=FieldData {_fb :_fda };return &_eaf ,nil ;};
2020-09-21 01:20:10 +00:00
2020-09-28 23:18:17 +00:00
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_a ,_ga :=_f .Open (filePath );if _ga !=nil {return nil ,_ga ;};defer _a .Close ();return LoadFromJSON (_a );};
2020-08-27 21:45:09 +00:00
2020-08-31 21:12:07 +00:00
// FieldValues implements model.FieldValueProvider interface.
2020-09-28 23:18:17 +00:00
func (_bfd *FieldData )FieldValues ()(map[string ]_e .PdfObject ,error ){_gc :=make (map[string ]_e .PdfObject );for _ ,_af :=range _bfd ._fb {if len (_af .Value )> 0{_gc [_af .Name ]=_e .MakeString (_af .Value );};};return _gc ,nil ;};
2020-08-27 21:45:09 +00:00
2020-09-28 23:18:17 +00:00
// JSON returns the field data as a string in JSON format.
func (_bddc FieldData )JSON ()(string ,error ){_geb ,_bfe :=_g .MarshalIndent (_bddc ._fb ,"","\u0020\u0020\u0020\u0020");return string (_geb ),_bfe ;};
2020-08-27 21:45:09 +00:00
2020-09-28 23:18:17 +00:00
// LoadFromJSON loads JSON form data from `r`.
func LoadFromJSON (r _fd .Reader )(*FieldData ,error ){var _d FieldData ;_eg :=_g .NewDecoder (r ).Decode (&_d ._fb );if _eg !=nil {return nil ,_eg ;};return &_d ,nil ;};
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_fc ,_bfb :=_f .Open (filePath );if _bfb !=nil {return nil ,_bfb ;};defer _fc .Close ();return LoadFromPDF (_fc );};
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_fb []fieldValue };