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.
|
2024-12-20 06:39:10 +00:00
|
|
|
package fjson ;import (_c "encoding/json";_b "github.com/unidoc/unipdf/v3/common";_cd "github.com/unidoc/unipdf/v3/core";_ad "github.com/unidoc/unipdf/v3/model";_e "io";_a "os";);type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
|
|
|
|
ImageValue *_ad .Image `json:"-"`;
|
2024-10-21 17:50:33 +00:00
|
|
|
|
2024-12-20 06:39:10 +00:00
|
|
|
// Options lists allowed values if present.
|
|
|
|
Options []string `json:"options,omitempty"`;};
|
2024-09-14 00:10:30 +00:00
|
|
|
|
2024-12-20 06:39:10 +00:00
|
|
|
// LoadFromJSONFile loads form field data from a JSON file.
|
|
|
|
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_ge ,_d :=_a .Open (filePath );if _d !=nil {return nil ,_d ;};defer _ge .Close ();return LoadFromJSON (_ge );};
|
2024-10-21 17:50:33 +00:00
|
|
|
|
2024-11-22 00:42:43 +00:00
|
|
|
// FieldImageValues implements model.FieldImageProvider interface.
|
2024-12-20 06:39:10 +00:00
|
|
|
func (_cde *FieldData )FieldImageValues ()(map[string ]*_ad .Image ,error ){_gcg :=make (map[string ]*_ad .Image );for _ ,_bf :=range _cde ._g {if _bf .ImageValue !=nil {_gcg [_bf .Name ]=_bf .ImageValue ;};};return _gcg ,nil ;};
|
2024-04-16 11:40:43 +00:00
|
|
|
|
2024-07-25 19:53:55 +00:00
|
|
|
// FieldValues implements model.FieldValueProvider interface.
|
2024-12-20 06:39:10 +00:00
|
|
|
func (_gc *FieldData )FieldValues ()(map[string ]_cd .PdfObject ,error ){_gfb :=make (map[string ]_cd .PdfObject );for _ ,_def :=range _gc ._g {if len (_def .Value )> 0{_gfb [_def .Name ]=_cd .MakeString (_def .Value );};};return _gfb ,nil ;};
|
2024-06-27 16:15:49 +00:00
|
|
|
|
2024-12-20 06:39:10 +00:00
|
|
|
// SetImage assign model.Image to a specific field identified by fieldName.
|
|
|
|
func (_bee *FieldData )SetImage (fieldName string ,img *_ad .Image ,opt []string )error {_ffc :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_bee ._g =append (_bee ._g ,_ffc );return nil ;};
|
2024-10-21 17:50:33 +00:00
|
|
|
|
|
|
|
// JSON returns the field data as a string in JSON format.
|
2024-12-20 06:39:10 +00:00
|
|
|
func (_fb FieldData )JSON ()(string ,error ){_ffb ,_fac :=_c .MarshalIndent (_fb ._g ,"","\u0020\u0020\u0020\u0020");return string (_ffb ),_fac ;};
|
2024-11-22 00:42:43 +00:00
|
|
|
|
2024-12-20 06:39:10 +00:00
|
|
|
// LoadFromPDF loads form field data from a PDF.
|
|
|
|
func LoadFromPDF (rs _e .ReadSeeker )(*FieldData ,error ){_af ,_gea :=_ad .NewPdfReader (rs );if _gea !=nil {return nil ,_gea ;};if _af .AcroForm ==nil {return nil ,nil ;};var _db []fieldValue ;_ced :=_af .AcroForm .AllFields ();for _ ,_cfd :=range _ced {var _adb []string ;
|
|
|
|
_gd :=make (map[string ]struct{});_ab ,_ca :=_cfd .FullName ();if _ca !=nil {return nil ,_ca ;};if _cb ,_ec :=_cfd .V .(*_cd .PdfObjectString );_ec {_db =append (_db ,fieldValue {Name :_ab ,Value :_cb .Decoded ()});continue ;};var _da string ;for _ ,_cfg :=range _cfd .Annotations {_fd ,_gf :=_cd .GetName (_cfg .AS );
|
|
|
|
if _gf {_da =_fd .String ();};_edd ,_abc :=_cd .GetDict (_cfg .AP );if !_abc {continue ;};_fe ,_ :=_cd .GetDict (_edd .Get ("\u004e"));for _ ,_edde :=range _fe .Keys (){_bef :=_edde .String ();if _ ,_faa :=_gd [_bef ];!_faa {_adb =append (_adb ,_bef );
|
|
|
|
_gd [_bef ]=struct{}{};};};_aff ,_ :=_cd .GetDict (_edd .Get ("\u0044"));for _ ,_gfe :=range _aff .Keys (){_ff :=_gfe .String ();if _ ,_bec :=_gd [_ff ];!_bec {_adb =append (_adb ,_ff );_gd [_ff ]=struct{}{};};};};_fff :=fieldValue {Name :_ab ,Value :_da ,Options :_adb };
|
|
|
|
_db =append (_db ,_fff );};_fffg :=FieldData {_g :_db };return &_fffg ,nil ;};
|
2024-06-27 16:15:49 +00:00
|
|
|
|
2024-12-20 06:39:10 +00:00
|
|
|
// LoadFromJSON loads JSON form data from `r`.
|
|
|
|
func LoadFromJSON (r _e .Reader )(*FieldData ,error ){var _ce FieldData ;_ed :=_c .NewDecoder (r ).Decode (&_ce ._g );if _ed !=nil {return nil ,_ed ;};return &_ce ,nil ;};
|
2024-07-25 19:53:55 +00:00
|
|
|
|
2024-12-20 06:39:10 +00:00
|
|
|
// SetImageFromFile assign image file to a specific field identified by fieldName.
|
|
|
|
func (_bd *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_dea ,_ee :=_a .Open (imagePath );if _ee !=nil {return _ee ;};defer _dea .Close ();_aa ,_ee :=_ad .ImageHandling .Read (_dea );if _ee !=nil {_b .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_ee );
|
|
|
|
return _ee ;};return _bd .SetImage (fieldName ,_aa ,opt );};
|
2024-04-16 11:40:43 +00:00
|
|
|
|
2024-12-20 06:39:10 +00:00
|
|
|
// FieldData represents form field data loaded from JSON file.
|
|
|
|
type FieldData struct{_g []fieldValue };
|
|
|
|
|
|
|
|
// LoadFromPDFFile loads form field data from a PDF file.
|
|
|
|
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_fg ,_ece :=_a .Open (filePath );if _ece !=nil {return nil ,_ece ;};defer _fg .Close ();return LoadFromPDF (_fg );};
|