unipdf/annotator/annotator.go

191 lines
52 KiB
Go
Raw Normal View History

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 annotator provides an interface for creating annotations with appearance
// streams. It goes beyond the models package which includes definitions of basic annotation models, in that it
// can create the appearance streams which specify the exact appearance as needed by many pdf viewers for consistent
// appearance of the annotations.
// It also contains methods for generating appearance streams for fields via widget annotations.
2020-10-12 14:17:59 +00:00
package annotator ;import (_f "bytes";_ac "errors";_ed "github.com/unidoc/unipdf/v3/common";_ec "github.com/unidoc/unipdf/v3/contentstream";_gd "github.com/unidoc/unipdf/v3/contentstream/draw";_bf "github.com/unidoc/unipdf/v3/core";_c "github.com/unidoc/unipdf/v3/internal/textencoding";_g "github.com/unidoc/unipdf/v3/model";_d "math";_a "strings";_b "unicode";);func _ccc (_dgb *_g .PdfAnnotationWidget ,_fda *_g .PdfFieldText ,_cde *_g .PdfPageResources ,_dad AppearanceStyle )(*_bf .PdfObjectDictionary ,error ){_dgbb :=_g .NewPdfPageResources ();_ceb ,_bged :=_bf .GetArray (_dgb .Rect );if !_bged {return nil ,_ac .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_acbf ,_cdbc :=_g .NewPdfRectangle (*_ceb );if _cdbc !=nil {return nil ,_cdbc ;};_eecf ,_aa :=_acbf .Width (),_acbf .Height ();_dfda ,_dfe :=_eecf ,_aa ;_bcb ,_ggdc :=_bf .GetDict (_dgb .MK );if _ggdc {_cb ,_ :=_bf .GetDict (_dgb .BS );_deac :=_dad .applyAppearanceCharacteristics (_bcb ,_cb ,nil );if _deac !=nil {return nil ,_deac ;};};_fag ,_ggdc :=_bf .GetIntVal (_fda .MaxLen );if !_ggdc {return nil ,_ac .New ("\u006d\u0061\u0078\u006c\u0065\u006e\u0020\u006e\u006ft\u0020\u0073\u0065\u0074");};if _fag <=0{return nil ,_ac .New ("\u006d\u0061\u0078\u004c\u0065\u006e\u0020\u0069\u006ev\u0061\u006c\u0069\u0064");};_bed :=float64 (_eecf )/float64 (_fag );_bfc ,_cdbc :=_ec .NewContentStreamParser (_ffe (_fda .PdfField )).Parse ();if _cdbc !=nil {return nil ,_cdbc ;};_bbe :=_ec .NewContentCreator ();if _dad .BorderSize > 0{_gbb (_bbe ,_dad ,_eecf ,_aa );};if _dad .DrawAlignmentReticle {_egg :=_dad ;_egg .BorderSize =0.2;_dcb (_bbe ,_egg ,_eecf ,_aa );};_bbe .Add_BMC ("\u0054\u0078");_bbe .Add_q ();_eecf ,_aa =_dad .applyRotation (_bcb ,_eecf ,_aa ,_bbe );_bbe .Add_BT ();_cad ,_edf ,_cdbc :=_dad .processDA (_fda .PdfField ,_bfc ,_cde ,_dgbb ,_bbe );if _cdbc !=nil {return nil ,_cdbc ;};_fcc :=_cad .Font ;_bbc :=_bf .MakeName (_cad .Name );_bad :=_cad .Size ;_gbed :=_bad ==0;if _gbed &&_edf {_bad =_aa *_dad .AutoFontSizeFraction ;};_ga :=_fcc .Encoder ();if _ga ==nil {_ed .Log .Debug ("\u0057\u0041RN\u003a\u0020\u0066\u006f\u006e\u0074\u0020\u0065\u006e\u0063\u006f\u0064\u0065\u0072\u0020\u0069\u0073\u0020\u006e\u0069l\u002e\u0020\u0041\u0073s\u0075\u006d\u0069\u006eg \u0069\u0064e\u006et\u0069\u0074\u0079\u0020\u0065\u006ec\u006f\u0064\u0065r\u002e\u0020O\u0075\u0074\u0070\u0075\u0074\u0020\u006d\u0061\u0079\u0020\u0062\u0065\u0020\u0069n\u0063\u006f\u0072\u0072\u0065\u0063\u0074\u002e");_ga =_c .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};var _gdbf string ;if _abb ,_eff :=_bf .GetString (_fda .V );_eff {_gdbf =_abb .Decoded ();};_bbe .Add_Tf (*_bbc ,_bad );var _bgg float64 ;for _ ,_acfe :=range _gdbf {_dbgb ,_ffb :=_fcc .GetRuneMetrics (_acfe );if !_ffb {_ed .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u003a \u0052\u0075\u006e\u0065\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064\u0020\u0069\u006e\u0020\u0066\u006fn\u0074\u003a\u0020\u0025\u0076\u0020\u002d\u0020\u0073\u006b\u0069\u0070\u0070\u0069n\u0067 \u006f\u0076\u0065\u0072",_acfe );continue ;};_gfg :=_dbgb .Wy ;if int (_gfg )<=0{_gfg =_dbgb .Wx ;};if _gfg > _bgg {_bgg =_gfg ;};};if int (_bgg )==0{_ed .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u003a\u0020\u0055\u006e\u0061\u0062\u006c\u0065\u0020\u0074o\u0020\u0064\u0065\u0074\u0065\u0072\u006d\u0069\u006e\u0065\u0020\u006d\u0061x\u0020\u0067\u006c\u0079\u0070\u0068\u0020\u0073\u0069\u007a\u0065\u0020- \u0075\u0073\u0069\u006e\u0067\u0020\u0031\u0030\u0030\u0030");_bgg =1000;};_cfb ,_cdbc :=_fcc .GetFontDescriptor ();if _cdbc !=nil {_ed .Log .Debug ("\u0045\u0072ro\u0072\u003a\u0020U\u006e\u0061\u0062\u006ce t\u006f g\u0065\u0074\u0020\u0066\u006f\u006e\u0074 d\u0065\u0073\u0063\u0072\u0069\u0070\u0074o\u0072");};var _ggb float64 ;if _cfb !=nil {_ggb ,_cdbc =_cfb .GetCapHeight ();if _cdbc !=nil {_ed .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u003a\u0020\u0055\u006e\u0061\u0062\u006c\u0065 \u0074\u006f\u0020\u0067\u0065\u0074 \u0066\u006f\u006e\u0074\u0020\u0043\u0061\u0070\
2020-10-05 19:28:24 +00:00
// AppearanceStyle defines style parameters for appearance stream generation.
type AppearanceStyle struct{
// How much of Rect height to fill when autosizing text.
AutoFontSizeFraction float64 ;
// CheckmarkRune is a rune used for check mark in checkboxes (for ZapfDingbats font).
2020-10-12 14:17:59 +00:00
CheckmarkRune rune ;BorderSize float64 ;BorderColor _g .PdfColor ;FillColor _g .PdfColor ;
2020-10-05 19:28:24 +00:00
// Multiplier for lineheight for multi line text.
MultilineLineHeight float64 ;MultilineVAlignMiddle bool ;
// Visual guide checking alignment of field contents (debugging).
DrawAlignmentReticle bool ;
// Allow field MK appearance characteristics to override style settings.
AllowMK bool ;
// Fonts holds appearance styles for fonts.
2020-10-12 14:17:59 +00:00
Fonts *AppearanceFontStyle ;};func _ea (_bg CircleAnnotationDef )(*_bf .PdfObjectDictionary ,*_g .PdfRectangle ,error ){_bfb :=_g .NewXObjectForm ();_bfb .Resources =_g .NewPdfPageResources ();_gb :="";if _bg .Opacity < 1.0{_aed :=_bf .MakeDict ();_aed .Set ("\u0063\u0061",_bf .MakeFloat (_bg .Opacity ));_aed .Set ("\u0043\u0041",_bf .MakeFloat (_bg .Opacity ));_be :=_bfb .Resources .AddExtGState ("\u0067\u0073\u0031",_aed );if _be !=nil {_ed .Log .Debug ("U\u006e\u0061\u0062\u006c\u0065\u0020t\u006f\u0020\u0061\u0064\u0064\u0020\u0065\u0078\u0074g\u0073\u0074\u0061t\u0065 \u0067\u0073\u0031");return nil ,nil ,_be ;};_gb ="\u0067\u0073\u0031";};_cf ,_ecd ,_cfc ,_cab :=_ee (_bg ,_gb );if _cab !=nil {return nil ,nil ,_cab ;};_cab =_bfb .SetContentStream (_cf ,nil );if _cab !=nil {return nil ,nil ,_cab ;};_bfb .BBox =_ecd .ToPdfObject ();_dd :=_bf .MakeDict ();_dd .Set ("\u004e",_bfb .ToPdfObject ());return _dd ,_cfc ,nil ;};
2020-10-05 19:28:24 +00:00
2020-10-12 14:17:59 +00:00
// NewSignatureFieldOpts returns a new initialized instance of options
// used to generate a signature appearance.
func NewSignatureFieldOpts ()*SignatureFieldOpts {return &SignatureFieldOpts {Font :_g .DefaultFont (),FontSize :10,LineHeight :1,AutoSize :true ,TextColor :_g .NewPdfColorDeviceGray (0),BorderColor :_g .NewPdfColorDeviceGray (0),FillColor :_g .NewPdfColorDeviceGray (1)};};func _ee (_ef CircleAnnotationDef ,_baf string )([]byte ,*_g .PdfRectangle ,*_g .PdfRectangle ,error ){_da :=_gd .Circle {X :_ef .X ,Y :_ef .Y ,Width :_ef .Width ,Height :_ef .Height ,FillEnabled :_ef .FillEnabled ,FillColor :_ef .FillColor ,BorderEnabled :_ef .BorderEnabled ,BorderWidth :_ef .BorderWidth ,BorderColor :_ef .BorderColor ,Opacity :_ef .Opacity };_cae ,_gca ,_bfa :=_da .Draw (_baf );if _bfa !=nil {return nil ,nil ,nil ,_bfa ;};_bc :=&_g .PdfRectangle {};_bc .Llx =_ef .X +_gca .Llx ;_bc .Lly =_ef .Y +_gca .Lly ;_bc .Urx =_ef .X +_gca .Urx ;_bc .Ury =_ef .Y +_gca .Ury ;return _cae ,_gca ,_bc ,nil ;};
2020-09-21 01:20:10 +00:00
2020-10-05 19:28:24 +00:00
// TextFieldOptions defines optional parameter for a text field in a form.
2020-10-12 14:17:59 +00:00
type TextFieldOptions struct{MaxLen int ;Value string ;};func _ddgf (_efdf []*SignatureLine ,_gcee *SignatureFieldOpts )(*_bf .PdfObjectDictionary ,error ){if _gcee ==nil {_gcee =NewSignatureFieldOpts ();};var _cgea error ;var _fdg *_bf .PdfObjectName ;_ceee :=_gcee .Font ;if _ceee !=nil {_dggd ,_ :=_ceee .GetFontDescriptor ();if _dggd !=nil {if _fad ,_aac :=_dggd .FontName .(*_bf .PdfObjectName );_aac {_fdg =_fad ;};};if _fdg ==nil {_fdg =_bf .MakeName ("\u0046\u006f\u006et\u0031");};}else {if _ceee ,_cgea =_g .NewStandard14Font ("\u0048e\u006c\u0076\u0065\u0074\u0069\u0063a");_cgea !=nil {return nil ,_cgea ;};_fdg =_bf .MakeName ("\u0048\u0065\u006c\u0076");};_fegc :=_gcee .FontSize ;if _fegc <=0{_fegc =10;};if _gcee .LineHeight <=0{_gcee .LineHeight =1;};_deacf :=_gcee .LineHeight *_fegc ;_cbdd ,_cdec :=_ceee .GetRuneMetrics (' ');if !_cdec {return nil ,_ac .New ("\u0074\u0068e \u0066\u006f\u006et\u0020\u0064\u006f\u0065s n\u006ft \u0068\u0061\u0076\u0065\u0020\u0061\u0020sp\u0061\u0063\u0065\u0020\u0067\u006c\u0079p\u0068");};_becb :=_cbdd .Wx ;var _fgaf float64 ;var _gbea []string ;for _ ,_fef :=range _efdf {if _fef .Text ==""{continue ;};_ccgdd :=_fef .Text ;if _fef .Desc !=""{_ccgdd =_fef .Desc +"\u003a\u0020"+_ccgdd ;};_gbea =append (_gbea ,_ccgdd );var _cgf float64 ;for _ ,_daga :=range _ccgdd {_cbc ,_fcac :=_ceee .GetRuneMetrics (_daga );if !_fcac {continue ;};_cgf +=_cbc .Wx ;};if _cgf > _fgaf {_fgaf =_cgf ;};};_fgaf =_fgaf *_fegc /1000.0;_effg :=float64 (len (_gbea ))*_deacf ;_dcg :=_gcee .Rect ;if _dcg ==nil {_dcg =[]float64 {0,0,_fgaf ,_effg };_gcee .Rect =_dcg ;};_gbbf :=_dcg [2]-_dcg [0];_ggdg :=_dcg [3]-_dcg [1];var _eggb float64 ;if _gcee .AutoSize {if _fgaf > _gbbf ||_effg > _ggdg {_gdbc :=_d .Min (_gbbf /_fgaf ,_ggdg /_effg );_fegc *=_gdbc ;};_deacf =_gcee .LineHeight *_fegc ;_eggb +=(_ggdg -float64 (len (_gbea ))*_deacf )/2;};_beda :=_ec .NewContentCreator ();if _gcee .BorderSize <=0{_gcee .BorderSize =0;_gcee .BorderColor =_g .NewPdfColorDeviceGray (1);};if _gcee .BorderColor ==nil {_gcee .BorderColor =_g .NewPdfColorDeviceGray (1);};if _gcee .FillColor ==nil {_gcee .FillColor =_g .NewPdfColorDeviceGray (1);};_beda .Add_q ().SetNonStrokingColor (_gcee .FillColor ).SetStrokingColor (_gcee .BorderColor ).Add_w (_gcee .BorderSize ).Add_re (_dcg [0],_dcg [1],_gbbf ,_ggdg ).Add_B ().Add_Q ();_beda .Add_q ();_beda .Translate (_dcg [0],_dcg [3]-_deacf -_eggb );_beda .Add_BT ();_dga :=_ceee .Encoder ();for _ ,_gbeaf :=range _gbea {var _bdab []byte ;for _ ,_dadg :=range _gbeaf {if _b .IsSpace (_dadg ){if len (_bdab )> 0{_beda .SetNonStrokingColor (_gcee .TextColor ).Add_Tf (*_fdg ,_fegc ).Add_TL (_deacf ).Add_TJ ([]_bf .PdfObject {_bf .MakeStringFromBytes (_bdab )}...);_bdab =nil ;};_beda .Add_Tf (*_fdg ,_fegc ).Add_TL (_deacf ).Add_TJ ([]_bf .PdfObject {_bf .MakeFloat (-_becb )}...);}else {_bdab =append (_bdab ,_dga .Encode (string (_dadg ))...);};};if len (_bdab )> 0{_beda .SetNonStrokingColor (_gcee .TextColor ).Add_Tf (*_fdg ,_fegc ).Add_TL (_deacf ).Add_TJ ([]_bf .PdfObject {_bf .MakeStringFromBytes (_bdab )}...);};_beda .Add_Td (0,-_deacf );};_beda .Add_ET ();_beda .Add_Q ();_eeca :=_g .NewPdfPageResources ();_eeca .SetFontByName (*_fdg ,_ceee .ToPdfObject ());_ffc :=_g .NewXObjectForm ();_ffc .Resources =_eeca ;_ffc .BBox =_bf .MakeArrayFromFloats (_dcg );_ffc .SetContentStream (_beda .Bytes (),_aeb ());_gfec :=_bf .MakeDict ();_gfec .Set ("\u004e",_ffc .ToPdfObject ());return _gfec ,nil ;};func _gdcg (_cfcf RectangleAnnotationDef ,_fefe string )([]byte ,*_g .PdfRectangle ,*_g .PdfRectangle ,error ){_caac :=_gd .Rectangle {X :0,Y :0,Width :_cfcf .Width ,Height :_cfcf .Height ,FillEnabled :_cfcf .FillEnabled ,FillColor :_cfcf .FillColor ,BorderEnabled :_cfcf .BorderEnabled ,BorderWidth :2*_cfcf .BorderWidth ,BorderColor :_cfcf .BorderColor ,Opacity :_cfcf .Opacity };_baae ,_gab ,_abbg :=_caac .Draw (_fefe );if _abbg !=nil {return nil ,nil ,nil ,_abbg ;};_acff :=&_g .PdfRectangle {};_acff .Llx =_cfcf .X +_gab .Llx ;_acff .Lly =_cfcf .Y +_gab .Lly ;_acff .Urx =_cfcf .X +_gab .Urx ;_acff .Ury =_cfcf .Y +
2020-09-21 01:20:10 +00:00
2020-10-12 14:17:59 +00:00
// SignatureLine represents a line of information in the signature field appearance.
type SignatureLine struct{Desc string ;Text string ;};
// Style returns the appearance style of `fa`. If not specified, returns default style.
func (_deg FieldAppearance )Style ()AppearanceStyle {if _deg ._fd !=nil {return *_deg ._fd ;};return AppearanceStyle {AutoFontSizeFraction :0.65,CheckmarkRune :'✔',BorderSize :0.0,BorderColor :_g .NewPdfColorDeviceGray (0),FillColor :_g .NewPdfColorDeviceGray (1),MultilineLineHeight :1.2,MultilineVAlignMiddle :false ,DrawAlignmentReticle :false ,AllowMK :true };};
2020-09-21 01:20:10 +00:00
2020-10-05 19:28:24 +00:00
// NewSignatureField returns a new signature field with a visible appearance
// containing the specified signature lines and styled according to the
// specified options.
2020-10-12 14:17:59 +00:00
func NewSignatureField (signature *_g .PdfSignature ,lines []*SignatureLine ,opts *SignatureFieldOpts )(*_g .PdfFieldSignature ,error ){if signature ==nil {return nil ,_ac .New ("\u0073\u0069\u0067na\u0074\u0075\u0072\u0065\u0020\u0063\u0061\u006e\u006e\u006f\u0074\u0020\u0062\u0065\u0020\u006e\u0069\u006c");};_gedd ,_dbd :=_ddgf (lines ,opts );if _dbd !=nil {return nil ,_dbd ;};_cca :=_g .NewPdfFieldSignature (signature );_cca .Rect =_bf .MakeArrayFromFloats (opts .Rect );_cca .AP =_gedd ;return _cca ,nil ;};
2020-10-05 19:28:24 +00:00
2020-10-12 14:17:59 +00:00
// SetStyle applies appearance `style` to `fa`.
func (_de *FieldAppearance )SetStyle (style AppearanceStyle ){_de ._fd =&style };func _aeb ()_bf .StreamEncoder {return _bf .NewFlateEncoder ()};
2020-10-05 19:28:24 +00:00
2020-10-12 14:17:59 +00:00
// RectangleAnnotationDef is a rectangle defined with a specified Width and Height and a lower left corner at (X,Y).
// The rectangle can optionally have a border and a filling color.
// The Width/Height includes the border (if any specified).
type RectangleAnnotationDef struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;FillEnabled bool ;FillColor *_g .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_g .PdfColorDeviceRGB ;Opacity float64 ;};
// CreateCircleAnnotation creates a circle/ellipse annotation object with appearance stream that can be added to
// page PDF annotations.
func CreateCircleAnnotation (circDef CircleAnnotationDef )(*_g .PdfAnnotation ,error ){_bd :=_g .NewPdfAnnotationCircle ();if circDef .BorderEnabled {_gc ,_fa ,_cg :=circDef .BorderColor .R (),circDef .BorderColor .G (),circDef .BorderColor .B ();_bd .C =_bf .MakeArrayFromFloats ([]float64 {_gc ,_fa ,_cg });_ae :=_g .NewBorderStyle ();_ae .SetBorderWidth (circDef .BorderWidth );_bd .BS =_ae .ToPdfObject ();};if circDef .FillEnabled {_gf ,_ca ,_acf :=circDef .FillColor .R (),circDef .FillColor .G (),circDef .FillColor .B ();_bd .IC =_bf .MakeArrayFromFloats ([]float64 {_gf ,_ca ,_acf });}else {_bd .IC =_bf .MakeArrayFromIntegers ([]int {});};if circDef .Opacity < 1.0{_bd .CA =_bf .MakeFloat (circDef .Opacity );};_af ,_ba ,_ge :=_ea (circDef );if _ge !=nil {return nil ,_ge ;};_bd .AP =_af ;_bd .Rect =_bf .MakeArrayFromFloats ([]float64 {_ba .Llx ,_ba .Lly ,_ba .Urx ,_ba .Ury });return _bd .PdfAnnotation ,nil ;};func _dgbc (_afd *_g .PdfAcroForm ,_bbg *_g .PdfAnnotationWidget ,_gbge *_g .PdfFieldChoice ,_dfc AppearanceStyle )(*_bf .PdfObjectDictionary ,error ){_cccd ,_bfad :=_bf .GetArray (_bbg .Rect );if !_bfad {return nil ,_ac .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_gdg ,_bec :=_g .NewPdfRectangle (*_cccd );if _bec !=nil {return nil ,_bec ;};_gfdf ,_adad :=_gdg .Width (),_gdg .Height ();_ed .Log .Debug ("\u0043\u0068\u006f\u0069\u0063\u0065\u002c\u0020\u0077\u0061\u0020\u0042S\u003a\u0020\u0025\u0076",_bbg .BS );_fbc ,_bec :=_ec .NewContentStreamParser (_ffe (_gbge .PdfField )).Parse ();if _bec !=nil {return nil ,_bec ;};_aba ,_cec :=_bf .GetDict (_bbg .MK );if _cec {_accf ,_ :=_bf .GetDict (_bbg .BS );_aee :=_dfc .applyAppearanceCharacteristics (_aba ,_accf ,nil );if _aee !=nil {return nil ,_aee ;};};_agg :=_bf .MakeDict ();for _ ,_fae :=range _gbge .Opt .Elements (){if _daaf ,_ggg :=_bf .GetArray (_fae );_ggg &&_daaf .Len ()==2{_fae =_daaf .Get (1);};var _fdeg string ;if _bdf ,_adc :=_bf .GetString (_fae );_adc {_fdeg =_bdf .Decoded ();}else if _gcg ,_fcd :=_bf .GetName (_fae );_fcd {_fdeg =_gcg .String ();}else {_ed .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u003a \u004f\u0070\u0074\u0020\u006e\u006f\u0074\u0020\u0061\u0020\u006e\u0061\u006de\u002f\u0073\u0074\u0072\u0069\u006e\u0067 \u002d\u0020\u0025\u0054",_fae );return nil ,_ac .New ("\u006e\u006f\u0074\u0020\u0061\u0020\u006e\u0061\u006d\u0065\u002f\u0073t\u0072\u0069\u006e\u0067");};if len (_fdeg )> 0{_cgcb ,_eef :=_edd (_gbge .PdfField ,_gfdf ,_adad ,_fdeg ,_dfc ,_fbc ,_afd .DR ,_aba );if _eef !=nil {return nil ,_eef ;};_agg .Set (*_bf .MakeName (_fdeg ),_cgcb .ToPdfObject ());};};_cfbd :=_bf .MakeDict ();_cfbd .Set ("\u004e",_agg );return _cfbd ,nil ;};func _bge (_aca *_g .PdfAnnotationWidget ,_bfbf *_g .PdfFieldText ,_egf *_g .PdfPageResources ,_fe AppearanceStyle )(*_bf .PdfObjectDictionary ,error ){_gdc :=_g .NewPdfPageResources ();_fb ,_gdb :=_bf .GetArray (_aca .Rect );if !_gdb {return nil ,_ac .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_faa ,_dee :=_g .NewPdfRectangle (*_fb );if _dee !=nil {return nil ,_dee ;};_ggd ,_fga :=_faa .Width (),_faa .Height ();_afg ,_bafe :=_ggd ,_fga ;_cdb ,_efa :=_bf .GetDict (_aca .MK );if _efa {_gbg ,_ :=_bf .GetDict (_aca .BS );_ecc :=_fe .applyAppearanceCharacteristics (_cdb ,_gbg ,nil );if _ecc !=nil {return nil ,_ecc ;};};_dc ,_dee :=_ec .NewContentStreamParser (_ffe (_bfbf .PdfField )).Parse ();if _dee !=nil {return nil ,_dee ;};_faaa :=_ec .NewContentCreator ();if _fe .BorderSize > 0{_gbb (_faaa ,_fe ,_ggd ,_fga );};if _fe .DrawAlignmentReticle {_ccd :=_fe ;_ccd .BorderSize =0.2;_dcb (_faaa ,_ccd ,_ggd ,_fga );};_faaa .Add_BMC ("\u0054\u0078");_faaa .Add_q ();_ggd ,_fga =_fe .applyRotation (_cdb ,_ggd ,_fga ,_faaa );_faaa .Add_BT ();_gee ,_ged ,_dee :=_fe .processDA (_bfbf .PdfField ,_dc ,_egf ,_gdc ,_faaa );if _dee !=nil {return nil ,_dee ;};_ffg :=_gee .Font ;_gbe :=_gee .Size ;_fac :=_bf .MakeName (_gee .Name );_faf :=_gbe ==0;if _faf &&_ged {_gbe =_fga *_fe .AutoFontSizeFraction ;};_eec :=_ffg .Encoder ();if _eec ==nil {_ed .Log .Debug (
// CreateLineAnnotation creates a line annotation object that can be added to page PDF annotations.
func CreateLineAnnotation (lineDef LineAnnotationDef )(*_g .PdfAnnotation ,error ){_baa :=_g .NewPdfAnnotationLine ();_baa .L =_bf .MakeArrayFromFloats ([]float64 {lineDef .X1 ,lineDef .Y1 ,lineDef .X2 ,lineDef .Y2 });_faac :=_bf .MakeName ("\u004e\u006f\u006e\u0065");if lineDef .LineEndingStyle1 ==_gd .LineEndingStyleArrow {_faac =_bf .MakeName ("C\u006c\u006f\u0073\u0065\u0064\u0041\u0072\u0072\u006f\u0077");};_bcbg :=_bf .MakeName ("\u004e\u006f\u006e\u0065");if lineDef .LineEndingStyle2 ==_gd .LineEndingStyleArrow {_bcbg =_bf .MakeName ("C\u006c\u006f\u0073\u0065\u0064\u0041\u0072\u0072\u006f\u0077");};_baa .LE =_bf .MakeArray (_faac ,_bcbg );if lineDef .Opacity < 1.0{_baa .CA =_bf .MakeFloat (lineDef .Opacity );};_dfga ,_aeda ,_caba :=lineDef .LineColor .R (),lineDef .LineColor .G (),lineDef .LineColor .B ();_baa .IC =_bf .MakeArrayFromFloats ([]float64 {_dfga ,_aeda ,_caba });_baa .C =_bf .MakeArrayFromFloats ([]float64 {_dfga ,_aeda ,_caba });_cadd :=_g .NewBorderStyle ();_cadd .SetBorderWidth (lineDef .LineWidth );_baa .BS =_cadd .ToPdfObject ();_daede ,_aaf ,_fba :=_bee (lineDef );if _fba !=nil {return nil ,_fba ;};_baa .AP =_daede ;_baa .Rect =_bf .MakeArrayFromFloats ([]float64 {_aaf .Llx ,_aaf .Lly ,_aaf .Urx ,_aaf .Ury });return _baa .PdfAnnotation ,nil ;};func _bee (_cddg LineAnnotationDef )(*_bf .PdfObjectDictionary ,*_g .PdfRectangle ,error ){_eeaa :=_g .NewXObjectForm ();_eeaa .Resources =_g .NewPdfPageResources ();_ece :="";if _cddg .Opacity < 1.0{_gbgb :=_bf .MakeDict ();_gbgb .Set ("\u0063\u0061",_bf .MakeFloat (_cddg .Opacity ));_fgba :=_eeaa .Resources .AddExtGState ("\u0067\u0073\u0031",_gbgb );if _fgba !=nil {_ed .Log .Debug ("U\u006e\u0061\u0062\u006c\u0065\u0020t\u006f\u0020\u0061\u0064\u0064\u0020\u0065\u0078\u0074g\u0073\u0074\u0061t\u0065 \u0067\u0073\u0031");return nil ,nil ,_fgba ;};_ece ="\u0067\u0073\u0031";};_cddf ,_aaae ,_degc ,_feb :=_dfdae (_cddg ,_ece );if _feb !=nil {return nil ,nil ,_feb ;};_feb =_eeaa .SetContentStream (_cddf ,nil );if _feb !=nil {return nil ,nil ,_feb ;};_eeaa .BBox =_aaae .ToPdfObject ();_cdaa :=_bf .MakeDict ();_cdaa .Set ("\u004e",_eeaa .ToPdfObject ());return _cdaa ,_degc ,nil ;};
2020-10-05 19:28:24 +00:00
// NewCheckboxField generates a new checkbox field with partial name `name` at location `rect`
// on specified `page` and with field specific options `opt`.
2020-10-12 14:17:59 +00:00
func NewCheckboxField (page *_g .PdfPage ,name string ,rect []float64 ,opt CheckboxFieldOptions )(*_g .PdfFieldButton ,error ){if page ==nil {return nil ,_ac .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_ac .New ("\u0072\u0065\u0071\u0075\u0069\u0072\u0065\u0064\u0020\u0061\u0074\u0074\u0072\u0069\u0062u\u0074e\u0020\u006e\u006f\u0074\u0020\u0073\u0070\u0065\u0063\u0069\u0066\u0069\u0065\u0064");};if len (rect )!=4{return nil ,_ac .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_cff ,_fbcc :=_g .NewStandard14Font (_g .ZapfDingbatsName );if _fbcc !=nil {return nil ,_fbcc ;};_afea :=_g .NewPdfField ();_dgcb :=&_g .PdfFieldButton {};_afea .SetContext (_dgcb );_dgcb .PdfField =_afea ;_dgcb .T =_bf .MakeString (name );_dgcb .SetType (_g .ButtonTypeCheckbox );_deaa :="\u004f\u0066\u0066";if opt .Checked {_deaa ="\u0059\u0065\u0073";};_dgcb .V =_bf .MakeName (_deaa );_gfdc :=_g .NewPdfAnnotationWidget ();_gfdc .Rect =_bf .MakeArrayFromFloats (rect );_gfdc .P =page .ToPdfObject ();_gfdc .F =_bf .MakeInteger (4);_gfdc .Parent =_dgcb .ToPdfObject ();_gffb :=rect [2]-rect [0];_efe :=rect [3]-rect [1];var _fagb _f .Buffer ;_fagb .WriteString ("\u0071\u000a");_fagb .WriteString ("\u0030 \u0030\u0020\u0031\u0020\u0072\u0067\n");_fagb .WriteString ("\u0042\u0054\u000a");_fagb .WriteString ("\u002f\u005a\u0061D\u0062\u0020\u0031\u0032\u0020\u0054\u0066\u000a");_fagb .WriteString ("\u0045\u0054\u000a");_fagb .WriteString ("\u0051\u000a");_cdd :=_ec .NewContentCreator ();_cdd .Add_q ();_cdd .Add_rg (0,0,1);_cdd .Add_BT ();_cdd .Add_Tf (*_bf .MakeName ("\u005a\u0061\u0044\u0062"),12);_cdd .Add_Td (0,0);_cdd .Add_ET ();_cdd .Add_Q ();_gfbg :=_g .NewXObjectForm ();_gfbg .SetContentStream (_cdd .Bytes (),_bf .NewRawEncoder ());_gfbg .BBox =_bf .MakeArrayFromFloats ([]float64 {0,0,_gffb ,_efe });_gfbg .Resources =_g .NewPdfPageResources ();_gfbg .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_cff .ToPdfObject ());_cdd =_ec .NewContentCreator ();_cdd .Add_q ();_cdd .Add_re (0,0,_gffb ,_efe );_cdd .Add_W ().Add_n ();_cdd .Add_rg (0,0,1);_cdd .Translate (0,3.0);_cdd .Add_BT ();_cdd .Add_Tf (*_bf .MakeName ("\u005a\u0061\u0044\u0062"),12);_cdd .Add_Td (0,0);_cdd .Add_Tj (*_bf .MakeString ("\u0034"));_cdd .Add_ET ();_cdd .Add_Q ();_addb :=_g .NewXObjectForm ();_addb .SetContentStream (_cdd .Bytes (),_bf .NewRawEncoder ());_addb .BBox =_bf .MakeArrayFromFloats ([]float64 {0,0,_gffb ,_efe });_addb .Resources =_g .NewPdfPageResources ();_addb .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_cff .ToPdfObject ());_eac :=_bf .MakeDict ();_eac .Set ("\u004f\u0066\u0066",_gfbg .ToPdfObject ());_eac .Set ("\u0059\u0065\u0073",_addb .ToPdfObject ());_dgcc :=_bf .MakeDict ();_dgcc .Set ("\u004e",_eac );_gfdc .AP =_dgcc ;_gfdc .AS =_bf .MakeName (_deaa );_dgcb .Annotations =append (_dgcb .Annotations ,_gfdc );return _dgcb ,nil ;};
2020-10-05 19:28:24 +00:00
2020-10-12 14:17:59 +00:00
// CheckboxFieldOptions defines optional parameters for a checkbox field a form.
type CheckboxFieldOptions struct{Checked bool ;};func (_fcg *AppearanceStyle )processDA (_ddbg *_g .PdfField ,_gfac *_ec .ContentStreamOperations ,_gfb ,_fega *_g .PdfPageResources ,_aaag *_ec .ContentCreator )(*AppearanceFont ,bool ,error ){var _dbb *AppearanceFont ;var _cdg bool ;if _fcg .Fonts !=nil {if _fcg .Fonts .Fallback !=nil {_dbb =_fcg .Fonts .Fallback ;};if _gda :=_fcg .Fonts .FieldFallbacks ;_gda !=nil {if _age ,_bfgd :=_gda [_ddbg .PartialName ()];_bfgd {_dbb =_age ;}else if _faeb ,_caeb :=_ddbg .FullName ();_caeb ==nil {if _aef ,_ffd :=_gda [_faeb ];_ffd {_dbb =_aef ;};};};_cdg =_fcg .Fonts .ForceReplace ;};var _cabe string ;var _dbee float64 ;var _bbf bool ;if _gfac !=nil {for _ ,_bef :=range *_gfac {if _bef .Operand =="\u0054\u0066"&&len (_bef .Params )==2{if _eedg ,_cge :=_bf .GetNameVal (_bef .Params [0]);_cge {_cabe =_eedg ;};if _fcae ,_ecdg :=_bf .GetNumberAsFloat (_bef .Params [1]);_ecdg ==nil {_dbee =_fcae ;};_bbf =true ;continue ;};_aaag .AddOperand (*_bef );};};var _edage *AppearanceFont ;var _beg _bf .PdfObject ;if _cdg &&_dbb !=nil {_edage =_dbb ;}else {if _gfb !=nil &&_cabe !=""{if _dcac ,_bfe :=_gfb .GetFontByName (*_bf .MakeName (_cabe ));_bfe {if _abc ,_gafc :=_g .NewPdfFontFromPdfObject (_dcac );_gafc ==nil {_beg =_dcac ;_edage =&AppearanceFont {Name :_cabe ,Font :_abc ,Size :_dbee };}else {_ed .Log .Debug ("\u0045\u0052\u0052\u004f\u0052:\u0020\u0063\u006f\u0075\u006c\u0064\u0020\u006e\u006f\u0074\u0020\u006c\u006fa\u0064\u0020\u0061\u0070\u0070\u0065\u0061\u0072\u0061\u006e\u0063\u0065\u0020\u0066\u006f\u006e\u0074\u003a\u0020\u0025\u0076",_gafc );};};};if _edage ==nil &&_dbb !=nil {_edage =_dbb ;};if _edage ==nil {_fbcb ,_fdbf :=_g .NewStandard14Font ("\u0048e\u006c\u0076\u0065\u0074\u0069\u0063a");if _fdbf !=nil {return nil ,false ,_fdbf ;};_edage =&AppearanceFont {Name :"\u0048\u0065\u006c\u0076",Font :_fbcb ,Size :_dbee };};};_cbe :=*_bf .MakeName (_edage .Name );if _beg ==nil {_beg =_edage .Font .ToPdfObject ();};if _gfb !=nil &&!_gfb .HasFontByName (_cbe ){_gfb .SetFontByName (_cbe ,_beg );};if _fega !=nil &&!_fega .HasFontByName (_cbe ){_fega .SetFontByName (_cbe ,_beg );};return _edage ,_bbf ,nil ;};
2020-10-05 19:28:24 +00:00
// FieldAppearance implements interface model.FieldAppearanceGenerator and generates appearance streams
// for fields taking into account what value is in the field. A common use case is for generating the
// appearance stream prior to flattening fields.
//
// If `OnlyIfMissing` is true, the field appearance is generated only for fields that do not have an
// appearance stream specified.
// If `RegenerateTextFields` is true, all text fields are regenerated (even if OnlyIfMissing is true).
2020-10-12 14:17:59 +00:00
type FieldAppearance struct{OnlyIfMissing bool ;RegenerateTextFields bool ;_fd *AppearanceStyle ;};type CircleAnnotationDef struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;FillEnabled bool ;FillColor *_g .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_g .PdfColorDeviceRGB ;Opacity float64 ;};func (_cfce *AppearanceStyle )applyAppearanceCharacteristics (_geed *_bf .PdfObjectDictionary ,_gcge *_bf .PdfObjectDictionary ,_cdf *_g .PdfFont )error {if !_cfce .AllowMK {return nil ;};if _gcge !=nil {if _edag ,_afdc :=_bf .GetNumberAsFloat (_gcge .Get ("\u0057"));_afdc ==nil {_cfce .BorderSize =_edag ;};};if CA ,_fdag :=_bf .GetString (_geed .Get ("\u0043\u0041"));_fdag &&_cdf !=nil {_gcf :=CA .Bytes ();if len (_gcf )!=0{_fafe :=[]rune (_cdf .Encoder ().Decode (_gcf ));if len (_fafe )==1{_cfce .CheckmarkRune =_fafe [0];};};};if BC ,_gff :=_bf .GetArray (_geed .Get ("\u0042\u0043"));_gff {_geea ,_dadc :=BC .ToFloat64Array ();if _dadc !=nil {return _dadc ;};switch len (_geea ){case 1:_cfce .BorderColor =_g .NewPdfColorDeviceGray (_geea [0]);case 3:_cfce .BorderColor =_g .NewPdfColorDeviceRGB (_geea [0],_geea [1],_geea [2]);case 4:_cfce .BorderColor =_g .NewPdfColorDeviceCMYK (_geea [0],_geea [1],_geea [2],_geea [3]);default:_ed .Log .Debug ("\u0045\u0052\u0052\u004f\u0052:\u0020\u0042\u0043\u0020\u002d\u0020\u0049\u006e\u0076\u0061\u006c\u0069\u0064 \u006e\u0075\u006d\u0062\u0065\u0072\u0020\u006f\u0066\u0020\u0063\u006f\u006c\u006f\u0072\u0020\u0063\u006f\u006d\u0070\u006f\u006e\u0065\u006e\u0074\u0073\u0020\u0028\u0025\u0064)",len (_geea ));};};if BG ,_fdc :=_bf .GetArray (_geed .Get ("\u0042\u0047"));_fdc {_dcc ,_gdd :=BG .ToFloat64Array ();if _gdd !=nil {return _gdd ;};switch len (_dcc ){case 1:_cfce .FillColor =_g .NewPdfColorDeviceGray (_dcc [0]);case 3:_cfce .FillColor =_g .NewPdfColorDeviceRGB (_dcc [0],_dcc [1],_dcc [2]);case 4:_cfce .FillColor =_g .NewPdfColorDeviceCMYK (_dcc [0],_dcc [1],_dcc [2],_dcc [3]);default:_ed .Log .Debug ("\u0045\u0052\u0052\u004f\u0052:\u0020\u0042\u0047\u0020\u002d\u0020\u0049\u006e\u0076\u0061\u006c\u0069\u0064 \u006e\u0075\u006d\u0062\u0065\u0072\u0020\u006f\u0066\u0020\u0063\u006f\u006c\u006f\u0072\u0020\u0063\u006f\u006d\u0070\u006f\u006e\u0065\u006e\u0074\u0073\u0020\u0028\u0025\u0064)",len (_dcc ));};};return nil ;};
2020-09-21 01:20:10 +00:00
2020-10-12 14:17:59 +00:00
// ComboboxFieldOptions defines optional parameters for a combobox form field.
type ComboboxFieldOptions struct{
2020-09-21 01:20:10 +00:00
2020-10-12 14:17:59 +00:00
// Choices is the list of string values that can be selected.
Choices []string ;};func _gdab (_begb RectangleAnnotationDef )(*_bf .PdfObjectDictionary ,*_g .PdfRectangle ,error ){_eace :=_g .NewXObjectForm ();_eace .Resources =_g .NewPdfPageResources ();_cdda :="";if _begb .Opacity < 1.0{_cfe :=_bf .MakeDict ();_cfe .Set ("\u0063\u0061",_bf .MakeFloat (_begb .Opacity ));_cfe .Set ("\u0043\u0041",_bf .MakeFloat (_begb .Opacity ));_bdcd :=_eace .Resources .AddExtGState ("\u0067\u0073\u0031",_cfe );if _bdcd !=nil {_ed .Log .Debug ("U\u006e\u0061\u0062\u006c\u0065\u0020t\u006f\u0020\u0061\u0064\u0064\u0020\u0065\u0078\u0074g\u0073\u0074\u0061t\u0065 \u0067\u0073\u0031");return nil ,nil ,_bdcd ;};_cdda ="\u0067\u0073\u0031";};_cbf ,_gbbgg ,_bbgb ,_cdac :=_gdcg (_begb ,_cdda );if _cdac !=nil {return nil ,nil ,_cdac ;};_cdac =_eace .SetContentStream (_cbf ,nil );if _cdac !=nil {return nil ,nil ,_cdac ;};_eace .BBox =_gbbgg .ToPdfObject ();_bffa :=_bf .MakeDict ();_bffa .Set ("\u004e",_eace .ToPdfObject ());return _bffa ,_bbgb ,nil ;};type quadding int ;
2020-09-07 00:23:12 +00:00
2020-09-14 09:32:45 +00:00
// AppearanceFontStyle defines font style characteristics for form fields,
// used in the filling/flattening process.
type AppearanceFontStyle struct{
2020-09-07 00:23:12 +00:00
2020-09-14 09:32:45 +00:00
// Fallback represents a global font fallback, used for fields which do
// not specify a font in their default appearance (DA). The fallback is
// also used if there is a font specified in the DA, but it is not
// found in the AcroForm resources (DR).
Fallback *AppearanceFont ;
2020-08-31 21:12:07 +00:00
2020-09-14 09:32:45 +00:00
// FieldFallbacks defines font fallbacks for specific fields. The map keys
// represent the names of the fields (which can be specified by their
// partial or full names). Specific field fallback fonts take precedence
// over the global font fallback.
FieldFallbacks map[string ]*AppearanceFont ;
2020-08-31 21:12:07 +00:00
2020-09-14 09:32:45 +00:00
// ForceReplace forces the replacement of fonts in the filling/flattening
// process, even if the default appearance (DA) specify a valid font.
// If no fallback font is provided, setting this field has no effect.
2020-09-28 23:18:17 +00:00
ForceReplace bool ;};
2020-10-12 14:17:59 +00:00
// CreateRectangleAnnotation creates a rectangle annotation object that can be added to page PDF annotations.
func CreateRectangleAnnotation (rectDef RectangleAnnotationDef )(*_g .PdfAnnotation ,error ){_bggf :=_g .NewPdfAnnotationSquare ();if rectDef .BorderEnabled {_ecgd ,_cdad ,_cgd :=rectDef .BorderColor .R (),rectDef .BorderColor .G (),rectDef .BorderColor .B ();_bggf .C =_bf .MakeArrayFromFloats ([]float64 {_ecgd ,_cdad ,_cgd });_ebf :=_g .NewBorderStyle ();_ebf .SetBorderWidth (rectDef .BorderWidth );_bggf .BS =_ebf .ToPdfObject ();};if rectDef .FillEnabled {_efdfb ,_edae ,_dbab :=rectDef .FillColor .R (),rectDef .FillColor .G (),rectDef .FillColor .B ();_bggf .IC =_bf .MakeArrayFromFloats ([]float64 {_efdfb ,_edae ,_dbab });}else {_bggf .IC =_bf .MakeArrayFromIntegers ([]int {});};if rectDef .Opacity < 1.0{_bggf .CA =_bf .MakeFloat (rectDef .Opacity );};_fcdg ,_bgf ,_cfga :=_gdab (rectDef );if _cfga !=nil {return nil ,_cfga ;};_bggf .AP =_fcdg ;_bggf .Rect =_bf .MakeArrayFromFloats ([]float64 {_bgf .Llx ,_bgf .Lly ,_bgf .Urx ,_bgf .Ury });return _bggf .PdfAnnotation ,nil ;};func _dcb (_cac *_ec .ContentCreator ,_eb AppearanceStyle ,_bdfa ,_efab float64 ){_cac .Add_q ().Add_re (0,0,_bdfa ,_efab ).Add_re (0,_efab /2,_bdfa ,_efab /2).Add_re (0,0,_bdfa ,_efab ).Add_re (_bdfa /2,0,_bdfa /2,_efab ).Add_w (_eb .BorderSize ).SetStrokingColor (_eb .BorderColor ).SetNonStrokingColor (_eb .FillColor ).Add_B ().Add_Q ();};
// NewTextField generates a new text field with partial name `name` at location
// specified by `rect` on given `page` and with field specific options `opt`.
func NewTextField (page *_g .PdfPage ,name string ,rect []float64 ,opt TextFieldOptions )(*_g .PdfFieldText ,error ){if page ==nil {return nil ,_ac .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_ac .New ("\u0072\u0065\u0071\u0075\u0069\u0072\u0065\u0064\u0020\u0061\u0074\u0074\u0072\u0069\u0062u\u0074e\u0020\u006e\u006f\u0074\u0020\u0073\u0070\u0065\u0063\u0069\u0066\u0069\u0065\u0064");};if len (rect )!=4{return nil ,_ac .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_aea :=_g .NewPdfField ();_eeg :=&_g .PdfFieldText {};_aea .SetContext (_eeg );_eeg .PdfField =_aea ;_eeg .T =_bf .MakeString (name );if opt .MaxLen > 0{_eeg .MaxLen =_bf .MakeInteger (int64 (opt .MaxLen ));};if len (opt .Value )> 0{_eeg .V =_bf .MakeString (opt .Value );};_cfa :=_g .NewPdfAnnotationWidget ();_cfa .Rect =_bf .MakeArrayFromFloats (rect );_cfa .P =page .ToPdfObject ();_cfa .F =_bf .MakeInteger (4);_cfa .Parent =_eeg .ToPdfObject ();_eeg .Annotations =append (_eeg .Annotations ,_cfa );return _eeg ,nil ;};
// AppearanceFont represents a font used for generating the appearance of a
// field in the filling/flattening process.
type AppearanceFont struct{
// Name represents the name of the font which will be added to the
// AcroForm resources (DR).
Name string ;
// Font represents the actual font used for the field appearance.
Font *_g .PdfFont ;
// Size represents the size of the font used for the field appearance.
// If size is 0, a default font size will be used.
// The default font size is calculated using the available annotation
// height and the AutoFontSizeFraction of the AppearanceStyle.
Size float64 ;};
// NewComboboxField generates a new combobox form field with partial name `name` at location `rect`
// on specified `page` and with field specific options `opt`.
func NewComboboxField (page *_g .PdfPage ,name string ,rect []float64 ,opt ComboboxFieldOptions )(*_g .PdfFieldChoice ,error ){if page ==nil {return nil ,_ac .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_ac .New ("\u0072\u0065\u0071\u0075\u0069\u0072\u0065\u0064\u0020\u0061\u0074\u0074\u0072\u0069\u0062u\u0074e\u0020\u006e\u006f\u0074\u0020\u0073\u0070\u0065\u0063\u0069\u0066\u0069\u0065\u0064");};if len (rect )!=4{return nil ,_ac .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_aeae :=_g .NewPdfField ();_feff :=&_g .PdfFieldChoice {};_aeae .SetContext (_feff );_feff .PdfField =_aeae ;_feff .T =_bf .MakeString (name );_feff .Opt =_bf .MakeArray ();for _ ,_cbg :=range opt .Choices {_feff .Opt .Append (_bf .MakeString (_cbg ));};_feff .SetFlag (_g .FieldFlagCombo );_gba :=_g .NewPdfAnnotationWidget ();_gba .Rect =_bf .MakeArrayFromFloats (rect );_gba .P =page .ToPdfObject ();_gba .F =_bf .MakeInteger (4);_gba .Parent =_feff .ToPdfObject ();_feff .Annotations =append (_feff .Annotations ,_gba );return _feff ,nil ;};
// WrapContentStream ensures that the entire content stream for a `page` is wrapped within q ... Q operands.
// Ensures that following operands that are added are not affected by additional operands that are added.
// Implements interface model.ContentStreamWrapper.
func (_ecge FieldAppearance )WrapContentStream (page *_g .PdfPage )error {_efda ,_caga :=page .GetAllContentStreams ();if _caga !=nil {return _caga ;};_fcaef :=_ec .NewContentStreamParser (_efda );_efb ,_caga :=_fcaef .Parse ();if _caga !=nil {return _caga ;};_efb .WrapIfNeeded ();_beb :=[]string {_efb .String ()};return page .SetContentStreams (_beb ,_aeb ());};func _ffe (_ccf *_g .PdfField )string {if _ccf ==nil {return "";};_dbf ,_eaba :=_ccf .GetContext ().(*_g .PdfFieldText );if !_eaba {return _ffe (_ccf .Parent );};if _dbf .DA !=nil {return _dbf .DA .Str ();};return _ffe (_dbf .Parent );};func _aab (_bdaf *_g .PdfAnnotationWidget ,_faca *_g .PdfFieldButton ,_ccce *_g .PdfPageResources ,_bbcg AppearanceStyle )(*_bf .PdfObjectDictionary ,error ){_bfba ,_egde :=_bf .GetArray (_bdaf .Rect );if !_egde {return nil ,_ac .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_bcc ,_cgb :=_g .NewPdfRectangle (*_bfba );if _cgb !=nil {return nil ,_cgb ;};_dgc ,_cag :=_bcc .Width (),_bcc .Height ();_eee ,_ag :=_dgc ,_cag ;_ed .Log .Debug ("\u0043\u0068\u0065\u0063kb\u006f\u0078\u002c\u0020\u0077\u0061\u0020\u0042\u0053\u003a\u0020\u0025\u0076",_bdaf .BS );_ggf ,_cgb :=_g .NewStandard14Font ("\u005a\u0061\u0070f\u0044\u0069\u006e\u0067\u0062\u0061\u0074\u0073");if _cgb !=nil {return nil ,_cgb ;};_dgg ,_bfg :=_bf .GetDict (_bdaf .MK );if _bfg {_dgcg ,_ :=_bf .GetDict (_bdaf .BS );_aaa :=_bbcg .applyAppearanceCharacteristics (_dgg ,_dgcg ,_ggf );if _aaa !=nil {return nil ,_aaa ;};};_dde :=_g .NewXObjectForm ();{_cef :=_ec .NewContentCreator ();if _bbcg .BorderSize > 0{_gbb (_cef ,_bbcg ,_dgc ,_cag );};if _bbcg .DrawAlignmentReticle {_efdd :=_bbcg ;_efdd .BorderSize =0.2;_dcb (_cef ,_efdd ,_dgc ,_cag );};_dgc ,_cag =_bbcg .applyRotation (_dgg ,_dgc ,_cag ,_cef );_fabf :=_bbcg .AutoFontSizeFraction *_cag ;_bga ,_bbd :=_ggf .GetRuneMetrics (_bbcg .CheckmarkRune );if !_bbd {return nil ,_ac .New ("\u0067l\u0079p\u0068\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064");};_ccced :=_ggf .Encoder ();_fgb :=_ccced .Encode (string (_bbcg .CheckmarkRune ));_cgc :=_bga .Wx *_fabf /1000.0;_adab :=705.0;_afcb :=_adab /1000.0*_fabf ;_gbc :=2.0;_eca :=1.0;if _cgc < _dgc {_gbc =(_dgc -_cgc )/2.0;};if _afcb < _cag {_eca =(_cag -_afcb )/2.0;};_cef .Add_q ().Add_g (0).Add_BT ().Add_Tf ("\u005a\u0061\u0044\u0062",_fabf ).Add_Td (_gbc ,_eca ).Add_Tj (*_bf .MakeStringFromBytes (_fgb )).Add_ET ().Add_Q ();_dde .Resources =_g .NewPdfPageResources ();_dde .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_ggf .ToPdfObject ());_dde .BBox =_bf .MakeArrayFromFloats ([]float64 {0,0,_eee ,_ag });_dde .SetContentStream (_cef .Bytes (),_aeb ());};_dba :=_g .NewXObjectForm ();{_daf :=_ec .NewContentCreator ();if _bbcg .BorderSize > 0{_gbb (_daf ,_bbcg ,_dgc ,_cag );};_dba .BBox =_bf .MakeArrayFromFloats ([]float64 {0,0,_eee ,_ag });_dba .SetContentStream (_daf .Bytes (),_aeb ());};_faae :=_bf .MakeDict ();_faae .Set ("\u004f\u0066\u0066",_dba .ToPdfObject ());_faae .Set ("\u0059\u0065\u0073",_dde .ToPdfObject ());_edb :=_bf .MakeDict ();_edb .Set ("\u004e",_faae );return _edb ,nil ;};func _gbb (_dcf *_ec .ContentCreator ,_fca AppearanceStyle ,_dfcd ,_bgeg float64 ){_dcf .Add_q ().Add_re (0,0,_dfcd ,_bgeg ).Add_w (_fca .BorderSize ).SetStrokingColor (_fca .BorderColor ).SetNonStrokingColor (_fca .FillColor ).Add_B ().Add_Q ();};
2020-08-27 21:45:09 +00:00
// SignatureFieldOpts represents a set of options used to configure
// an appearance widget dictionary.
type SignatureFieldOpts struct{
// Rect represents the area the signature annotation is displayed on.
Rect []float64 ;
// AutoSize specifies if the content of the appearance should be
// scaled to fit in the annotation rectangle.
AutoSize bool ;
// Font specifies the font of the text content.
2020-10-12 14:17:59 +00:00
Font *_g .PdfFont ;
2020-08-27 21:45:09 +00:00
// FontSize specifies the size of the text content.
FontSize float64 ;
// LineHeight specifies the height of a line of text in the appearance annotation.
LineHeight float64 ;
// TextColor represents the color of the text content displayed.
2020-10-12 14:17:59 +00:00
TextColor _g .PdfColor ;
2020-08-27 21:45:09 +00:00
// FillColor represents the background color of the appearance annotation area.
2020-10-12 14:17:59 +00:00
FillColor _g .PdfColor ;
2020-08-27 21:45:09 +00:00
// BorderSize represents border size of the appearance annotation area.
BorderSize float64 ;
// BorderColor represents the border color of the appearance annotation area.
2020-10-12 14:17:59 +00:00
BorderColor _g .PdfColor ;};
2020-08-27 21:45:09 +00:00
2020-10-12 14:17:59 +00:00
// LineAnnotationDef defines a line between point 1 (X1,Y1) and point 2 (X2,Y2). The line ending styles can be none
// (regular line), or arrows at either end. The line also has a specified width, color and opacity.
type LineAnnotationDef struct{X1 float64 ;Y1 float64 ;X2 float64 ;Y2 float64 ;LineColor *_g .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineEndingStyle1 _gd .LineEndingStyle ;LineEndingStyle2 _gd .LineEndingStyle ;};func _dfdae (_afeb LineAnnotationDef ,_bdc string )([]byte ,*_g .PdfRectangle ,*_g .PdfRectangle ,error ){_efg :=_gd .Line {X1 :0,Y1 :0,X2 :_afeb .X2 -_afeb .X1 ,Y2 :_afeb .Y2 -_afeb .Y1 ,LineColor :_afeb .LineColor ,Opacity :_afeb .Opacity ,LineWidth :_afeb .LineWidth ,LineEndingStyle1 :_afeb .LineEndingStyle1 ,LineEndingStyle2 :_afeb .LineEndingStyle2 };_gcec ,_fcb ,_agb :=_efg .Draw (_bdc );if _agb !=nil {return nil ,nil ,nil ,_agb ;};_bafc :=&_g .PdfRectangle {};_bafc .Llx =_afeb .X1 +_fcb .Llx ;_bafc .Lly =_afeb .Y1 +_fcb .Lly ;_bafc .Urx =_afeb .X1 +_fcb .Urx ;_bafc .Ury =_afeb .Y1 +_fcb .Ury ;return _gcec ,_fcb ,_bafc ,nil ;};func _edd (_feg *_g .PdfField ,_dbc ,_aff float64 ,_add string ,_effe AppearanceStyle ,_dbge *_ec .ContentStreamOperations ,_cfg *_g .PdfPageResources ,_ecfe *_bf .PdfObjectDictionary )(*_g .XObjectForm ,error ){_dafg :=_g .NewPdfPageResources ();_gdf ,_gce :=_dbc ,_aff ;_cggc :=_ec .NewContentCreator ();if _effe .BorderSize > 0{_gbb (_cggc ,_effe ,_dbc ,_aff );};if _effe .DrawAlignmentReticle {_cda :=_effe ;_cda .BorderSize =0.2;_dcb (_cggc ,_cda ,_dbc ,_aff );};_cggc .Add_BMC ("\u0054\u0078");_cggc .Add_q ();_cggc .Add_BT ();_dbc ,_aff =_effe .applyRotation (_ecfe ,_dbc ,_aff ,_cggc );_abg ,_gaf ,_gdfg :=_effe .processDA (_feg ,_dbge ,_cfg ,_dafg ,_cggc );if _gdfg !=nil {return nil ,_gdfg ;};_bbed :=_abg .Font ;_cage :=_abg .Size ;_ecg :=_bf .MakeName (_abg .Name );_bce :=_cage ==0;if _bce &&_gaf {_cage =_aff *_effe .AutoFontSizeFraction ;};_cba :=_bbed .Encoder ();if _cba ==nil {_ed .Log .Debug ("\u0057\u0041RN\u003a\u0020\u0066\u006f\u006e\u0074\u0020\u0065\u006e\u0063\u006f\u0064\u0065\u0072\u0020\u0069\u0073\u0020\u006e\u0069l\u002e\u0020\u0041\u0073s\u0075\u006d\u0069\u006eg \u0069\u0064e\u006et\u0069\u0074\u0079\u0020\u0065\u006ec\u006f\u0064\u0065r\u002e\u0020O\u0075\u0074\u0070\u0075\u0074\u0020\u006d\u0061\u0079\u0020\u0062\u0065\u0020\u0069n\u0063\u006f\u0072\u0072\u0065\u0063\u0074\u002e");_cba =_c .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};if len (_add )==0{return nil ,nil ;};_ceed :=2.0;_bdb :=0.0;if _cba !=nil {for _ ,_agd :=range _add {_caa ,_ggge :=_bbed .GetRuneMetrics (_agd );if !_ggge {_ed .Log .Debug ("\u0046\u006f\u006e\u0074\u0020\u0064o\u0065\u0073\u0020\u006e\u006f\u0074\u0020\u0068\u0061\u0076\u0065\u0020\u0072\u0075\u006e\u0065\u0020\u006d\u0065\u0074r\u0069\u0063\u0073\u0020\u0066\u006f\u0072\u0020\u0025\u0076\u0020\u002d\u0020\u0073k\u0069p\u0070\u0069\u006e\u0067",_agd );continue ;};_bdb +=_caa .Wx ;};_add =string (_cba .Encode (_add ));};if _cage ==0||_bce &&_bdb > 0&&_ceed +_bdb *_cage /1000.0> _dbc {_cage =0.95*1000.0*(_dbc -_ceed )/_bdb ;};_ede :=1.0*_cage ;_acg :=2.0;{_eae :=_ede ;if _bce &&_acg +_eae > _aff {_cage =0.95*(_aff -_acg );_ede =1.0*_cage ;_eae =_ede ;};if _aff > _eae {_acg =(_aff -_eae )/2.0;_acg +=1.50;};};_cggc .Add_Tf (*_ecg ,_cage );_cggc .Add_Td (_ceed ,_acg );_cggc .Add_Tj (*_bf .MakeString (_add ));_cggc .Add_ET ();_cggc .Add_Q ();_cggc .Add_EMC ();_bacc :=_g .NewXObjectForm ();_bacc .Resources =_dafg ;_bacc .BBox =_bf .MakeArrayFromFloats ([]float64 {0,0,_gdf ,_gce });_bacc .SetContentStream (_cggc .Bytes (),_aeb ());return _bacc ,nil ;};
2020-08-31 21:12:07 +00:00
2020-10-12 14:17:59 +00:00
// GenerateAppearanceDict generates an appearance dictionary for widget annotation `wa` for the `field` in `form`.
// Implements interface model.FieldAppearanceGenerator.
func (_ce FieldAppearance )GenerateAppearanceDict (form *_g .PdfAcroForm ,field *_g .PdfField ,wa *_g .PdfAnnotationWidget )(*_bf .PdfObjectDictionary ,error ){_ed .Log .Trace ("\u0047\u0065n\u0065\u0072\u0061\u0074e\u0041\u0070p\u0065\u0061\u0072\u0061\u006e\u0063\u0065\u0044i\u0063\u0074\u0020\u0066\u006f\u0072\u0020\u0025\u0076\u0020\u0020\u0056:\u0020\u0025\u002b\u0076",field .PartialName (),field .V );_ ,_df :=field .GetContext ().(*_g .PdfFieldText );_cc ,_eg :=_bf .GetDict (wa .AP );if _eg &&_ce .OnlyIfMissing &&(!_df ||!_ce .RegenerateTextFields ){_ed .Log .Trace ("\u0041\u006c\u0072\u0065a\u0064\u0079\u0020\u0070\u006f\u0070\u0075\u006c\u0061\u0074e\u0064 \u002d\u0020\u0069\u0067\u006e\u006f\u0072i\u006e\u0067");return _cc ,nil ;};if form .DR ==nil {form .DR =_g .NewPdfPageResources ();};switch _ad :=field .GetContext ().(type ){case *_g .PdfFieldText :_gg :=_ad ;switch {case _gg .Flags ().Has (_g .FieldFlagPassword ):return nil ,nil ;case _gg .Flags ().Has (_g .FieldFlagFileSelect ):return nil ,nil ;case _gg .Flags ().Has (_g .FieldFlagComb ):if _gg .MaxLen !=nil {_ff ,_db :=_ccc (wa ,_gg ,form .DR ,_ce .Style ());if _db !=nil {return nil ,_db ;};return _ff ,nil ;};};_bca ,_acc :=_bge (wa ,_gg ,form .DR ,_ce .Style ());if _acc !=nil {return nil ,_acc ;};return _bca ,nil ;case *_g .PdfFieldButton :_gcb :=_ad ;if _gcb .IsCheckbox (){_ddg ,_eed :=_aab (wa ,_gcb ,form .DR ,_ce .Style ());if _eed !=nil {return nil ,_eed ;};return _ddg ,nil ;};_ed .Log .Debug ("\u0054\u004f\u0044\u004f\u003a\u0020\u0055\u004e\u0048\u0041\u004e\u0044\u004c\u0045\u0044 \u0062u\u0074\u0074\u006f\u006e\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u002b\u0076",_gcb .GetType ());case *_g .PdfFieldChoice :_cga :=_ad ;switch {case _cga .Flags ().Has (_g .FieldFlagCombo ):_ecdc ,_gga :=_dgbc (form ,wa ,_cga ,_ce .Style ());if _gga !=nil {return nil ,_gga ;};return _ecdc ,nil ;default:_ed .Log .Debug ("\u0054\u004f\u0044\u004f\u003a\u0020\u0055N\u0048\u0041\u004eD\u004c\u0045\u0044\u0020c\u0068\u006f\u0069\u0063\u0065\u0020\u0066\u0069\u0065\u006c\u0064\u0020\u0077\u0069\u0074\u0068\u0020\u0066\u006c\u0061\u0067\u0073\u003a\u0020\u0025\u0073",_cga .Flags ().String ());};default:_ed .Log .Debug ("\u0054\u004f\u0044\u004f\u003a\u0020\u0055\u004e\u0048\u0041N\u0044\u004c\u0045\u0044\u0020\u0066\u0069e\u006c\u0064\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u0054",_ad );};return nil ,nil ;};
2020-08-31 21:12:07 +00:00
2020-10-05 19:28:24 +00:00
// NewSignatureLine returns a new signature line displayed as a part of the
// signature field appearance.
2020-10-12 14:17:59 +00:00
func NewSignatureLine (desc ,text string )*SignatureLine {return &SignatureLine {Desc :desc ,Text :text };};const (_cd quadding =0;_fg quadding =1;_daa quadding =2;);