mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-29 13:48:54 +08:00
52 lines
4.4 KiB
Go
52 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 (_d "encoding/json";_gb "github.com/unidoc/unipdf/v3/common";_da "github.com/unidoc/unipdf/v3/core";_aa "github.com/unidoc/unipdf/v3/model";_gg "io";_a "os";);type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
|
|
ImageValue *_aa .Image `json:"-"`;
|
|
|
|
// Options lists allowed values if present.
|
|
Options []string `json:"options,omitempty"`;};
|
|
|
|
// FieldValues implements model.FieldValueProvider interface.
|
|
func (_aeg *FieldData )FieldValues ()(map[string ]_da .PdfObject ,error ){_eg :=make (map[string ]_da .PdfObject );for _ ,_agg :=range _aeg ._de {if len (_agg .Value )> 0{_eg [_agg .Name ]=_da .MakeString (_agg .Value );};};return _eg ,nil ;};
|
|
|
|
// LoadFromPDFFile loads form field data from a PDF file.
|
|
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_dabf ,_ba :=_a .Open (filePath );if _ba !=nil {return nil ,_ba ;};defer _dabf .Close ();return LoadFromPDF (_dabf );};
|
|
|
|
// SetImageFromFile assign image file to a specific field identified by fieldName.
|
|
func (_fd *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_edd ,_bgd :=_a .Open (imagePath );if _bgd !=nil {return _bgd ;};defer _edd .Close ();_cgd ,_bgd :=_aa .ImageHandling .Read (_edd );if _bgd !=nil {_gb .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_bgd );
|
|
return _bgd ;};return _fd .SetImage (fieldName ,_cgd ,opt );};
|
|
|
|
// LoadFromJSON loads JSON form data from `r`.
|
|
func LoadFromJSON (r _gg .Reader )(*FieldData ,error ){var _f FieldData ;_fb :=_d .NewDecoder (r ).Decode (&_f ._de );if _fb !=nil {return nil ,_fb ;};return &_f ,nil ;};
|
|
|
|
// FieldImageValues implements model.FieldImageProvider interface.
|
|
func (_abgd *FieldData )FieldImageValues ()(map[string ]*_aa .Image ,error ){_ad :=make (map[string ]*_aa .Image );for _ ,_egc :=range _abgd ._de {if _egc .ImageValue !=nil {_ad [_egc .Name ]=_egc .ImageValue ;};};return _ad ,nil ;};
|
|
|
|
// SetImage assign model.Image to a specific field identified by fieldName.
|
|
func (_acf *FieldData )SetImage (fieldName string ,img *_aa .Image ,opt []string )error {_gbd :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_acf ._de =append (_acf ._de ,_gbd );return nil ;};
|
|
|
|
// LoadFromPDF loads form field data from a PDF.
|
|
func LoadFromPDF (rs _gg .ReadSeeker )(*FieldData ,error ){_dd ,_b :=_aa .NewPdfReader (rs );if _b !=nil {return nil ,_b ;};if _dd .AcroForm ==nil {return nil ,nil ;};var _bd []fieldValue ;_ac :=_dd .AcroForm .AllFields ();for _ ,_e :=range _ac {var _cb []string ;
|
|
_bc :=make (map[string ]struct{});_ab ,_ggf :=_e .FullName ();if _ggf !=nil {return nil ,_ggf ;};if _ed ,_fba :=_e .V .(*_da .PdfObjectString );_fba {_bd =append (_bd ,fieldValue {Name :_ab ,Value :_ed .Decoded ()});continue ;};var _gc string ;for _ ,_dce :=range _e .Annotations {_dad ,_bf :=_da .GetName (_dce .AS );
|
|
if _bf {_gc =_dad .String ();};_ce ,_fe :=_da .GetDict (_dce .AP );if !_fe {continue ;};_ca ,_ :=_da .GetDict (_ce .Get ("\u004e"));for _ ,_db :=range _ca .Keys (){_cba :=_db .String ();if _ ,_cg :=_bc [_cba ];!_cg {_cb =append (_cb ,_cba );_bc [_cba ]=struct{}{};
|
|
};};_ag ,_ :=_da .GetDict (_ce .Get ("\u0044"));for _ ,_cd :=range _ag .Keys (){_cc :=_cd .String ();if _ ,_abg :=_bc [_cc ];!_abg {_cb =append (_cb ,_cc );_bc [_cc ]=struct{}{};};};};_dab :=fieldValue {Name :_ab ,Value :_gc ,Options :_cb };_bd =append (_bd ,_dab );
|
|
};_ggd :=FieldData {_de :_bd };return &_ggd ,nil ;};
|
|
|
|
// FieldData represents form field data loaded from JSON file.
|
|
type FieldData struct{_de []fieldValue };
|
|
|
|
// LoadFromJSONFile loads form field data from a JSON file.
|
|
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_c ,_dc :=_a .Open (filePath );if _dc !=nil {return nil ,_dc ;};defer _c .Close ();return LoadFromJSON (_c );};
|
|
|
|
// JSON returns the field data as a string in JSON format.
|
|
func (_dda FieldData )JSON ()(string ,error ){_ccf ,_df :=_d .MarshalIndent (_dda ._de ,"","\u0020\u0020\u0020\u0020");return string (_ccf ),_df ;}; |