unipdf/fjson/fjson.go
2020-09-07 00:23:12 +00:00

37 lines
3.2 KiB
Go

//
// 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.
package fjson ;import (_e "encoding/json";_fe "github.com/unidoc/unipdf/v3/core";_fb "github.com/unidoc/unipdf/v3/model";_f "io";_aa "os";);type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
// LoadFromJSON loads JSON form data from `r`.
func LoadFromJSON (r _f .Reader )(*FieldData ,error ){var _d FieldData ;_ea :=_e .NewDecoder (r ).Decode (&_d ._c );if _ea !=nil {return nil ,_ea ;};return &_d ,nil ;};
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_c []fieldValue };
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_df ,_cb :=_aa .Open (filePath );if _cb !=nil {return nil ,_cb ;};defer _df .Close ();return LoadFromJSON (_df );};
// FieldValues implements model.FieldValueProvider interface.
func (_ee *FieldData )FieldValues ()(map[string ]_fe .PdfObject ,error ){_ae :=make (map[string ]_fe .PdfObject );for _ ,_dd :=range _ee ._c {if len (_dd .Value )> 0{_ae [_dd .Name ]=_fe .MakeString (_dd .Value );};};return _ae ,nil ;};
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_ag ,_acc :=_aa .Open (filePath );if _acc !=nil {return nil ,_acc ;};defer _ag .Close ();return LoadFromPDF (_ag );};
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _f .ReadSeeker )(*FieldData ,error ){_dfd ,_cf :=_fb .NewPdfReader (rs );if _cf !=nil {return nil ,_cf ;};if _dfd .AcroForm ==nil {return nil ,nil ;};var _g []fieldValue ;_b :=_dfd .AcroForm .AllFields ();for _ ,_be :=range _b {var _fd []string ;_cd :=make (map[string ]struct{});_de ,_gb :=_be .FullName ();if _gb !=nil {return nil ,_gb ;};if _cdf ,_gd :=_be .V .(*_fe .PdfObjectString );_gd {_g =append (_g ,fieldValue {Name :_de ,Value :_cdf .Decoded ()});continue ;};var _ec string ;for _ ,_cff :=range _be .Annotations {_cbf ,_gc :=_fe .GetName (_cff .AS );if _gc {_ec =_cbf .String ();};_ecb ,_ac :=_fe .GetDict (_cff .AP );if !_ac {continue ;};_cc ,_ :=_fe .GetDict (_ecb .Get ("\u004e"));for _ ,_cbg :=range _cc .Keys (){_fef :=_cbg .String ();if _ ,_ccb :=_cd [_fef ];!_ccb {_fd =append (_fd ,_fef );_cd [_fef ]=struct{}{};};};_bb ,_ :=_fe .GetDict (_ecb .Get ("\u0044"));for _ ,_fde :=range _bb .Keys (){_gdf :=_fde .String ();if _ ,_ff :=_cd [_gdf ];!_ff {_fd =append (_fd ,_gdf );_cd [_gdf ]=struct{}{};};};};_bd :=fieldValue {Name :_de ,Value :_ec ,Options :_fd };_g =append (_g ,_bd );};_aae :=FieldData {_c :_g };return &_aae ,nil ;};
// JSON returns the field data as a string in JSON format.
func (_ecc FieldData )JSON ()(string ,error ){_eae ,_def :=_e .MarshalIndent (_ecc ._c ,"","\u0020\u0020\u0020\u0020");return string (_eae ),_def ;};