diff options
| author | leshe4ka46 <alex9102naid1@ya.ru> | 2025-10-19 14:47:59 +0300 |
|---|---|---|
| committer | leshe4ka46 <alex9102naid1@ya.ru> | 2025-10-19 14:47:59 +0300 |
| commit | e17a425dfb3382310fb5863f516dacdca9f44956 (patch) | |
| tree | 7babb3048d0eb20aa1e68e9b29c2acfa794ab96f /unipdf/fjson/fjson.go | |
| parent | 091963a50c3bb2926f559f01c49e8f5bd03d2bfd (diff) | |
fuck the unipdf licencing
Diffstat (limited to 'unipdf/fjson/fjson.go')
| -rw-r--r-- | unipdf/fjson/fjson.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/unipdf/fjson/fjson.go b/unipdf/fjson/fjson.go new file mode 100644 index 0000000..5b3968a --- /dev/null +++ b/unipdf/fjson/fjson.go @@ -0,0 +1,52 @@ +// +// 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 (_bf "encoding/json";_f "github.com/unidoc/unipdf/v4/common";_aa "github.com/unidoc/unipdf/v4/core";_d "github.com/unidoc/unipdf/v4/model";_ae "io";_a "os";); + +// LoadFromPDF loads form field data from a PDF. +func LoadFromPDF (rs _ae .ReadSeeker )(*FieldData ,error ){_bd ,_ef :=_d .NewPdfReader (rs );if _ef !=nil {return nil ,_ef ;};if _bd .AcroForm ==nil {return nil ,nil ;};var _dbc []fieldValue ;_ce :=_bd .AcroForm .AllFields ();for _ ,_ag :=range _ce {var _ba []string ; +_ec :=make (map[string ]struct{});_baa ,_gb :=_ag .FullName ();if _gb !=nil {return nil ,_gb ;};if _bdd ,_gd :=_ag .V .(*_aa .PdfObjectString );_gd {_dbc =append (_dbc ,fieldValue {Name :_baa ,Value :_bdd .Decoded ()});continue ;};var _gbb string ;for _ ,_fa :=range _ag .Annotations {_ca ,_ac :=_aa .GetName (_fa .AS ); +if _ac {_gbb =_ca .String ();};_fc ,_cc :=_aa .GetDict (_fa .AP );if !_cc {continue ;};_cf ,_ :=_aa .GetDict (_fc .Get ("\u004e"));for _ ,_ea :=range _cf .Keys (){_acd :=_ea .String ();if _ ,_dbe :=_ec [_acd ];!_dbe {_ba =append (_ba ,_acd );_ec [_acd ]=struct{}{}; +};};_eb ,_ :=_aa .GetDict (_fc .Get ("\u0044"));for _ ,_ffa :=range _eb .Keys (){_gg :=_ffa .String ();if _ ,_cb :=_ec [_gg ];!_cb {_ba =append (_ba ,_gg );_ec [_gg ]=struct{}{};};};};_de :=fieldValue {Name :_baa ,Value :_gbb ,Options :_ba };_dbc =append (_dbc ,_de ); +};_ffb :=FieldData {_db :_dbc };return &_ffb ,nil ;}; + +// FieldData represents form field data loaded from JSON file. +type FieldData struct{_db []fieldValue }; + +// FieldValues implements model.FieldValueProvider interface. +func (_egb *FieldData )FieldValues ()(map[string ]_aa .PdfObject ,error ){_ee :=make (map[string ]_aa .PdfObject );for _ ,_af :=range _egb ._db {if len (_af .Value )> 0{_ee [_af .Name ]=_aa .MakeString (_af .Value );};};return _ee ,nil ;}; + +// LoadFromPDFFile loads form field data from a PDF file. +func LoadFromPDFFile (filePath string )(*FieldData ,error ){_bb ,_acdd :=_a .Open (filePath );if _acdd !=nil {return nil ,_acdd ;};defer _bb .Close ();return LoadFromPDF (_bb );}; + +// FieldImageValues implements model.FieldImageProvider interface. +func (_fd *FieldData )FieldImageValues ()(map[string ]*_d .Image ,error ){_ebg :=make (map[string ]*_d .Image );for _ ,_ad :=range _fd ._db {if _ad .ImageValue !=nil {_ebg [_ad .Name ]=_ad .ImageValue ;};};return _ebg ,nil ;}; + +// SetImage assign model.Image to a specific field identified by fieldName. +func (_aeec *FieldData )SetImage (fieldName string ,img *_d .Image ,opt []string )error {_cfd :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_aeec ._db =append (_aeec ._db ,_cfd );return nil ;};type fieldValue struct{Name string `json:"name"`; +Value string `json:"value"`;ImageValue *_d .Image `json:"-"`; + +// Options lists allowed values if present. +Options []string `json:"options,omitempty"`;}; + +// LoadFromJSON loads JSON form data from `r`. +func LoadFromJSON (r _ae .Reader )(*FieldData ,error ){var _c FieldData ;_g :=_bf .NewDecoder (r ).Decode (&_c ._db );if _g !=nil {return nil ,_g ;};return &_c ,nil ;}; + +// LoadFromJSONFile loads form field data from a JSON file. +func LoadFromJSONFile (filePath string )(*FieldData ,error ){_e ,_bc :=_a .Open (filePath );if _bc !=nil {return nil ,_bc ;};defer _e .Close ();return LoadFromJSON (_e );}; + +// SetImageFromFile assign image file to a specific field identified by fieldName. +func (_fe *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_eag ,_bcd :=_a .Open (imagePath );if _bcd !=nil {return _bcd ;};defer _eag .Close ();_cbf ,_bcd :=_d .ImageHandling .Read (_eag );if _bcd !=nil {_f .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_bcd ); +return _bcd ;};return _fe .SetImage (fieldName ,_cbf ,opt );}; + +// JSON returns the field data as a string in JSON format. +func (_cd FieldData )JSON ()(string ,error ){_gba ,_gf :=_bf .MarshalIndent (_cd ._db ,"","\u0020\u0020\u0020\u0020");return string (_gba ),_gf ;};
\ No newline at end of file |
