mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-26 13:48:55 +08:00
51 lines
4.4 KiB
Go
51 lines
4.4 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 (_b "encoding/json";_a "github.com/unidoc/unipdf/v3/common";_c "github.com/unidoc/unipdf/v3/core";_ae "github.com/unidoc/unipdf/v3/model";_be "io";_e "os";);
|
|
|
|
// LoadFromJSON loads JSON form data from `r`.
|
|
func LoadFromJSON (r _be .Reader )(*FieldData ,error ){var _d FieldData ;_gb :=_b .NewDecoder (r ).Decode (&_d ._bd );if _gb !=nil {return nil ,_gb ;};return &_d ,nil ;};
|
|
|
|
// SetImage assign model.Image to a specific field identified by fieldName.
|
|
func (_dfg *FieldData )SetImage (fieldName string ,img *_ae .Image ,opt []string )error {_feff :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_dfg ._bd =append (_dfg ._bd ,_feff );return nil ;};
|
|
|
|
// FieldImageValues implements model.FieldImageProvider interface.
|
|
func (_gbd *FieldData )FieldImageValues ()(map[string ]*_ae .Image ,error ){_ec :=make (map[string ]*_ae .Image );for _ ,_fba :=range _gbd ._bd {if _fba .ImageValue !=nil {_ec [_fba .Name ]=_fba .ImageValue ;};};return _ec ,nil ;};
|
|
|
|
// FieldData represents form field data loaded from JSON file.
|
|
type FieldData struct{_bd []fieldValue };type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;ImageValue *_ae .Image `json:"-"`;
|
|
|
|
// Options lists allowed values if present.
|
|
Options []string `json:"options,omitempty"`;};
|
|
|
|
// JSON returns the field data as a string in JSON format.
|
|
func (_bee FieldData )JSON ()(string ,error ){_afg ,_gdf :=_b .MarshalIndent (_bee ._bd ,"","\u0020\u0020\u0020\u0020");return string (_afg ),_gdf ;};
|
|
|
|
// LoadFromPDF loads form field data from a PDF.
|
|
func LoadFromPDF (rs _be .ReadSeeker )(*FieldData ,error ){_ag ,_ac :=_ae .NewPdfReader (rs );if _ac !=nil {return nil ,_ac ;};if _ag .AcroForm ==nil {return nil ,nil ;};var _f []fieldValue ;_fd :=_ag .AcroForm .AllFields ();for _ ,_gec :=range _fd {var _cf []string ;
|
|
_eg :=make (map[string ]struct{});_egc ,_fa :=_gec .FullName ();if _fa !=nil {return nil ,_fa ;};if _ee ,_aeb :=_gec .V .(*_c .PdfObjectString );_aeb {_f =append (_f ,fieldValue {Name :_egc ,Value :_ee .Decoded ()});continue ;};var _fe string ;for _ ,_fc :=range _gec .Annotations {_gf ,_dg :=_c .GetName (_fc .AS );
|
|
if _dg {_fe =_gf .String ();};_gd ,_agg :=_c .GetDict (_fc .AP );if !_agg {continue ;};_cfd ,_ :=_c .GetDict (_gd .Get ("\u004e"));for _ ,_cb :=range _cfd .Keys (){_fef :=_cb .String ();if _ ,_ga :=_eg [_fef ];!_ga {_cf =append (_cf ,_fef );_eg [_fef ]=struct{}{};
|
|
};};_gecc ,_ :=_c .GetDict (_gd .Get ("\u0044"));for _ ,_af :=range _gecc .Keys (){_bbg :=_af .String ();if _ ,_aee :=_eg [_bbg ];!_aee {_cf =append (_cf ,_bbg );_eg [_bbg ]=struct{}{};};};};_da :=fieldValue {Name :_egc ,Value :_fe ,Options :_cf };_f =append (_f ,_da );
|
|
};_bc :=FieldData {_bd :_f };return &_bc ,nil ;};
|
|
|
|
// FieldValues implements model.FieldValueProvider interface.
|
|
func (_gaa *FieldData )FieldValues ()(map[string ]_c .PdfObject ,error ){_gfc :=make (map[string ]_c .PdfObject );for _ ,_bae :=range _gaa ._bd {if len (_bae .Value )> 0{_gfc [_bae .Name ]=_c .MakeString (_bae .Value );};};return _gfc ,nil ;};
|
|
|
|
// SetImageFromFile assign image file to a specific field identified by fieldName.
|
|
func (_cff *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_ecg ,_afa :=_e .Open (imagePath );if _afa !=nil {return _afa ;};defer _ecg .Close ();_afb ,_afa :=_ae .ImageHandling .Read (_ecg );if _afa !=nil {_a .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_afa );
|
|
return _afa ;};return _cff .SetImage (fieldName ,_afb ,opt );};
|
|
|
|
// LoadFromJSONFile loads form field data from a JSON file.
|
|
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_ba ,_ge :=_e .Open (filePath );if _ge !=nil {return nil ,_ge ;};defer _ba .Close ();return LoadFromJSON (_ba );};
|
|
|
|
// LoadFromPDFFile loads form field data from a PDF file.
|
|
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_aca ,_dfd :=_e .Open (filePath );if _dfd !=nil {return nil ,_dfd ;};defer _aca .Close ();return LoadFromPDF (_aca );}; |