unipdf/annotator/annotator.go

208 lines
56 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-19 10:58:10 +00:00
package annotator ;import (_ge "bytes";_e "errors";_c "github.com/unidoc/unipdf/v3/common";_b "github.com/unidoc/unipdf/v3/contentstream";_fc "github.com/unidoc/unipdf/v3/contentstream/draw";_d "github.com/unidoc/unipdf/v3/core";_f "github.com/unidoc/unipdf/v3/internal/textencoding";_ef "github.com/unidoc/unipdf/v3/model";_fe "image";_ab "math";_a "strings";_ca "unicode";);const (_gg quadding =0;_bga quadding =1;_edg quadding =2;);func _bg (_ag CircleAnnotationDef )(*_d .PdfObjectDictionary ,*_ef .PdfRectangle ,error ){_bc :=_ef .NewXObjectForm ();_bc .Resources =_ef .NewPdfPageResources ();_fcd :="";if _ag .Opacity < 1.0{_df :=_d .MakeDict ();_df .Set ("\u0063\u0061",_d .MakeFloat (_ag .Opacity ));_df .Set ("\u0043\u0041",_d .MakeFloat (_ag .Opacity ));_ff :=_bc .Resources .AddExtGState ("\u0067\u0073\u0031",_df );if _ff !=nil {_c .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 ,_ff ;};_fcd ="\u0067\u0073\u0031";};_fa ,_efg ,_ac ,_ec :=_efc (_ag ,_fcd );if _ec !=nil {return nil ,nil ,_ec ;};_ec =_bc .SetContentStream (_fa ,nil );if _ec !=nil {return nil ,nil ,_ec ;};_bc .BBox =_efg .ToPdfObject ();_fag :=_d .MakeDict ();_fag .Set ("\u004e",_bc .ToPdfObject ());return _fag ,_ac ,nil ;};
2020-10-05 19:28:24 +00:00
2020-10-19 10:58:10 +00:00
// SignatureFieldOpts represents a set of options used to configure
// an appearance widget dictionary.
type SignatureFieldOpts struct{
2020-10-05 19:28:24 +00:00
2020-10-19 10:58:10 +00:00
// Rect represents the area the signature annotation is displayed on.
Rect []float64 ;
2020-10-05 19:28:24 +00:00
2020-10-19 10:58:10 +00:00
// AutoSize specifies if the content of the appearance should be
// scaled to fit in the annotation rectangle.
AutoSize bool ;
2020-10-05 19:28:24 +00:00
2020-10-19 10:58:10 +00:00
// Font specifies the font of the text content.
Font *_ef .PdfFont ;
2020-10-05 19:28:24 +00:00
2020-10-19 10:58:10 +00:00
// FontSize specifies the size of the text content.
FontSize float64 ;
2020-09-21 01:20:10 +00:00
2020-10-19 10:58:10 +00:00
// LineHeight specifies the height of a line of text in the appearance annotation.
LineHeight float64 ;
2020-09-21 01:20:10 +00:00
2020-10-19 10:58:10 +00:00
// TextColor represents the color of the text content displayed.
TextColor _ef .PdfColor ;
2020-10-12 14:17:59 +00:00
2020-10-19 10:58:10 +00:00
// FillColor represents the background color of the appearance annotation area.
FillColor _ef .PdfColor ;
2020-09-21 01:20:10 +00:00
2020-10-19 10:58:10 +00:00
// BorderSize represents border size of the appearance annotation area.
BorderSize float64 ;
2020-10-05 19:28:24 +00:00
2020-10-19 10:58:10 +00:00
// BorderColor represents the border color of the appearance annotation area.
BorderColor _ef .PdfColor ;
2020-10-05 19:28:24 +00:00
2020-10-19 10:58:10 +00:00
// WatermarkImage specifies the image used as a watermark that will be rendered
// behind the signature.
WatermarkImage _fe .Image ;
2020-10-12 14:17:59 +00:00
2020-10-19 10:58:10 +00:00
// Image represents the image used for the signature appearance.
Image _fe .Image ;
2020-10-12 14:17:59 +00:00
2020-10-19 10:58:10 +00:00
// Encoder specifies the image encoder used for image signature. Defaults to flate encoder.
Encoder _d .StreamEncoder ;
2020-10-05 19:28:24 +00:00
2020-10-19 10:58:10 +00:00
// ImagePosition specifies the image location relative to the text signature.
ImagePosition SignatureImagePosition ;};func _caa (_agbe []*SignatureLine ,_eccda *SignatureFieldOpts )(*_d .PdfObjectDictionary ,error ){if _eccda ==nil {_eccda =NewSignatureFieldOpts ();};var _dbf error ;var _fedb *_d .PdfObjectName ;_dee :=_eccda .Font ;if _dee !=nil {_cfgf ,_ :=_dee .GetFontDescriptor ();if _cfgf !=nil {if _efe ,_bebf :=_cfgf .FontName .(*_d .PdfObjectName );_bebf {_fedb =_efe ;};};if _fedb ==nil {_fedb =_d .MakeName ("\u0046\u006f\u006et\u0031");};}else {if _dee ,_dbf =_ef .NewStandard14Font ("\u0048e\u006c\u0076\u0065\u0074\u0069\u0063a");_dbf !=nil {return nil ,_dbf ;};_fedb =_d .MakeName ("\u0048\u0065\u006c\u0076");};_ccff :=_eccda .FontSize ;if _ccff <=0{_ccff =10;};if _eccda .LineHeight <=0{_eccda .LineHeight =1;};_cce :=_eccda .LineHeight *_ccff ;_gfef ,_eeec :=_dee .GetRuneMetrics (' ');if !_eeec {return nil ,_e .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");};_gdd :=_gfef .Wx ;var _fffc float64 ;var _fbe []string ;for _ ,_dgea :=range _agbe {if _dgea .Text ==""{continue ;};_bdcd :=_dgea .Text ;if _dgea .Desc !=""{_bdcd =_dgea .Desc +"\u003a\u0020"+_bdcd ;};_fbe =append (_fbe ,_bdcd );var _fffb float64 ;for _ ,_ecaa :=range _bdcd {_aab ,_ebde :=_dee .GetRuneMetrics (_ecaa );if !_ebde {continue ;};_fffb +=_aab .Wx ;};if _fffb > _fffc {_fffc =_fffb ;};};_fffc =_fffc *_ccff /1000.0;_fbae :=float64 (len (_fbe ))*_cce ;_debf :=_eccda .Image !=nil ;_fdca :=_eccda .Rect ;if _fdca ==nil {_fdca =[]float64 {0,0,_fffc ,_fbae };if _debf {_fdca [2]=_fffc *2;_fdca [3]=_fbae *2;};_eccda .Rect =_fdca ;};_acab :=_fdca [2]-_fdca [0];_begg :=_fdca [3]-_fdca [1];_daba ,_ddf :=_fdca ,_fdca ;var _bfdb ,_cacd float64 ;if _debf &&len (_fbe )> 0{if _eccda .ImagePosition <=SignatureImageRight {_cgef :=[]float64 {_fdca [0],_fdca [1],_fdca [0]+(_acab /2),_fdca [3]};_cacde :=[]float64 {_fdca [0]+(_acab /2),_fdca [1],_fdca [2],_fdca [3]};if _eccda .ImagePosition ==SignatureImageLeft {_daba ,_ddf =_cgef ,_cacde ;}else {_daba ,_ddf =_cacde ,_cgef ;};}else {_cebb :=[]float64 {_fdca [0],_fdca [1],_fdca [2],_fdca [1]+(_begg /2)};_cde :=[]float64 {_fdca [0],_fdca [1]+(_begg /2),_fdca [2],_fdca [3]};if _eccda .ImagePosition ==SignatureImageTop {_daba ,_ddf =_cde ,_cebb ;}else {_daba ,_ddf =_cebb ,_cde ;};};};_bfdb =_ddf [2]-_ddf [0];_cacd =_ddf [3]-_ddf [1];var _ccaf float64 ;if _eccda .AutoSize {if _fffc > _bfdb ||_fbae > _cacd {_bceb :=_ab .Min (_bfdb /_fffc ,_cacd /_fbae );_ccff *=_bceb ;};_cce =_eccda .LineHeight *_ccff ;_ccaf +=(_cacd -float64 (len (_fbe ))*_cce )/2;};_fagd :=_b .NewContentCreator ();_gdc :=_ef .NewPdfPageResources ();_gdc .SetFontByName (*_fedb ,_dee .ToPdfObject ());if _eccda .BorderSize <=0{_eccda .BorderSize =0;_eccda .BorderColor =_ef .NewPdfColorDeviceGray (1);};if _eccda .BorderColor ==nil {_eccda .BorderColor =_ef .NewPdfColorDeviceGray (1);};if _eccda .FillColor ==nil {_eccda .FillColor =_ef .NewPdfColorDeviceGray (1);};_fagd .Add_q ().SetNonStrokingColor (_eccda .FillColor ).SetStrokingColor (_eccda .BorderColor ).Add_w (_eccda .BorderSize ).Add_re (_fdca [0],_fdca [1],_acab ,_begg ).Add_B ().Add_Q ();if _eccda .WatermarkImage !=nil {_ffc :=[]float64 {_fdca [0],_fdca [1],_fdca [2],_fdca [3]};_abg ,_ggec ,_fdef :=_ege (_eccda .WatermarkImage ,"\u0049\u006d\u0061\u0067\u0065\u0057\u0061\u0074\u0065r\u006d\u0061\u0072\u006b",_eccda ,_ffc ,_fagd );if _fdef !=nil {return nil ,_fdef ;};_gdc .SetXObjectImageByName (*_abg ,_ggec );};_fagd .Add_q ();_fagd .Translate (_ddf [0],_ddf [3]-_cce -_ccaf );_fagd .Add_BT ();_dae :=_dee .Encoder ();for _ ,_abbe :=range _fbe {var _fef []byte ;for _ ,_dca :=range _abbe {if _ca .IsSpace (_dca ){if len (_fef )> 0{_fagd .SetNonStrokingColor (_eccda .TextColor ).Add_Tf (*_fedb ,_ccff ).Add_TL (_cce ).Add_TJ ([]_d .PdfObject {_d .MakeStringFromBytes (_fef )}...);_fef =nil ;};_fagd .Add_Tf (*_fedb ,_ccff ).Add_TL (_cce ).Add_TJ ([]_d .PdfObject {_d .MakeFloat (-_gdd )}...);}else {_fef =append (_fef ,_dae .Encode (string (_dca ))...);};};if len (_fef
2020-10-05 19:28:24 +00:00
2020-10-19 10:58:10 +00:00
// 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 *_ef .PdfPage ,name string ,rect []float64 ,opt TextFieldOptions )(*_ef .PdfFieldText ,error ){if page ==nil {return nil ,_e .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_e .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 ,_e .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_baec :=_ef .NewPdfField ();_caad :=&_ef .PdfFieldText {};_baec .SetContext (_caad );_caad .PdfField =_baec ;_caad .T =_d .MakeString (name );if opt .MaxLen > 0{_caad .MaxLen =_d .MakeInteger (int64 (opt .MaxLen ));};if len (opt .Value )> 0{_caad .V =_d .MakeString (opt .Value );};_gcbbg :=_ef .NewPdfAnnotationWidget ();_gcbbg .Rect =_d .MakeArrayFromFloats (rect );_gcbbg .P =page .ToPdfObject ();_gcbbg .F =_d .MakeInteger (4);_gcbbg .Parent =_caad .ToPdfObject ();_caad .Annotations =append (_caad .Annotations ,_gcbbg );return _caad ,nil ;};func (_bgeef *AppearanceStyle )applyRotation (_fff *_d .PdfObjectDictionary ,_dfca ,_bcbb float64 ,_bebb *_b .ContentCreator )(float64 ,float64 ){if !_bgeef .AllowMK {return _dfca ,_bcbb ;};if _fff ==nil {return _dfca ,_bcbb ;};_gcbb ,_ :=_d .GetNumberAsFloat (_fff .Get ("\u0052"));if _gcbb ==0{return _dfca ,_bcbb ;};_gfdc :=-_gcbb ;_cgc :=_fc .Path {Points :[]_fc .Point {_fc .NewPoint (0,0).Rotate (_gfdc ),_fc .NewPoint (_dfca ,0).Rotate (_gfdc ),_fc .NewPoint (0,_bcbb ).Rotate (_gfdc ),_fc .NewPoint (_dfca ,_bcbb ).Rotate (_gfdc )}}.GetBoundingBox ();_bebb .RotateDeg (_gcbb );_bebb .Translate (_cgc .X ,_cgc .Y );return _cgc .Width ,_cgc .Height ;};
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-19 10:58:10 +00:00
type FieldAppearance struct{OnlyIfMissing bool ;RegenerateTextFields bool ;_ee *AppearanceStyle ;};
2020-09-21 01:20:10 +00:00
2020-10-19 10:58:10 +00:00
// CreateCircleAnnotation creates a circle/ellipse annotation object with appearance stream that can be added to
// page PDF annotations.
func CreateCircleAnnotation (circDef CircleAnnotationDef )(*_ef .PdfAnnotation ,error ){_eg :=_ef .NewPdfAnnotationCircle ();if circDef .BorderEnabled {_cc ,_gd ,_gf :=circDef .BorderColor .R (),circDef .BorderColor .G (),circDef .BorderColor .B ();_eg .C =_d .MakeArrayFromFloats ([]float64 {_cc ,_gd ,_gf });_cd :=_ef .NewBorderStyle ();_cd .SetBorderWidth (circDef .BorderWidth );_eg .BS =_cd .ToPdfObject ();};if circDef .FillEnabled {_da ,_bf ,_bfg :=circDef .FillColor .R (),circDef .FillColor .G (),circDef .FillColor .B ();_eg .IC =_d .MakeArrayFromFloats ([]float64 {_da ,_bf ,_bfg });}else {_eg .IC =_d .MakeArrayFromIntegers ([]int {});};if circDef .Opacity < 1.0{_eg .CA =_d .MakeFloat (circDef .Opacity );};_be ,_ed ,_beg :=_bg (circDef );if _beg !=nil {return nil ,_beg ;};_eg .AP =_be ;_eg .Rect =_d .MakeArrayFromFloats ([]float64 {_ed .Llx ,_ed .Lly ,_ed .Urx ,_ed .Ury });return _eg .PdfAnnotation ,nil ;};func _ecf (_cdda *_ef .PdfAnnotationWidget ,_bed *_ef .PdfFieldButton ,_fcab *_ef .PdfPageResources ,_cgg AppearanceStyle )(*_d .PdfObjectDictionary ,error ){_bcea ,_fcda :=_d .GetArray (_cdda .Rect );if !_fcda {return nil ,_e .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_fede ,_gcd :=_ef .NewPdfRectangle (*_bcea );if _gcd !=nil {return nil ,_gcd ;};_ggcg ,_gad :=_fede .Width (),_fede .Height ();_fcag ,_edgc :=_ggcg ,_gad ;_c .Log .Debug ("\u0043\u0068\u0065\u0063kb\u006f\u0078\u002c\u0020\u0077\u0061\u0020\u0042\u0053\u003a\u0020\u0025\u0076",_cdda .BS );_ecd ,_gcd :=_ef .NewStandard14Font ("\u005a\u0061\u0070f\u0044\u0069\u006e\u0067\u0062\u0061\u0074\u0073");if _gcd !=nil {return nil ,_gcd ;};_fee ,_bcae :=_d .GetDict (_cdda .MK );if _bcae {_cgfef ,_ :=_d .GetDict (_cdda .BS );_dgac :=_cgg .applyAppearanceCharacteristics (_fee ,_cgfef ,_ecd );if _dgac !=nil {return nil ,_dgac ;};};_ggfa :=_ef .NewXObjectForm ();{_ead :=_b .NewContentCreator ();if _cgg .BorderSize > 0{_cbd (_ead ,_cgg ,_ggcg ,_gad );};if _cgg .DrawAlignmentReticle {_ddc :=_cgg ;_ddc .BorderSize =0.2;_ffbb (_ead ,_ddc ,_ggcg ,_gad );};_ggcg ,_gad =_cgg .applyRotation (_fee ,_ggcg ,_gad ,_ead );_dafb :=_cgg .AutoFontSizeFraction *_gad ;_abfd ,_dcc :=_ecd .GetRuneMetrics (_cgg .CheckmarkRune );if !_dcc {return nil ,_e .New ("\u0067l\u0079p\u0068\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064");};_gga :=_ecd .Encoder ();_ebg :=_gga .Encode (string (_cgg .CheckmarkRune ));_ffb :=_abfd .Wx *_dafb /1000.0;_dgb :=705.0;_cag :=_dgb /1000.0*_dafb ;_dff :=2.0;_ebf :=1.0;if _ffb < _ggcg {_dff =(_ggcg -_ffb )/2.0;};if _cag < _gad {_ebf =(_gad -_cag )/2.0;};_ead .Add_q ().Add_g (0).Add_BT ().Add_Tf ("\u005a\u0061\u0044\u0062",_dafb ).Add_Td (_dff ,_ebf ).Add_Tj (*_d .MakeStringFromBytes (_ebg )).Add_ET ().Add_Q ();_ggfa .Resources =_ef .NewPdfPageResources ();_ggfa .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_ecd .ToPdfObject ());_ggfa .BBox =_d .MakeArrayFromFloats ([]float64 {0,0,_fcag ,_edgc });_ggfa .SetContentStream (_ead .Bytes (),_ggdd ());};_geg :=_ef .NewXObjectForm ();{_deg :=_b .NewContentCreator ();if _cgg .BorderSize > 0{_cbd (_deg ,_cgg ,_ggcg ,_gad );};_geg .BBox =_d .MakeArrayFromFloats ([]float64 {0,0,_fcag ,_edgc });_geg .SetContentStream (_deg .Bytes (),_ggdd ());};_dfc :=_d .MakeDict ();_dfc .Set ("\u004f\u0066\u0066",_geg .ToPdfObject ());_dfc .Set ("\u0059\u0065\u0073",_ggfa .ToPdfObject ());_ebd :=_d .MakeDict ();_ebd .Set ("\u004e",_dfc );return _ebd ,nil ;};func _aad (_bdc *_ef .PdfAcroForm ,_gdg *_ef .PdfAnnotationWidget ,_fgef *_ef .PdfFieldChoice ,_aag AppearanceStyle )(*_d .PdfObjectDictionary ,error ){_fcge ,_dfcg :=_d .GetArray (_gdg .Rect );if !_dfcg {return nil ,_e .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_gfe ,_dcd :=_ef .NewPdfRectangle (*_fcge );if _dcd !=nil {return nil ,_dcd ;};_ddce ,_fceb :=_gfe .Width (),_gfe .Height ();_c .Log .Debug ("\u0043\u0068\u006f\u0069\u0063\u0065\u002c\u0020\u0077\u0061\u0020\u0042S\u003a\u0020\u0025\u0076",_gdg .BS );_gcf ,_dcd :=_b .NewContentStreamParser (_cbc (_fgef .PdfField )).Parse ();if _dcd
2020-09-21 01:20:10 +00:00
2020-10-19 10:58:10 +00:00
// TextFieldOptions defines optional parameter for a text field in a form.
type TextFieldOptions struct{MaxLen int ;Value string ;};
// NewSignatureLine returns a new signature line displayed as a part of the
// signature field appearance.
func NewSignatureLine (desc ,text string )*SignatureLine {return &SignatureLine {Desc :desc ,Text :text };};
// GenerateAppearanceDict generates an appearance dictionary for widget annotation `wa` for the `field` in `form`.
// Implements interface model.FieldAppearanceGenerator.
func (_edd FieldAppearance )GenerateAppearanceDict (form *_ef .PdfAcroForm ,field *_ef .PdfField ,wa *_ef .PdfAnnotationWidget )(*_d .PdfObjectDictionary ,error ){_c .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 );_ ,_ggb :=field .GetContext ().(*_ef .PdfFieldText );_db ,_beb :=_d .GetDict (wa .AP );if _beb &&_edd .OnlyIfMissing &&(!_ggb ||!_edd .RegenerateTextFields ){_c .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 _db ,nil ;};if form .DR ==nil {form .DR =_ef .NewPdfPageResources ();};switch _gae :=field .GetContext ().(type ){case *_ef .PdfFieldText :_fg :=_gae ;switch {case _fg .Flags ().Has (_ef .FieldFlagPassword ):return nil ,nil ;case _fg .Flags ().Has (_ef .FieldFlagFileSelect ):return nil ,nil ;case _fg .Flags ().Has (_ef .FieldFlagComb ):if _fg .MaxLen !=nil {_dbc ,_bcb :=_abfe (wa ,_fg ,form .DR ,_edd .Style ());if _bcb !=nil {return nil ,_bcb ;};return _dbc ,nil ;};};_bab ,_ad :=_ea (wa ,_fg ,form .DR ,_edd .Style ());if _ad !=nil {return nil ,_ad ;};return _bab ,nil ;case *_ef .PdfFieldButton :_fed :=_gae ;if _fed .IsCheckbox (){_bd ,_gge :=_ecf (wa ,_fed ,form .DR ,_edd .Style ());if _gge !=nil {return nil ,_gge ;};return _bd ,nil ;};_c .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",_fed .GetType ());case *_ef .PdfFieldChoice :_de :=_gae ;switch {case _de .Flags ().Has (_ef .FieldFlagCombo ):_cg ,_fae :=_aad (form ,wa ,_de ,_edd .Style ());if _fae !=nil {return nil ,_fae ;};return _cg ,nil ;default:_c .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",_de .Flags ().String ());};default:_c .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",_gae );};return nil ,nil ;};func _fcga (_ace RectangleAnnotationDef ,_adfc string )([]byte ,*_ef .PdfRectangle ,*_ef .PdfRectangle ,error ){_acac :=_fc .Rectangle {X :0,Y :0,Width :_ace .Width ,Height :_ace .Height ,FillEnabled :_ace .FillEnabled ,FillColor :_ace .FillColor ,BorderEnabled :_ace .BorderEnabled ,BorderWidth :2*_ace .BorderWidth ,BorderColor :_ace .BorderColor ,Opacity :_ace .Opacity };_edc ,_ebc ,_cfef :=_acac .Draw (_adfc );if _cfef !=nil {return nil ,nil ,nil ,_cfef ;};_dgaa :=&_ef .PdfRectangle {};_dgaa .Llx =_ace .X +_ebc .Llx ;_dgaa .Lly =_ace .Y +_ebc .Lly ;_dgaa .Urx =_ace .X +_ebc .Urx ;_dgaa .Ury =_ace .Y +_ebc .Ury ;return _edc ,_ebc ,_dgaa ,nil ;};
// 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 *_ef .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineEndingStyle1 _fc .LineEndingStyle ;LineEndingStyle2 _fc .LineEndingStyle ;};type CircleAnnotationDef struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;FillEnabled bool ;FillColor *_ef .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_ef .PdfColorDeviceRGB ;Opacity float64 ;};
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-19 10:58:10 +00:00
// 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 *_ef .PdfPage ,name string ,rect []float64 ,opt ComboboxFieldOptions )(*_ef .PdfFieldChoice ,error ){if page ==nil {return nil ,_e .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_e .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 ,_e .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_aef :=_ef .NewPdfField ();_bega :=&_ef .PdfFieldChoice {};_aef .SetContext (_bega );_bega .PdfField =_aef ;_bega .T =_d .MakeString (name );_bega .Opt =_d .MakeArray ();for _ ,_edgd :=range opt .Choices {_bega .Opt .Append (_d .MakeString (_edgd ));};_bega .SetFlag (_ef .FieldFlagCombo );_ddb :=_ef .NewPdfAnnotationWidget ();_ddb .Rect =_d .MakeArrayFromFloats (rect );_ddb .P =page .ToPdfObject ();_ddb .F =_d .MakeInteger (4);_ddb .Parent =_bega .ToPdfObject ();_bega .Annotations =append (_bega .Annotations ,_ddb );return _bega ,nil ;};
2020-10-12 14:17:59 +00:00
2020-10-19 10:58:10 +00:00
// CreateLineAnnotation creates a line annotation object that can be added to page PDF annotations.
func CreateLineAnnotation (lineDef LineAnnotationDef )(*_ef .PdfAnnotation ,error ){_ddg :=_ef .NewPdfAnnotationLine ();_ddg .L =_d .MakeArrayFromFloats ([]float64 {lineDef .X1 ,lineDef .Y1 ,lineDef .X2 ,lineDef .Y2 });_bgc :=_d .MakeName ("\u004e\u006f\u006e\u0065");if lineDef .LineEndingStyle1 ==_fc .LineEndingStyleArrow {_bgc =_d .MakeName ("C\u006c\u006f\u0073\u0065\u0064\u0041\u0072\u0072\u006f\u0077");};_egfc :=_d .MakeName ("\u004e\u006f\u006e\u0065");if lineDef .LineEndingStyle2 ==_fc .LineEndingStyleArrow {_egfc =_d .MakeName ("C\u006c\u006f\u0073\u0065\u0064\u0041\u0072\u0072\u006f\u0077");};_ddg .LE =_d .MakeArray (_bgc ,_egfc );if lineDef .Opacity < 1.0{_ddg .CA =_d .MakeFloat (lineDef .Opacity );};_dbed ,_afba ,_gda :=lineDef .LineColor .R (),lineDef .LineColor .G (),lineDef .LineColor .B ();_ddg .IC =_d .MakeArrayFromFloats ([]float64 {_dbed ,_afba ,_gda });_ddg .C =_d .MakeArrayFromFloats ([]float64 {_dbed ,_afba ,_gda });_dcad :=_ef .NewBorderStyle ();_dcad .SetBorderWidth (lineDef .LineWidth );_ddg .BS =_dcad .ToPdfObject ();_ggeda ,_ggdg ,_ffbbg :=_becd (lineDef );if _ffbbg !=nil {return nil ,_ffbbg ;};_ddg .AP =_ggeda ;_ddg .Rect =_d .MakeArrayFromFloats ([]float64 {_ggdg .Llx ,_ggdg .Lly ,_ggdg .Urx ,_ggdg .Ury });return _ddg .PdfAnnotation ,nil ;};func _gfa (_efa *_ef .PdfField ,_adda ,_adg float64 ,_fgcb string ,_faga AppearanceStyle ,_cfe *_b .ContentStreamOperations ,_fde *_ef .PdfPageResources ,_bde *_d .PdfObjectDictionary )(*_ef .XObjectForm ,error ){_gfd :=_ef .NewPdfPageResources ();_gecb ,_fb :=_adda ,_adg ;_ccdc :=_b .NewContentCreator ();if _faga .BorderSize > 0{_cbd (_ccdc ,_faga ,_adda ,_adg );};if _faga .DrawAlignmentReticle {_bacg :=_faga ;_bacg .BorderSize =0.2;_ffbb (_ccdc ,_bacg ,_adda ,_adg );};_ccdc .Add_BMC ("\u0054\u0078");_ccdc .Add_q ();_ccdc .Add_BT ();_adda ,_adg =_faga .applyRotation (_bde ,_adda ,_adg ,_ccdc );_fedg ,_efb ,_age :=_faga .processDA (_efa ,_cfe ,_fde ,_gfd ,_ccdc );if _age !=nil {return nil ,_age ;};_adbd :=_fedg .Font ;_cbb :=_fedg .Size ;_bcaef :=_d .MakeName (_fedg .Name );_ecdf :=_cbb ==0;if _ecdf &&_efb {_cbb =_adg *_faga .AutoFontSizeFraction ;};_efga :=_adbd .Encoder ();if _efga ==nil {_c .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");_efga =_f .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};if len (_fgcb )==0{return nil ,nil ;};_cge :=2.0;_geb :=0.0;if _efga !=nil {for _ ,_cbgd :=range _fgcb {_agc ,_faa :=_adbd .GetRuneMetrics (_cbgd );if !_faa {_c .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",_cbgd );continue ;};_geb +=_agc .Wx ;};_fgcb =string (_efga .Encode (_fgcb ));};if _cbb ==0||_ecdf &&_geb > 0&&_cge +_geb *_cbb /1000.0> _adda {_cbb =0.95*1000.0*(_adda -_cge )/_geb ;};_debe :=1.0*_cbb ;_ccga :=2.0;{_fcf :=_debe ;if _ecdf &&_ccga +_fcf > _adg {_cbb =0.95*(_adg -_ccga );_debe =1.0*_cbb ;_fcf =_debe ;};if _adg > _fcf {_ccga =(_adg -_fcf )/2.0;_ccga +=1.50;};};_ccdc .Add_Tf (*_bcaef ,_cbb );_ccdc .Add_Td (_cge ,_ccga );_ccdc .Add_Tj (*_d .MakeString (_fgcb ));_ccdc .Add_ET ();_ccdc .Add_Q ();_ccdc .Add_EMC ();_cad :=_ef .NewXObjectForm ();_cad .Resources =_gfd ;_cad .BBox =_d .MakeArrayFromFloats ([]float64 {0,0,_gecb ,_fb });_cad .SetContentStream (_ccdc .Bytes (),_ggdd ());return _cad ,nil ;};func (_aca *AppearanceStyle )processDA (_ccca *_ef .PdfField ,_ccgae *_b .ContentStreamOperations ,_gff ,_abda *_ef .PdfPageResources ,_abe *_b .ContentCreator )(*AppearanceFont ,bool ,error ){var _aggc *
// NewSignatureFieldOpts returns a new initialized instance of options
// used to generate a signature appearance.
func NewSignatureFieldOpts ()*SignatureFieldOpts {return &SignatureFieldOpts {Font :_ef .DefaultFont (),FontSize :10,LineHeight :1,AutoSize :true ,TextColor :_ef .NewPdfColorDeviceGray (0),BorderColor :_ef .NewPdfColorDeviceGray (0),FillColor :_ef .NewPdfColorDeviceGray (1),Encoder :_d .NewFlateEncoder (),ImagePosition :SignatureImageLeft };};
// SignatureLine represents a line of information in the signature field appearance.
type SignatureLine struct{Desc string ;Text string ;};
// CheckboxFieldOptions defines optional parameters for a checkbox field a form.
type CheckboxFieldOptions struct{Checked bool ;};func _abfe (_acb *_ef .PdfAnnotationWidget ,_fdc *_ef .PdfFieldText ,_cdga *_ef .PdfPageResources ,_gbg AppearanceStyle )(*_d .PdfObjectDictionary ,error ){_acg :=_ef .NewPdfPageResources ();_eeb ,_dfg :=_d .GetArray (_acb .Rect );if !_dfg {return nil ,_e .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_daf ,_adb :=_ef .NewPdfRectangle (*_eeb );if _adb !=nil {return nil ,_adb ;};_bbc ,_cdc :=_daf .Width (),_daf .Height ();_ggg ,_bcf :=_bbc ,_cdc ;_cgf ,_fgc :=_d .GetDict (_acb .MK );if _fgc {_ceb ,_ :=_d .GetDict (_acb .BS );_gccb :=_gbg .applyAppearanceCharacteristics (_cgf ,_ceb ,nil );if _gccb !=nil {return nil ,_gccb ;};};_bec ,_fgc :=_d .GetIntVal (_fdc .MaxLen );if !_fgc {return nil ,_e .New ("\u006d\u0061\u0078\u006c\u0065\u006e\u0020\u006e\u006ft\u0020\u0073\u0065\u0074");};if _bec <=0{return nil ,_e .New ("\u006d\u0061\u0078\u004c\u0065\u006e\u0020\u0069\u006ev\u0061\u006c\u0069\u0064");};_eba :=float64 (_bbc )/float64 (_bec );_gccf ,_adb :=_b .NewContentStreamParser (_cbc (_fdc .PdfField )).Parse ();if _adb !=nil {return nil ,_adb ;};_fcdd :=_b .NewContentCreator ();if _gbg .BorderSize > 0{_cbd (_fcdd ,_gbg ,_bbc ,_cdc );};if _gbg .DrawAlignmentReticle {_dga :=_gbg ;_dga .BorderSize =0.2;_ffbb (_fcdd ,_dga ,_bbc ,_cdc );};_fcdd .Add_BMC ("\u0054\u0078");_fcdd .Add_q ();_bbc ,_cdc =_gbg .applyRotation (_cgf ,_bbc ,_cdc ,_fcdd );_fcdd .Add_BT ();_fda ,_fdb ,_adb :=_gbg .processDA (_fdc .PdfField ,_gccf ,_cdga ,_acg ,_fcdd );if _adb !=nil {return nil ,_adb ;};_dba :=_fda .Font ;_dd :=_d .MakeName (_fda .Name );_cebe :=_fda .Size ;_gee :=_cebe ==0;if _gee &&_fdb {_cebe =_cdc *_gbg .AutoFontSizeFraction ;};_fad :=_dba .Encoder ();if _fad ==nil {_c .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");_fad =_f .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};var _bcc string ;if _cfae ,_ebaf :=_d .GetString (_fdc .V );_ebaf {_bcc =_cfae .Decoded ();};_fcdd .Add_Tf (*_dd ,_cebe );var _dc float64 ;for _ ,_bgbd :=range _bcc {_ecba ,_baa :=_dba .GetRuneMetrics (_bgbd );if !_baa {_c .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",_bgbd );continue ;};_bgg :=_ecba .Wy ;if int (_bgg )<=0{_bgg =_ecba .Wx ;};if _bgg > _dc {_dc =_bgg ;};};if int (_dc )==0{_c .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");_dc =1000;};_cdce ,_adb :=_dba .GetFontDescriptor ();if _adb !=nil {_c .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 _ecge float64 ;if _cdce !=nil {_ecge ,_adb =_cdce .GetCapHeight ();if _adb !=nil {_c .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\u0048\u0065\u0069\u0067\u0068t\u003a\u0020\u0025\u0076",_adb );};};if int (_ecge )<=0{_c .Log .Debug ("W\u0041\u0052\u004e\u003a\u0020\u0043\u0061\u0070\u0048e\u0069\u0067\u0068\u0074\u0020\u006e\u006ft \u0061\u0076\u0061\u0069l\u0061\u0062\u006c\u0065\u0020\u002d\u0020\u0073\u0065tt\u0069\u006eg\u0020\u0074\u006f\u0020\u0031\u00
// 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 *_ef .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_ef .PdfColorDeviceRGB ;Opacity float64 ;};
// NewCheckboxField generates a new checkbox field with partial name `name` at location `rect`
// on specified `page` and with field specific options `opt`.
func NewCheckboxField (page *_ef .PdfPage ,name string ,rect []float64 ,opt CheckboxFieldOptions )(*_ef .PdfFieldButton ,error ){if page ==nil {return nil ,_e .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_e .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 ,_e .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_becb ,_cba :=_ef .NewStandard14Font (_ef .ZapfDingbatsName );if _cba !=nil {return nil ,_cba ;};_eadf :=_ef .NewPdfField ();_fdaa :=&_ef .PdfFieldButton {};_eadf .SetContext (_fdaa );_fdaa .PdfField =_eadf ;_fdaa .T =_d .MakeString (name );_fdaa .SetType (_ef .ButtonTypeCheckbox );_eccb :="\u004f\u0066\u0066";if opt .Checked {_eccb ="\u0059\u0065\u0073";};_fdaa .V =_d .MakeName (_eccb );_dag :=_ef .NewPdfAnnotationWidget ();_dag .Rect =_d .MakeArrayFromFloats (rect );_dag .P =page .ToPdfObject ();_dag .F =_d .MakeInteger (4);_dag .Parent =_fdaa .ToPdfObject ();_aed :=rect [2]-rect [0];_gde :=rect [3]-rect [1];var _fffcb _ge .Buffer ;_fffcb .WriteString ("\u0071\u000a");_fffcb .WriteString ("\u0030 \u0030\u0020\u0031\u0020\u0072\u0067\n");_fffcb .WriteString ("\u0042\u0054\u000a");_fffcb .WriteString ("\u002f\u005a\u0061D\u0062\u0020\u0031\u0032\u0020\u0054\u0066\u000a");_fffcb .WriteString ("\u0045\u0054\u000a");_fffcb .WriteString ("\u0051\u000a");_eddd :=_b .NewContentCreator ();_eddd .Add_q ();_eddd .Add_rg (0,0,1);_eddd .Add_BT ();_eddd .Add_Tf (*_d .MakeName ("\u005a\u0061\u0044\u0062"),12);_eddd .Add_Td (0,0);_eddd .Add_ET ();_eddd .Add_Q ();_ffbc :=_ef .NewXObjectForm ();_ffbc .SetContentStream (_eddd .Bytes (),_d .NewRawEncoder ());_ffbc .BBox =_d .MakeArrayFromFloats ([]float64 {0,0,_aed ,_gde });_ffbc .Resources =_ef .NewPdfPageResources ();_ffbc .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_becb .ToPdfObject ());_eddd =_b .NewContentCreator ();_eddd .Add_q ();_eddd .Add_re (0,0,_aed ,_gde );_eddd .Add_W ().Add_n ();_eddd .Add_rg (0,0,1);_eddd .Translate (0,3.0);_eddd .Add_BT ();_eddd .Add_Tf (*_d .MakeName ("\u005a\u0061\u0044\u0062"),12);_eddd .Add_Td (0,0);_eddd .Add_Tj (*_d .MakeString ("\u0034"));_eddd .Add_ET ();_eddd .Add_Q ();_egaa :=_ef .NewXObjectForm ();_egaa .SetContentStream (_eddd .Bytes (),_d .NewRawEncoder ());_egaa .BBox =_d .MakeArrayFromFloats ([]float64 {0,0,_aed ,_gde });_egaa .Resources =_ef .NewPdfPageResources ();_egaa .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_becb .ToPdfObject ());_gaaa :=_d .MakeDict ();_gaaa .Set ("\u004f\u0066\u0066",_ffbc .ToPdfObject ());_gaaa .Set ("\u0059\u0065\u0073",_egaa .ToPdfObject ());_gcdb :=_d .MakeDict ();_gcdb .Set ("\u004e",_gaaa );_dag .AP =_gcdb ;_dag .AS =_d .MakeName (_eccb );_fdaa .Annotations =append (_fdaa .Annotations ,_dag );return _fdaa ,nil ;};func _becd (_ffef LineAnnotationDef )(*_d .PdfObjectDictionary ,*_ef .PdfRectangle ,error ){_egbf :=_ef .NewXObjectForm ();_egbf .Resources =_ef .NewPdfPageResources ();_beec :="";if _ffef .Opacity < 1.0{_deea :=_d .MakeDict ();_deea .Set ("\u0063\u0061",_d .MakeFloat (_ffef .Opacity ));_bfdd :=_egbf .Resources .AddExtGState ("\u0067\u0073\u0031",_deea );if _bfdd !=nil {_c .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 ,_bfdd ;};_beec ="\u0067\u0073\u0031";};_degb ,_bdgc ,_ecdc ,_gbce :=_fbd (_ffef ,_beec );if _gbce !=nil {return nil ,nil ,_gbce ;};_gbce =_egbf .SetContentStream (_degb ,nil );if _gbce !=nil {return nil ,nil ,_gbce ;};_egbf .BBox =_bdgc .ToPdfObject ();_cefa :=_d .MakeDict ();_cefa .Set ("\u004e",_egbf .ToPdfObject ());return _cefa ,_ecdc ,nil ;};func _fbd (_feda LineAnnotationDef ,_egc string )([]byte ,*_ef .PdfRectangle ,*_ef .PdfRectangle ,error ){_aba :=_fc .Line {X1 :0,Y1 :0,X2 :_feda .X2 -_feda .X1 ,Y2 :_feda .Y2 -_feda .Y1 ,LineColor :_fe
// 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).
CheckmarkRune rune ;BorderSize float64 ;BorderColor _ef .PdfColor ;FillColor _ef .PdfColor ;
// 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.
Fonts *AppearanceFontStyle ;};func _ea (_abb *_ef .PdfAnnotationWidget ,_faf *_ef .PdfFieldText ,_ccf *_ef .PdfPageResources ,_bag AppearanceStyle )(*_d .PdfObjectDictionary ,error ){_ccc :=_ef .NewPdfPageResources ();_eee ,_bge :=_d .GetArray (_abb .Rect );if !_bge {return nil ,_e .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_eca ,_bff :=_ef .NewPdfRectangle (*_eee );if _bff !=nil {return nil ,_bff ;};_bb ,_bfc :=_eca .Width (),_eca .Height ();_dg ,_bac :=_bb ,_bfc ;_dbe ,_fgg :=_d .GetDict (_abb .MK );if _fgg {_bee ,_ :=_d .GetDict (_abb .BS );_af :=_bag .applyAppearanceCharacteristics (_dbe ,_bee ,nil );if _af !=nil {return nil ,_af ;};};_ggf ,_bff :=_b .NewContentStreamParser (_cbc (_faf .PdfField )).Parse ();if _bff !=nil {return nil ,_bff ;};_bbf :=_b .NewContentCreator ();if _bag .BorderSize > 0{_cbd (_bbf ,_bag ,_bb ,_bfc );};if _bag .DrawAlignmentReticle {_fca :=_bag ;_fca .BorderSize =0.2;_ffbb (_bbf ,_fca ,_bb ,_bfc );};_bbf .Add_BMC ("\u0054\u0078");_bbf .Add_q ();_bb ,_bfc =_bag .applyRotation (_dbe ,_bb ,_bfc ,_bbf );_bbf .Add_BT ();_afe ,_ecg ,_bff :=_bag .processDA (_faf .PdfField ,_ggf ,_ccf ,_ccc ,_bbf );if _bff !=nil {return nil ,_bff ;};_bgee :=_afe .Font ;_bfcf :=_afe .Size ;_aa :=_d .MakeName (_afe .Name );_gbe :=_bfcf ==0;if _gbe &&_ecg {_bfcf =_bfc *_bag .AutoFontSizeFraction ;};_gbb :=_bgee .Encoder ();if _gbb ==nil {_c .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");_gbb =_f .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};_gag ,_bff :=_bgee .GetFontDescriptor ();if _bff !=nil {_c .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 _egb string ;if _fafa ,_cgb :=_d .GetString (_faf .V );_cgb {_egb =_fafa .Decoded ();};if len (_egb )==0{return nil ,nil ;};_cb :=[]string {_egb };_cf :=false ;if _faf .Flags ().Has (_ef .FieldFlagMultiline ){_cf =true ;_egb =_a .Replace (_egb ,"\u000d\u000a","\u000a",-1);_egb =_a .Replace (_egb ,"\u000d","\u000a",-1);_cb =_a .Split (_egb ,"\u000a");};_gc :=0.0;_add :=0;if _gbb !=nil {_acf :=len (_cb );_ada :=0;for _ada < _acf {var _gcc float64 ;_dgf :=-1;_adf :=0.0;for _cdg ,_bfcc :=range _cb [_ada ]{if _bfcc ==' '{_dgf =_cdg ;_gcc =_adf ;};_fge ,_abf :=_bgee .GetRuneMetrics (_bfcc );if !_abf {_c .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",_bfcc );continue ;};_adf +=_fge .Wx ;if _cf &&!_gbe &&_bfcf *_adf /1000.0> _bb &&_dgf > 0{_fgea :=_cb [_ada ][_dgf +1:];if _ada < len (_cb )-1{_cb =append (_cb [:_ada +1],_cb [_ada :]...);_cb [_ada +1]=_fgea ;}else {_cb =append (_cb ,_fgea );};_acf ++;_cb [_ada ]=_cb [_ada ][0:_dgf ];_adf =_gcc ;break ;};};if _adf > _gc {_gc =_adf ;};_cb [_ada ]=string (_gbb .Encode (_cb [_ada ]));if len (_cb [_ada ])> 0{_add ++;};_ada ++;};};_gec :=2.0;if _bfcf ==0||_gbe &&_gc > 0&&_gec +_gc *_bfcf /1000.0> _bb {_bfcf =0.95*1000.0*(_bb -_gec )/_gc ;};_bffe :=_gg ;{if _ega ,_fec :=_d .GetIntVal (_faf .Q );_fec {switch _ega {case 0:_bffe =_gg ;case 1:_bffe =_bga ;case 2:_bffe =_edg ;default:_c .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u003a\u0020\u0055\u006e\u0073\u0075\u0070\u0070\u006f\u0072t\u0065\u0064\u0020\u0071\u0075\u0061\u0064\u0064\u0069\u006e\u0067\u003a\u0020%\u0064\u0020\u002d\u0020\u0075\u0073\u0069\u006e\u0067\u0020\u006c\u0065ft\u0020\u0061\u006c\u0069\u0067\u006e\u006d\u0065\u006e\u0074",_ega );};};};_aaf :=_bag .
2020-10-12 14:17:59 +00:00
// 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.
2020-10-19 10:58:10 +00:00
Font *_ef .PdfFont ;
2020-10-12 14:17:59 +00:00
// 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 ;};
2020-10-19 10:58:10 +00:00
// SignatureImagePosition specifies the image signature location relative to the text signature.
// If text signature is not defined, this position will be ignored.
type SignatureImagePosition int ;func _ggdd ()_d .StreamEncoder {return _d .NewFlateEncoder ()};
2020-08-27 21:45:09 +00:00
2020-10-19 10:58:10 +00:00
// SetStyle applies appearance `style` to `fa`.
func (_gb *FieldAppearance )SetStyle (style AppearanceStyle ){_gb ._ee =&style };func _cbc (_dcg *_ef .PdfField )string {if _dcg ==nil {return "";};_cac ,_ecfa :=_dcg .GetContext ().(*_ef .PdfFieldText );if !_ecfa {return _cbc (_dcg .Parent );};if _cac .DA !=nil {return _cac .DA .Str ();};return _cbc (_cac .Parent );};const (SignatureImageLeft SignatureImagePosition =iota ;SignatureImageRight ;SignatureImageTop ;SignatureImageBottom ;);
2020-08-27 21:45:09 +00:00
2020-10-19 10:58:10 +00:00
// ComboboxFieldOptions defines optional parameters for a combobox form field.
type ComboboxFieldOptions struct{
2020-08-27 21:45:09 +00:00
2020-10-19 10:58:10 +00:00
// Choices is the list of string values that can be selected.
Choices []string ;};
2020-08-27 21:45:09 +00:00
2020-10-19 10:58:10 +00:00
// 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 (_gbc FieldAppearance )WrapContentStream (page *_ef .PdfPage )error {_dbag ,_cda :=page .GetAllContentStreams ();if _cda !=nil {return _cda ;};_gcfg :=_b .NewContentStreamParser (_dbag );_cfaa ,_cda :=_gcfg .Parse ();if _cda !=nil {return _cda ;};_cfaa .WrapIfNeeded ();_fcb :=[]string {_cfaa .String ()};return page .SetContentStreams (_fcb ,_ggdd ());};type quadding int ;func (_eccd *AppearanceStyle )applyAppearanceCharacteristics (_bbe *_d .PdfObjectDictionary ,_bggb *_d .PdfObjectDictionary ,_cef *_ef .PdfFont )error {if !_eccd .AllowMK {return nil ;};if CA ,_cdge :=_d .GetString (_bbe .Get ("\u0043\u0041"));_cdge &&_cef !=nil {_fagad :=CA .Bytes ();if len (_fagad )!=0{_bbff :=[]rune (_cef .Encoder ().Decode (_fagad ));if len (_bbff )==1{_eccd .CheckmarkRune =_bbff [0];};};};if BC ,_agea :=_d .GetArray (_bbe .Get ("\u0042\u0043"));_agea {_bdde ,_gbbd :=BC .ToFloat64Array ();if _gbbd !=nil {return _gbbd ;};switch len (_bdde ){case 1:_eccd .BorderColor =_ef .NewPdfColorDeviceGray (_bdde [0]);case 3:_eccd .BorderColor =_ef .NewPdfColorDeviceRGB (_bdde [0],_bdde [1],_bdde [2]);case 4:_eccd .BorderColor =_ef .NewPdfColorDeviceCMYK (_bdde [0],_bdde [1],_bdde [2],_bdde [3]);default:_c .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 (_bdde ));};if _bggb !=nil {if _dcbf ,_eadb :=_d .GetNumberAsFloat (_bggb .Get ("\u0057"));_eadb ==nil {_eccd .BorderSize =_dcbf ;};};};if BG ,_caf :=_d .GetArray (_bbe .Get ("\u0042\u0047"));_caf {_fba ,_eadd :=BG .ToFloat64Array ();if _eadd !=nil {return _eadd ;};switch len (_fba ){case 1:_eccd .FillColor =_ef .NewPdfColorDeviceGray (_fba [0]);case 3:_eccd .FillColor =_ef .NewPdfColorDeviceRGB (_fba [0],_fba [1],_fba [2]);case 4:_eccd .FillColor =_ef .NewPdfColorDeviceCMYK (_fba [0],_fba [1],_fba [2],_fba [3]);default:_c .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 (_fba ));};};return nil ;};
2020-08-27 21:45:09 +00:00
2020-10-19 10:58:10 +00:00
// Style returns the appearance style of `fa`. If not specified, returns default style.
func (_ba FieldAppearance )Style ()AppearanceStyle {if _ba ._ee !=nil {return *_ba ._ee ;};return AppearanceStyle {AutoFontSizeFraction :0.65,CheckmarkRune :'✔',BorderSize :0.0,BorderColor :_ef .NewPdfColorDeviceGray (0),FillColor :_ef .NewPdfColorDeviceGray (1),MultilineLineHeight :1.2,MultilineVAlignMiddle :false ,DrawAlignmentReticle :false ,AllowMK :true };};
2020-08-31 21:12:07 +00:00
2020-10-19 10:58:10 +00:00
// NewSignatureField returns a new signature field with a visible appearance
// containing the specified signature lines and styled according to the
// specified options.
func NewSignatureField (signature *_ef .PdfSignature ,lines []*SignatureLine ,opts *SignatureFieldOpts )(*_ef .PdfFieldSignature ,error ){if signature ==nil {return nil ,_e .New ("\u0073\u0069\u0067na\u0074\u0075\u0072\u0065\u0020\u0063\u0061\u006e\u006e\u006f\u0074\u0020\u0062\u0065\u0020\u006e\u0069\u006c");};_fecb ,_ggfg :=_caa (lines ,opts );if _ggfg !=nil {return nil ,_ggfg ;};_dccd :=_ef .NewPdfFieldSignature (signature );_dccd .Rect =_d .MakeArrayFromFloats (opts .Rect );_dccd .AP =_fecb ;return _dccd ,nil ;};
2020-08-31 21:12:07 +00:00
2020-10-19 10:58:10 +00:00
// CreateRectangleAnnotation creates a rectangle annotation object that can be added to page PDF annotations.
func CreateRectangleAnnotation (rectDef RectangleAnnotationDef )(*_ef .PdfAnnotation ,error ){_cggf :=_ef .NewPdfAnnotationSquare ();if rectDef .BorderEnabled {_efbe ,_bdbf ,_cefb :=rectDef .BorderColor .R (),rectDef .BorderColor .G (),rectDef .BorderColor .B ();_cggf .C =_d .MakeArrayFromFloats ([]float64 {_efbe ,_bdbf ,_cefb });_ebb :=_ef .NewBorderStyle ();_ebb .SetBorderWidth (rectDef .BorderWidth );_cggf .BS =_ebb .ToPdfObject ();};if rectDef .FillEnabled {_afaf ,_gbcd ,_bbcd :=rectDef .FillColor .R (),rectDef .FillColor .G (),rectDef .FillColor .B ();_cggf .IC =_d .MakeArrayFromFloats ([]float64 {_afaf ,_gbcd ,_bbcd });}else {_cggf .IC =_d .MakeArrayFromIntegers ([]int {});};if rectDef .Opacity < 1.0{_cggf .CA =_d .MakeFloat (rectDef .Opacity );};_dfb ,_bgeg ,_bdgb :=_afbe (rectDef );if _bdgb !=nil {return nil ,_bdgb ;};_cggf .AP =_dfb ;_cggf .Rect =_d .MakeArrayFromFloats ([]float64 {_bgeg .Llx ,_bgeg .Lly ,_bgeg .Urx ,_bgeg .Ury });return _cggf .PdfAnnotation ,nil ;};