unipdf/fjson/fjson.go
2020-08-27 21:45:09 +00:00

37 lines
3.2 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 (_c "encoding/json";_d "github.com/unidoc/unipdf/v3/core";_cg "github.com/unidoc/unipdf/v3/model";_fe "io";_f "os";);
// JSON returns the field data as a string in JSON format.
func (_dg FieldData )JSON ()(string ,error ){_eg ,_cde :=_c .MarshalIndent (_dg ._gf ,"","\u0020\u0020\u0020\u0020");return string (_eg ),_cde ;};
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _fe .ReadSeeker )(*FieldData ,error ){_de ,_cb :=_cg .NewPdfReader (rs );if _cb !=nil {return nil ,_cb ;};if _de .AcroForm ==nil {return nil ,nil ;};var _b []fieldValue ;_dc :=_de .AcroForm .AllFields ();for _ ,_bf :=range _dc {var _a []string ;_ba :=make (map[string ]struct{});_fec ,_deg :=_bf .FullName ();if _deg !=nil {return nil ,_deg ;};if _bd ,_gg :=_bf .V .(*_d .PdfObjectString );_gg {_b =append (_b ,fieldValue {Name :_fec ,Value :_bd .Decoded ()});continue ;};var _be string ;for _ ,_dcf :=range _bf .Annotations {_e ,_ag :=_d .GetName (_dcf .AS );if _ag {_be =_e .String ();};_bc ,_cgg :=_d .GetDict (_dcf .AP );if !_cgg {continue ;};_gfa ,_ :=_d .GetDict (_bc .Get ("\u004e"));for _ ,_ac :=range _gfa .Keys (){_dea :=_ac .String ();if _ ,_gfe :=_ba [_dea ];!_gfe {_a =append (_a ,_dea );_ba [_dea ]=struct{}{};};};_cdf ,_ :=_d .GetDict (_bc .Get ("\u0044"));for _ ,_acb :=range _cdf .Keys (){_ec :=_acb .String ();if _ ,_ga :=_ba [_ec ];!_ga {_a =append (_a ,_ec );_ba [_ec ]=struct{}{};};};};_cgaa :=fieldValue {Name :_fec ,Value :_be ,Options :_a };_b =append (_b ,_cgaa );};_age :=FieldData {_gf :_b };return &_age ,nil ;};
// LoadFromJSON loads JSON form data from `r`.
func LoadFromJSON (r _fe .Reader )(*FieldData ,error ){var _cd FieldData ;_fc :=_c .NewDecoder (r ).Decode (&_cd ._gf );if _fc !=nil {return nil ,_fc ;};return &_cd ,nil ;};type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_ce ,_gb :=_f .Open (filePath );if _gb !=nil {return nil ,_gb ;};defer _ce .Close ();return LoadFromJSON (_ce );};
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_gf []fieldValue };
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_gc ,_aa :=_f .Open (filePath );if _aa !=nil {return nil ,_aa ;};defer _gc .Close ();return LoadFromPDF (_gc );};
// FieldValues implements model.FieldValueProvider interface.
func (_cgc *FieldData )FieldValues ()(map[string ]_d .PdfObject ,error ){_cdc :=make (map[string ]_d .PdfObject );for _ ,_fd :=range _cgc ._gf {if len (_fd .Value )> 0{_cdc [_fd .Name ]=_d .MakeString (_fd .Value );};};return _cdc ,nil ;};