unipdf/fjson/fjson.go

52 lines
4.4 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.
2023-02-07 17:17:49 +00:00
package fjson ;import (_b "encoding/json";_c "github.com/unidoc/unipdf/v3/common";_ff "github.com/unidoc/unipdf/v3/core";_ba "github.com/unidoc/unipdf/v3/model";_f "io";_g "os";);
2022-09-10 15:35:04 +00:00
2023-02-07 17:17:49 +00:00
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_bd ,_ffg :=_g .Open (filePath );if _ffg !=nil {return nil ,_ffg ;};defer _bd .Close ();return LoadFromJSON (_bd );};type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
ImageValue *_ba .Image `json:"-"`;
2022-09-23 18:05:51 +00:00
2023-02-07 17:17:49 +00:00
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
2022-09-23 18:05:51 +00:00
2023-02-07 17:17:49 +00:00
// JSON returns the field data as a string in JSON format.
func (_cc FieldData )JSON ()(string ,error ){_abc ,_bgd :=_b .MarshalIndent (_cc ._cg ,"","\u0020\u0020\u0020\u0020");return string (_abc ),_bgd ;};
2022-06-27 19:58:38 +00:00
2022-12-15 21:59:56 +00:00
// LoadFromJSON loads JSON form data from `r`.
2023-02-07 17:17:49 +00:00
func LoadFromJSON (r _f .Reader )(*FieldData ,error ){var _ge FieldData ;_e :=_b .NewDecoder (r ).Decode (&_ge ._cg );if _e !=nil {return nil ,_e ;};return &_ge ,nil ;};
2022-09-10 15:35:04 +00:00
2023-02-07 17:17:49 +00:00
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_db ,_fcb :=_g .Open (filePath );if _fcb !=nil {return nil ,_fcb ;};defer _db .Close ();return LoadFromPDF (_db );};
2022-06-06 22:48:24 +00:00
2023-01-08 22:34:27 +00:00
// FieldImageValues implements model.FieldImageProvider interface.
2023-02-07 17:17:49 +00:00
func (_gee *FieldData )FieldImageValues ()(map[string ]*_ba .Image ,error ){_cd :=make (map[string ]*_ba .Image );for _ ,_bfc :=range _gee ._cg {if _bfc .ImageValue !=nil {_cd [_bfc .Name ]=_bfc .ImageValue ;};};return _cd ,nil ;};
2023-01-08 22:34:27 +00:00
2023-02-07 17:17:49 +00:00
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_cg []fieldValue };
2023-01-08 22:34:27 +00:00
// LoadFromPDF loads form field data from a PDF.
2023-02-07 17:17:49 +00:00
func LoadFromPDF (rs _f .ReadSeeker )(*FieldData ,error ){_de ,_a :=_ba .NewPdfReader (rs );if _a !=nil {return nil ,_a ;};if _de .AcroForm ==nil {return nil ,nil ;};var _bf []fieldValue ;_dd :=_de .AcroForm .AllFields ();for _ ,_af :=range _dd {var _ddg []string ;
_ad :=make (map[string ]struct{});_fb ,_fbf :=_af .FullName ();if _fbf !=nil {return nil ,_fbf ;};if _gc ,_ce :=_af .V .(*_ff .PdfObjectString );_ce {_bf =append (_bf ,fieldValue {Name :_fb ,Value :_gc .Decoded ()});continue ;};var _ac string ;for _ ,_ag :=range _af .Annotations {_ef ,_bg :=_ff .GetName (_ag .AS );
if _bg {_ac =_ef .String ();};_gf ,_ddc :=_ff .GetDict (_ag .AP );if !_ddc {continue ;};_ed ,_ :=_ff .GetDict (_gf .Get ("\u004e"));for _ ,_eg :=range _ed .Keys (){_eb :=_eg .String ();if _ ,_fc :=_ad [_eb ];!_fc {_ddg =append (_ddg ,_eb );_ad [_eb ]=struct{}{};
};};_ec ,_ :=_ff .GetDict (_gf .Get ("\u0044"));for _ ,_ab :=range _ec .Keys (){_fe :=_ab .String ();if _ ,_gca :=_ad [_fe ];!_gca {_ddg =append (_ddg ,_fe );_ad [_fe ]=struct{}{};};};};_cgc :=fieldValue {Name :_fb ,Value :_ac ,Options :_ddg };_bf =append (_bf ,_cgc );
};_ade :=FieldData {_cg :_bf };return &_ade ,nil ;};
2023-01-08 22:34:27 +00:00
2023-02-07 17:17:49 +00:00
// SetImageFromFile assign image file to a specific field identified by fieldName.
func (_ddd *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_cce ,_dc :=_g .Open (imagePath );if _dc !=nil {return _dc ;};defer _cce .Close ();_fcg ,_dc :=_ba .ImageHandling .Read (_cce );if _dc !=nil {_c .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_dc );
return _dc ;};return _ddd .SetImage (fieldName ,_fcg ,opt );};
// FieldValues implements model.FieldValueProvider interface.
func (_cfc *FieldData )FieldValues ()(map[string ]_ff .PdfObject ,error ){_egc :=make (map[string ]_ff .PdfObject );for _ ,_abe :=range _cfc ._cg {if len (_abe .Value )> 0{_egc [_abe .Name ]=_ff .MakeString (_abe .Value );};};return _egc ,nil ;};
// SetImage assign model.Image to a specific field identified by fieldName.
func (_bge *FieldData )SetImage (fieldName string ,img *_ba .Image ,opt []string )error {_bb :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_bge ._cg =append (_bge ._cg ,_bb );return nil ;};