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-08-31 21:12:07 +00:00
|
|
|
package annotator ;import (_f "bytes";_dd "errors";_d "github.com/unidoc/unipdf/v3/common";_b "github.com/unidoc/unipdf/v3/contentstream";_be "github.com/unidoc/unipdf/v3/contentstream/draw";_aa "github.com/unidoc/unipdf/v3/core";_a "github.com/unidoc/unipdf/v3/internal/textencoding";_ce "github.com/unidoc/unipdf/v3/model";_ac "math";_g "strings";_af "unicode";);
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +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 (_abac FieldAppearance )WrapContentStream (page *_ce .PdfPage )error {_age ,_cfd :=page .GetAllContentStreams ();if _cfd !=nil {return _cfd ;};_debf :=_b .NewContentStreamParser (_age );_fgca ,_cfd :=_debf .Parse ();if _cfd !=nil {return _cfd ;};_fgca .WrapIfNeeded ();_eff :=[]string {_fgca .String ()};return page .SetContentStreams (_eff ,_dfa ());};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +00:00
|
|
|
// NewSignatureFieldOpts returns a new initialized instance of options
|
|
|
|
// used to generate a signature appearance.
|
|
|
|
func NewSignatureFieldOpts ()*SignatureFieldOpts {return &SignatureFieldOpts {Font :_ce .DefaultFont (),FontSize :10,LineHeight :1,AutoSize :true ,TextColor :_ce .NewPdfColorDeviceGray (0),BorderColor :_ce .NewPdfColorDeviceGray (0),FillColor :_ce .NewPdfColorDeviceGray (1)};};func _fgdg (_dgg *_b .ContentCreator ,_aad AppearanceStyle ,_gecc ,_edab float64 ){_dgg .Add_q ().Add_re (0,0,_gecc ,_edab ).Add_re (0,_edab /2,_gecc ,_edab /2).Add_re (0,0,_gecc ,_edab ).Add_re (_gecc /2,0,_gecc /2,_edab ).Add_w (_aad .BorderSize ).SetStrokingColor (_aad .BorderColor ).SetNonStrokingColor (_aad .FillColor ).Add_B ().Add_Q ();};
|
|
|
|
|
|
|
|
// 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 _ce .PdfColor ;FillColor _ce .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 ;};
|
2020-08-27 21:45:09 +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-08-31 21:12:07 +00:00
|
|
|
Font *_ce .PdfFont ;
|
2020-08-27 21:45:09 +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.
|
2020-08-31 21:12:07 +00:00
|
|
|
Size float64 ;};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +00:00
|
|
|
// 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 *_ce .PdfPage ,name string ,rect []float64 ,opt CheckboxFieldOptions )(*_ce .PdfFieldButton ,error ){if page ==nil {return nil ,_dd .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_dd .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 ,_dd .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_cgcg ,_bab :=_ce .NewStandard14Font (_ce .ZapfDingbatsName );if _bab !=nil {return nil ,_bab ;};_dfba :=_ce .NewPdfField ();_gdbg :=&_ce .PdfFieldButton {};_dfba .SetContext (_gdbg );_gdbg .PdfField =_dfba ;_gdbg .T =_aa .MakeString (name );_gdbg .SetType (_ce .ButtonTypeCheckbox );_ccbc :="\u004f\u0066\u0066";if opt .Checked {_ccbc ="\u0059\u0065\u0073";};_gdbg .V =_aa .MakeName (_ccbc );_edeb :=_ce .NewPdfAnnotationWidget ();_edeb .Rect =_aa .MakeArrayFromFloats (rect );_edeb .P =page .ToPdfObject ();_edeb .F =_aa .MakeInteger (4);_edeb .Parent =_gdbg .ToPdfObject ();_ccbcf :=rect [2]-rect [0];_efgdd :=rect [3]-rect [1];var _efeb _f .Buffer ;_efeb .WriteString ("\u0071\u000a");_efeb .WriteString ("\u0030 \u0030\u0020\u0031\u0020\u0072\u0067\n");_efeb .WriteString ("\u0042\u0054\u000a");_efeb .WriteString ("\u002f\u005a\u0061D\u0062\u0020\u0031\u0032\u0020\u0054\u0066\u000a");_efeb .WriteString ("\u0045\u0054\u000a");_efeb .WriteString ("\u0051\u000a");_eagd :=_b .NewContentCreator ();_eagd .Add_q ();_eagd .Add_rg (0,0,1);_eagd .Add_BT ();_eagd .Add_Tf (*_aa .MakeName ("\u005a\u0061\u0044\u0062"),12);_eagd .Add_Td (0,0);_eagd .Add_ET ();_eagd .Add_Q ();_fgae :=_ce .NewXObjectForm ();_fgae .SetContentStream (_eagd .Bytes (),_aa .NewRawEncoder ());_fgae .BBox =_aa .MakeArrayFromFloats ([]float64 {0,0,_ccbcf ,_efgdd });_fgae .Resources =_ce .NewPdfPageResources ();_fgae .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_cgcg .ToPdfObject ());_eagd =_b .NewContentCreator ();_eagd .Add_q ();_eagd .Add_re (0,0,_ccbcf ,_efgdd );_eagd .Add_W ().Add_n ();_eagd .Add_rg (0,0,1);_eagd .Translate (0,3.0);_eagd .Add_BT ();_eagd .Add_Tf (*_aa .MakeName ("\u005a\u0061\u0044\u0062"),12);_eagd .Add_Td (0,0);_eagd .Add_Tj (*_aa .MakeString ("\u0034"));_eagd .Add_ET ();_eagd .Add_Q ();_cgf :=_ce .NewXObjectForm ();_cgf .SetContentStream (_eagd .Bytes (),_aa .NewRawEncoder ());_cgf .BBox =_aa .MakeArrayFromFloats ([]float64 {0,0,_ccbcf ,_efgdd });_cgf .Resources =_ce .NewPdfPageResources ();_cgf .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_cgcg .ToPdfObject ());_feaa :=_aa .MakeDict ();_feaa .Set ("\u004f\u0066\u0066",_fgae .ToPdfObject ());_feaa .Set ("\u0059\u0065\u0073",_cgf .ToPdfObject ());_gaaa :=_aa .MakeDict ();_gaaa .Set ("\u004e",_feaa );_edeb .AP =_gaaa ;_edeb .AS =_aa .MakeName (_ccbc );_gdbg .Annotations =append (_gdbg .Annotations ,_edeb );return _gdbg ,nil ;};const (_dge quadding =0;_ccg quadding =1;_dc quadding =2;);func (_dged *AppearanceStyle )processDA (_eeed *_ce .PdfField ,_gfd *_b .ContentStreamOperations ,_fcc ,_bbb *_ce .PdfPageResources ,_agce *_b .ContentCreator )(*AppearanceFont ,bool ,error ){var _eace *AppearanceFont ;var _cdfa bool ;if _dged .Fonts !=nil {if _dged .Fonts .Fallback !=nil {_eace =_dged .Fonts .Fallback ;};if _eef :=_dged .Fonts .FieldFallbacks ;_eef !=nil {if _dfce ,_badf :=_eef [_eeed .PartialName ()];_badf {_eace =_dfce ;}else if _cbe ,_agfb :=_eeed .FullName ();_agfb ==nil {if _abf ,_efgd :=_eef [_cbe ];_efgd {_eace =_abf ;};};};_cdfa =_dged .Fonts .ForceReplace ;};var _dbd string ;var _gdc float64 ;var _aadf bool ;if _gfd !=nil {for _ ,_bcdd :=range *_gfd {if _bcdd .Operand =="\u0054\u0066"&&len (_bcdd .Params )==2{if _efcb ,_ggd :=_aa .GetNameVal (_bcdd .Params [0]);_ggd {_dbd =_efcb ;};if _eded ,_dgbcc :=_aa .GetNumberAsFloat (_bcdd .Params [1]);_dgbcc ==nil {_gdc =_eded ;};_aadf =true ;continue ;};_agce .AddOperand (*_bcdd );};};var _cfce *AppearanceFont ;var _faa _aa .PdfO
|
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-08-31 21:12:07 +00:00
|
|
|
Font *_ce .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-08-31 21:12:07 +00:00
|
|
|
TextColor _ce .PdfColor ;
|
2020-08-27 21:45:09 +00:00
|
|
|
|
|
|
|
// FillColor represents the background color of the appearance annotation area.
|
2020-08-31 21:12:07 +00:00
|
|
|
FillColor _ce .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-08-31 21:12:07 +00:00
|
|
|
BorderColor _ce .PdfColor ;};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +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).
|
|
|
|
type FieldAppearance struct{OnlyIfMissing bool ;RegenerateTextFields bool ;_bd *AppearanceStyle ;};func _ba (_ge CircleAnnotationDef ,_e string )([]byte ,*_ce .PdfRectangle ,*_ce .PdfRectangle ,error ){_ca :=_be .Circle {X :_ge .X ,Y :_ge .Y ,Width :_ge .Width ,Height :_ge .Height ,FillEnabled :_ge .FillEnabled ,FillColor :_ge .FillColor ,BorderEnabled :_ge .BorderEnabled ,BorderWidth :_ge .BorderWidth ,BorderColor :_ge .BorderColor ,Opacity :_ge .Opacity };_fa ,_ceb ,_bg :=_ca .Draw (_e );if _bg !=nil {return nil ,nil ,nil ,_bg ;};_aff :=&_ce .PdfRectangle {};_aff .Llx =_ge .X +_ceb .Llx ;_aff .Lly =_ge .Y +_ceb .Lly ;_aff .Urx =_ge .X +_ceb .Urx ;_aff .Ury =_ge .Y +_ceb .Ury ;return _fa ,_ceb ,_aff ,nil ;};func _eebd (_fbd RectangleAnnotationDef ,_fdbb string )([]byte ,*_ce .PdfRectangle ,*_ce .PdfRectangle ,error ){_eeg :=_be .Rectangle {X :0,Y :0,Width :_fbd .Width ,Height :_fbd .Height ,FillEnabled :_fbd .FillEnabled ,FillColor :_fbd .FillColor ,BorderEnabled :_fbd .BorderEnabled ,BorderWidth :2*_fbd .BorderWidth ,BorderColor :_fbd .BorderColor ,Opacity :_fbd .Opacity };_cdfe ,_afde ,_bfbf :=_eeg .Draw (_fdbb );if _bfbf !=nil {return nil ,nil ,nil ,_bfbf ;};_caaf :=&_ce .PdfRectangle {};_caaf .Llx =_fbd .X +_afde .Llx ;_caaf .Lly =_fbd .Y +_afde .Lly ;_caaf .Urx =_fbd .X +_afde .Urx ;_caaf .Ury =_fbd .Y +_afde .Ury ;return _cdfe ,_afde ,_caaf ,nil ;};func (_ede *AppearanceStyle )applyAppearanceCharacteristics (_bbe *_aa .PdfObjectDictionary ,_eabg *_aa .PdfObjectDictionary ,_ggbe *_ce .PdfFont )error {if !_ede .AllowMK {return nil ;};if _eabg !=nil {if _dgba ,_cda :=_aa .GetNumberAsFloat (_eabg .Get ("\u0057"));_cda ==nil {_ede .BorderSize =_dgba ;};};if CA ,_gaa :=_aa .GetString (_bbe .Get ("\u0043\u0041"));_gaa &&_ggbe !=nil {_ebc :=CA .Bytes ();if len (_ebc )!=0{_afg :=[]rune (_ggbe .Encoder ().Decode (_ebc ));if len (_afg )==1{_ede .CheckmarkRune =_afg [0];};};};if BC ,_gda :=_aa .GetArray (_bbe .Get ("\u0042\u0043"));_gda {_bbc ,_cgce :=BC .ToFloat64Array ();if _cgce !=nil {return _cgce ;};switch len (_bbc ){case 1:_ede .BorderColor =_ce .NewPdfColorDeviceGray (_bbc [0]);case 3:_ede .BorderColor =_ce .NewPdfColorDeviceRGB (_bbc [0],_bbc [1],_bbc [2]);case 4:_ede .BorderColor =_ce .NewPdfColorDeviceCMYK (_bbc [0],_bbc [1],_bbc [2],_bbc [3]);default:_d .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 (_bbc ));};};if BG ,_fgbc :=_aa .GetArray (_bbe .Get ("\u0042\u0047"));_fgbc {_dgab ,_dcf :=BG .ToFloat64Array ();if _dcf !=nil {return _dcf ;};switch len (_dgab ){case 1:_ede .FillColor =_ce .NewPdfColorDeviceGray (_dgab [0]);case 3:_ede .FillColor =_ce .NewPdfColorDeviceRGB (_dgab [0],_dgab [1],_dgab [2]);case 4:_ede .FillColor =_ce .NewPdfColorDeviceCMYK (_dgab [0],_dgab [1],_dgab [2],_dgab [3]);default:_d .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 (_dgab ));};};return nil ;};func _gdb (_abab *_ce .PdfField )string {if _abab ==nil {return "";};_cea ,_ecg :=_abab .GetContext ().(*_ce .PdfFieldText );if !_ecg {return _gdb (_abab .Parent );};if _cea .DA !=nil {return _cea .DA .Str ();};return _gdb (_cea .Parent );};func (_cbcc *AppearanceStyle )applyRotation (_bdad *_aa .PdfObjectDictionary ,_befb ,_bdb float64 ,_gca *_b .ContentCreator )(float64 ,float64 ){if !_cbcc .AllowMK {return _befb ,_bdb ;};if _bdad ==nil {return _befb ,_bdb ;};_eeadb ,_ :=_aa .GetNumberAsFloat (_bdad .Get ("\u0052"));if _eeadb ==0{return _befb ,_bdb ;};_gbc :=-_eeadb ;_efefc :=_be .Path {Points :[]_be .Point {_be .NewPoint (0,0).Rotate (_gbc ),_be .NewPoint (_befb ,0).Rotate (_gbc ),_be .NewPoint (0,_bdb ).Rotate (_gbc )
|
2020-08-27 21:45:09 +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-08-31 21:12:07 +00:00
|
|
|
func NewSignatureField (signature *_ce .PdfSignature ,lines []*SignatureLine ,opts *SignatureFieldOpts )(*_ce .PdfFieldSignature ,error ){if signature ==nil {return nil ,_dd .New ("\u0073\u0069\u0067na\u0074\u0075\u0072\u0065\u0020\u0063\u0061\u006e\u006e\u006f\u0074\u0020\u0062\u0065\u0020\u006e\u0069\u006c");};_dfeg ,_cdea :=_adce (lines ,opts );if _cdea !=nil {return nil ,_cdea ;};_dbeg :=_ce .NewPdfFieldSignature (signature );_dbeg .Rect =_aa .MakeArrayFromFloats (opts .Rect );_dbeg .AP =_dfeg ;return _dbeg ,nil ;};func _efef (_bfcg *_ce .PdfAnnotationWidget ,_fbb *_ce .PdfFieldButton ,_efbb *_ce .PdfPageResources ,_ege AppearanceStyle )(*_aa .PdfObjectDictionary ,error ){_eba ,_add :=_aa .GetArray (_bfcg .Rect );if !_add {return nil ,_dd .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_cdc ,_bfe :=_ce .NewPdfRectangle (*_eba );if _bfe !=nil {return nil ,_bfe ;};_geg ,_bfec :=_cdc .Width (),_cdc .Height ();_gdf ,_eaf :=_geg ,_bfec ;_d .Log .Debug ("\u0043\u0068\u0065\u0063kb\u006f\u0078\u002c\u0020\u0077\u0061\u0020\u0042\u0053\u003a\u0020\u0025\u0076",_bfcg .BS );_cbg ,_bfe :=_ce .NewStandard14Font ("\u005a\u0061\u0070f\u0044\u0069\u006e\u0067\u0062\u0061\u0074\u0073");if _bfe !=nil {return nil ,_bfe ;};_deeg ,_aba :=_aa .GetDict (_bfcg .MK );if _aba {_ecb ,_ :=_aa .GetDict (_bfcg .BS );_ddd :=_ege .applyAppearanceCharacteristics (_deeg ,_ecb ,_cbg );if _ddd !=nil {return nil ,_ddd ;};};_cefd :=_ce .NewXObjectForm ();{_aeg :=_b .NewContentCreator ();if _ege .BorderSize > 0{_ebe (_aeg ,_ege ,_geg ,_bfec );};if _ege .DrawAlignmentReticle {_bgg :=_ege ;_bgg .BorderSize =0.2;_fgdg (_aeg ,_bgg ,_geg ,_bfec );};_geg ,_bfec =_ege .applyRotation (_deeg ,_geg ,_bfec ,_aeg );_cee :=_ege .AutoFontSizeFraction *_bfec ;_ffa ,_cab :=_cbg .GetRuneMetrics (_ege .CheckmarkRune );if !_cab {return nil ,_dd .New ("\u0067l\u0079p\u0068\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064");};_dgfa :=_cbg .Encoder ();_cbd :=_dgfa .Encode (string (_ege .CheckmarkRune ));_gfc :=_ffa .Wx *_cee /1000.0;_gafg :=705.0;_ebad :=_gafg /1000.0*_cee ;_bfb :=2.0;_edd :=1.0;if _gfc < _geg {_bfb =(_geg -_gfc )/2.0;};if _ebad < _bfec {_edd =(_bfec -_ebad )/2.0;};_aeg .Add_q ().Add_g (0).Add_BT ().Add_Tf ("\u005a\u0061\u0044\u0062",_cee ).Add_Td (_bfb ,_edd ).Add_Tj (*_aa .MakeStringFromBytes (_cbd )).Add_ET ().Add_Q ();_cefd .Resources =_ce .NewPdfPageResources ();_cefd .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_cbg .ToPdfObject ());_cefd .BBox =_aa .MakeArrayFromFloats ([]float64 {0,0,_gdf ,_eaf });_cefd .SetContentStream (_aeg .Bytes (),_dfa ());};_cdb :=_ce .NewXObjectForm ();{_bcd :=_b .NewContentCreator ();if _ege .BorderSize > 0{_ebe (_bcd ,_ege ,_geg ,_bfec );};_cdb .BBox =_aa .MakeArrayFromFloats ([]float64 {0,0,_gdf ,_eaf });_cdb .SetContentStream (_bcd .Bytes (),_dfa ());};_cdf :=_aa .MakeDict ();_cdf .Set ("\u004f\u0066\u0066",_cdb .ToPdfObject ());_cdf .Set ("\u0059\u0065\u0073",_cefd .ToPdfObject ());_agb :=_aa .MakeDict ();_agb .Set ("\u004e",_cdf );return _agb ,nil ;};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +00:00
|
|
|
// Style returns the appearance style of `fa`. If not specified, returns default style.
|
|
|
|
func (_bb FieldAppearance )Style ()AppearanceStyle {if _bb ._bd !=nil {return *_bb ._bd ;};return AppearanceStyle {AutoFontSizeFraction :0.65,CheckmarkRune :'✔',BorderSize :0.0,BorderColor :_ce .NewPdfColorDeviceGray (0),FillColor :_ce .NewPdfColorDeviceGray (1),MultilineLineHeight :1.2,MultilineVAlignMiddle :false ,DrawAlignmentReticle :false ,AllowMK :true };};func _adce (_dddb []*SignatureLine ,_adec *SignatureFieldOpts )(*_aa .PdfObjectDictionary ,error ){if _adec ==nil {_adec =NewSignatureFieldOpts ();};var _fddbd error ;var _gbce *_aa .PdfObjectName ;_bbed :=_adec .Font ;if _bbed !=nil {_egg ,_ :=_bbed .GetFontDescriptor ();if _egg !=nil {if _dag ,_dba :=_egg .FontName .(*_aa .PdfObjectName );_dba {_gbce =_dag ;};};if _gbce ==nil {_gbce =_aa .MakeName ("\u0046\u006f\u006et\u0031");};}else {if _bbed ,_fddbd =_ce .NewStandard14Font ("\u0048e\u006c\u0076\u0065\u0074\u0069\u0063a");_fddbd !=nil {return nil ,_fddbd ;};_gbce =_aa .MakeName ("\u0048\u0065\u006c\u0076");};_gfee :=_adec .FontSize ;if _gfee <=0{_gfee =10;};if _adec .LineHeight <=0{_adec .LineHeight =1;};_ebb :=_adec .LineHeight *_gfee ;_cbce ,_bbcd :=_bbed .GetRuneMetrics (' ');if !_bbcd {return nil ,_dd .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");};_caf :=_cbce .Wx ;var _bag float64 ;var _eefb []string ;for _ ,_edda :=range _dddb {if _edda .Text ==""{continue ;};_ddc :=_edda .Text ;if _edda .Desc !=""{_ddc =_edda .Desc +"\u003a\u0020"+_ddc ;};_eefb =append (_eefb ,_ddc );var _afa float64 ;for _ ,_edfb :=range _ddc {_egf ,_daga :=_bbed .GetRuneMetrics (_edfb );if !_daga {continue ;};_afa +=_egf .Wx ;};if _afa > _bag {_bag =_afa ;};};_bag =_bag *_gfee /1000.0;_bbee :=float64 (len (_eefb ))*_ebb ;_cfb :=_adec .Rect ;if _cfb ==nil {_cfb =[]float64 {0,0,_bag ,_bbee };_adec .Rect =_cfb ;};_ggee :=_cfb [2]-_cfb [0];_cdgd :=_cfb [3]-_cfb [1];var _ddcd float64 ;if _adec .AutoSize {if _bag > _ggee ||_bbee > _cdgd {_degfb :=_ac .Min (_ggee /_bag ,_cdgd /_bbee );_gfee *=_degfb ;};_ebb =_adec .LineHeight *_gfee ;_ddcd +=(_cdgd -float64 (len (_eefb ))*_ebb )/2;};_aega :=_b .NewContentCreator ();if _adec .BorderSize <=0{_adec .BorderSize =0;_adec .BorderColor =_ce .NewPdfColorDeviceGray (1);};if _adec .BorderColor ==nil {_adec .BorderColor =_ce .NewPdfColorDeviceGray (1);};if _adec .FillColor ==nil {_adec .FillColor =_ce .NewPdfColorDeviceGray (1);};_aega .Add_q ().SetNonStrokingColor (_adec .FillColor ).SetStrokingColor (_adec .BorderColor ).Add_w (_adec .BorderSize ).Add_re (_cfb [0],_cfb [1],_ggee ,_cdgd ).Add_B ().Add_Q ();_aega .Add_q ();_aega .Translate (_cfb [0],_cfb [3]-_ebb -_ddcd );_aega .Add_BT ();_caa :=_bbed .Encoder ();for _ ,_afdc :=range _eefb {var _adff []byte ;for _ ,_dcd :=range _afdc {if _af .IsSpace (_dcd ){if len (_adff )> 0{_aega .SetNonStrokingColor (_adec .TextColor ).Add_Tf (*_gbce ,_gfee ).Add_TL (_ebb ).Add_TJ ([]_aa .PdfObject {_aa .MakeStringFromBytes (_adff )}...);_adff =nil ;};_aega .Add_Tf (*_gbce ,_gfee ).Add_TL (_ebb ).Add_TJ ([]_aa .PdfObject {_aa .MakeFloat (-_caf )}...);}else {_adff =append (_adff ,_caa .Encode (string (_dcd ))...);};};if len (_adff )> 0{_aega .SetNonStrokingColor (_adec .TextColor ).Add_Tf (*_gbce ,_gfee ).Add_TL (_ebb ).Add_TJ ([]_aa .PdfObject {_aa .MakeStringFromBytes (_adff )}...);};_aega .Add_Td (0,-_ebb );};_aega .Add_ET ();_aega .Add_Q ();_aef :=_ce .NewPdfPageResources ();_aef .SetFontByName (*_gbce ,_bbed .ToPdfObject ());_gcf :=_ce .NewXObjectForm ();_gcf .Resources =_aef ;_gcf .BBox =_aa .MakeArrayFromFloats (_cfb );_gcf .SetContentStream (_aega .Bytes (),_dfa ());_ebbc :=_aa .MakeDict ();_ebbc .Set ("\u004e",_gcf .ToPdfObject ());return _ebbc ,nil ;};func _dfa ()_aa .StreamEncoder {return _aa .NewFlateEncoder ()};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +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 *_ce .PdfPage ,name string ,rect []float64 ,opt TextFieldOptions )(*_ce .PdfFieldText ,error ){if page ==nil {return nil ,_dd .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_dd .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 ,_dd .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_bdbg :=_ce .NewPdfField ();_afda :=&_ce .PdfFieldText {};_bdbg .SetContext (_afda );_afda .PdfField =_bdbg ;_afda .T =_aa .MakeString (name );if opt .MaxLen > 0{_afda .MaxLen =_aa .MakeInteger (int64 (opt .MaxLen ));};if len (opt .Value )> 0{_afda .V =_aa .MakeString (opt .Value );};_bcf :=_ce .NewPdfAnnotationWidget ();_bcf .Rect =_aa .MakeArrayFromFloats (rect );_bcf .P =page .ToPdfObject ();_bcf .F =_aa .MakeInteger (4);_bcf .Parent =_afda .ToPdfObject ();_afda .Annotations =append (_afda .Annotations ,_bcf );return _afda ,nil ;};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +00:00
|
|
|
// CreateCircleAnnotation creates a circle/ellipse annotation object with appearance stream that can be added to
|
|
|
|
// page PDF annotations.
|
|
|
|
func CreateCircleAnnotation (circDef CircleAnnotationDef )(*_ce .PdfAnnotation ,error ){_fd :=_ce .NewPdfAnnotationCircle ();if circDef .BorderEnabled {_ad ,_fdf ,_ga :=circDef .BorderColor .R (),circDef .BorderColor .G (),circDef .BorderColor .B ();_fd .C =_aa .MakeArrayFromFloats ([]float64 {_ad ,_fdf ,_ga });_gd :=_ce .NewBorderStyle ();_gd .SetBorderWidth (circDef .BorderWidth );_fd .BS =_gd .ToPdfObject ();};if circDef .FillEnabled {_da ,_cd ,_fe :=circDef .FillColor .R (),circDef .FillColor .G (),circDef .FillColor .B ();_fd .IC =_aa .MakeArrayFromFloats ([]float64 {_da ,_cd ,_fe });}else {_fd .IC =_aa .MakeArrayFromIntegers ([]int {});};if circDef .Opacity < 1.0{_fd .CA =_aa .MakeFloat (circDef .Opacity );};_dab ,_cc ,_gc :=_fg (circDef );if _gc !=nil {return nil ,_gc ;};_fd .AP =_dab ;_fd .Rect =_aa .MakeArrayFromFloats ([]float64 {_cc .Llx ,_cc .Lly ,_cc .Urx ,_cc .Ury });return _fd .PdfAnnotation ,nil ;};func _ec (_bf *_ce .PdfAnnotationWidget ,_cg *_ce .PdfFieldText ,_cad *_ce .PdfPageResources ,_bfd AppearanceStyle )(*_aa .PdfObjectDictionary ,error ){_bed :=_ce .NewPdfPageResources ();_gde ,_abg :=_aa .GetArray (_bf .Rect );if !_abg {return nil ,_dd .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_bga ,_cbc :=_ce .NewPdfRectangle (*_gde );if _cbc !=nil {return nil ,_cbc ;};_bba ,_ag :=_bga .Width (),_bga .Height ();_ef ,_afb :=_bba ,_ag ;_gced ,_cgd :=_aa .GetDict (_bf .MK );if _cgd {_efa ,_ :=_aa .GetDict (_bf .BS );_cgc :=_bfd .applyAppearanceCharacteristics (_gced ,_efa ,nil );if _cgc !=nil {return nil ,_cgc ;};};_ff ,_cbc :=_b .NewContentStreamParser (_gdb (_cg .PdfField )).Parse ();if _cbc !=nil {return nil ,_cbc ;};_bc :=_b .NewContentCreator ();if _bfd .BorderSize > 0{_ebe (_bc ,_bfd ,_bba ,_ag );};if _bfd .DrawAlignmentReticle {_ea :=_bfd ;_ea .BorderSize =0.2;_fgdg (_bc ,_ea ,_bba ,_ag );};_bc .Add_BMC ("\u0054\u0078");_bc .Add_q ();_bba ,_ag =_bfd .applyRotation (_gced ,_bba ,_ag ,_bc );_bc .Add_BT ();_fdcg ,_adb ,_cbc :=_bfd .processDA (_cg .PdfField ,_ff ,_cad ,_bed ,_bc );if _cbc !=nil {return nil ,_cbc ;};_fga :=_fdcg .Font ;_bdc :=_fdcg .Size ;_eda :=_aa .MakeName (_fdcg .Name );_ced :=_bdc ==0;if _ced &&_adb {_bdc =_ag *_bfd .AutoFontSizeFraction ;};_gcg :=_fga .Encoder ();if _gcg ==nil {_d .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");_gcg =_a .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};_ece ,_cbc :=_fga .GetFontDescriptor ();if _cbc !=nil {_d .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 _ccb string ;if _gfg ,_bfc :=_aa .GetString (_cg .V );_bfc {_ccb =_gfg .Decoded ();};if len (_ccb )==0{return nil ,nil ;};_fec :=[]string {_ccb };_eee :=false ;if _cg .Flags ().Has (_ce .FieldFlagMultiline ){_eee =true ;_ccb =_g .Replace (_ccb ,"\u000d\u000a","\u000a",-1);_ccb =_g .Replace (_ccb ,"\u000d","\u000a",-1);_fec =_g .Split (_ccb ,"\u000a");};_gaf :=0.0;_aag :=0;if _gcg !=nil {_fdfb :=len (_fec );_gec :=0;for _gec < _fdfb {var _afd float64 ;_cef :=-1;_aagb :=0.0;for _bcc ,_de :=range _fec [_gec ]{if _de ==' '{_cef =_bcc ;_afd =_aagb ;};_acb ,_gff :=_fga .GetRuneMetrics (_de );if !_gff {_d .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",_de );continue ;};_aagb +=_acb .Wx ;if _eee &&!_ced &&_bdc *_aagb /1000.0> _bba &&_cef > 0{_edaf :=_fec [_gec ][_cef +1:];if _gec < len (_fec
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +00:00
|
|
|
// 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 };};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +00:00
|
|
|
// SignatureLine represents a line of information in the signature field appearance.
|
|
|
|
type SignatureLine struct{Desc string ;Text string ;};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +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 *_ce .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_ce .PdfColorDeviceRGB ;Opacity float64 ;};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
|
|
|
// AppearanceFontStyle defines font style characteristics for form fields,
|
|
|
|
// used in the filling/flattening process.
|
|
|
|
type AppearanceFontStyle struct{
|
|
|
|
|
|
|
|
// 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 ;
|
|
|
|
|
|
|
|
// 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 ;
|
|
|
|
|
|
|
|
// 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-08-31 21:12:07 +00:00
|
|
|
ForceReplace bool ;};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +00:00
|
|
|
// CreateRectangleAnnotation creates a rectangle annotation object that can be added to page PDF annotations.
|
|
|
|
func CreateRectangleAnnotation (rectDef RectangleAnnotationDef )(*_ce .PdfAnnotation ,error ){_aeee :=_ce .NewPdfAnnotationSquare ();if rectDef .BorderEnabled {_bbf ,_fedg ,_bgga :=rectDef .BorderColor .R (),rectDef .BorderColor .G (),rectDef .BorderColor .B ();_aeee .C =_aa .MakeArrayFromFloats ([]float64 {_bbf ,_fedg ,_bgga });_ccc :=_ce .NewBorderStyle ();_ccc .SetBorderWidth (rectDef .BorderWidth );_aeee .BS =_ccc .ToPdfObject ();};if rectDef .FillEnabled {_aaf ,_dabd ,_feff :=rectDef .FillColor .R (),rectDef .FillColor .G (),rectDef .FillColor .B ();_aeee .IC =_aa .MakeArrayFromFloats ([]float64 {_aaf ,_dabd ,_feff });}else {_aeee .IC =_aa .MakeArrayFromIntegers ([]int {});};if rectDef .Opacity < 1.0{_aeee .CA =_aa .MakeFloat (rectDef .Opacity );};_ddeg ,_abb ,_eggg :=_dgcg (rectDef );if _eggg !=nil {return nil ,_eggg ;};_aeee .AP =_ddeg ;_aeee .Rect =_aa .MakeArrayFromFloats ([]float64 {_abb .Llx ,_abb .Lly ,_abb .Urx ,_abb .Ury });return _aeee .PdfAnnotation ,nil ;};
|
2020-08-27 21:45:09 +00:00
|
|
|
|
2020-08-31 21:12:07 +00:00
|
|
|
// SetStyle applies appearance `style` to `fa`.
|
|
|
|
func (_gce *FieldAppearance )SetStyle (style AppearanceStyle ){_gce ._bd =&style };func _ffe (_baf *_ce .PdfField ,_eeea ,_fddb float64 ,_gcdb string ,_agf AppearanceStyle ,_dfd *_b .ContentStreamOperations ,_ead *_ce .PdfPageResources ,_fbbc *_aa .PdfObjectDictionary )(*_ce .XObjectForm ,error ){_adg :=_ce .NewPdfPageResources ();_faga ,_cff :=_eeea ,_fddb ;_fgc :=_b .NewContentCreator ();if _agf .BorderSize > 0{_ebe (_fgc ,_agf ,_eeea ,_fddb );};if _agf .DrawAlignmentReticle {_eceb :=_agf ;_eceb .BorderSize =0.2;_fgdg (_fgc ,_eceb ,_eeea ,_fddb );};_fgc .Add_BMC ("\u0054\u0078");_fgc .Add_q ();_fgc .Add_BT ();_eeea ,_fddb =_agf .applyRotation (_fbbc ,_eeea ,_fddb ,_fgc );_agde ,_ceeg ,_bfeb :=_agf .processDA (_baf ,_dfd ,_ead ,_adg ,_fgc );if _bfeb !=nil {return nil ,_bfeb ;};_acac :=_agde .Font ;_agfd :=_agde .Size ;_cga :=_aa .MakeName (_agde .Name );_agfe :=_agfd ==0;if _agfe &&_ceeg {_agfd =_fddb *_agf .AutoFontSizeFraction ;};_eead :=_acac .Encoder ();if _eead ==nil {_d .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");_eead =_a .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};if len (_gcdb )==0{return nil ,nil ;};_bged :=2.0;_acc :=0.0;if _eead !=nil {for _ ,_gdg :=range _gcdb {_cffa ,_cbcg :=_acac .GetRuneMetrics (_gdg );if !_cbcg {_d .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",_gdg );continue ;};_acc +=_cffa .Wx ;};_gcdb =string (_eead .Encode (_gcdb ));};if _agfd ==0||_agfe &&_acc > 0&&_bged +_acc *_agfd /1000.0> _eeea {_agfd =0.95*1000.0*(_eeea -_bged )/_acc ;};_gcda :=1.0*_agfd ;_bgf :=2.0;{_cdg :=_gcda ;if _agfe &&_bgf +_cdg > _fddb {_agfd =0.95*(_fddb -_bgf );_gcda =1.0*_agfd ;_cdg =_gcda ;};if _fddb > _cdg {_bgf =(_fddb -_cdg )/2.0;_bgf +=1.50;};};_fgc .Add_Tf (*_cga ,_agfd );_fgc .Add_Td (_bged ,_bgf );_fgc .Add_Tj (*_aa .MakeString (_gcdb ));_fgc .Add_ET ();_fgc .Add_Q ();_fgc .Add_EMC ();_gge :=_ce .NewXObjectForm ();_gge .Resources =_adg ;_gge .BBox =_aa .MakeArrayFromFloats ([]float64 {0,0,_faga ,_cff });_gge .SetContentStream (_fgc .Bytes (),_dfa ());return _gge ,nil ;};type CircleAnnotationDef struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;FillEnabled bool ;FillColor *_ce .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_ce .PdfColorDeviceRGB ;Opacity float64 ;};
|
|
|
|
|
|
|
|
// 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 *_ce .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineEndingStyle1 _be .LineEndingStyle ;LineEndingStyle2 _be .LineEndingStyle ;};func _dcee (_dfc *_ce .PdfAcroForm ,_gcea *_ce .PdfAnnotationWidget ,_fbc *_ce .PdfFieldChoice ,_aagg AppearanceStyle )(*_aa .PdfObjectDictionary ,error ){_adf ,_dcb :=_aa .GetArray (_gcea .Rect );if !_dcb {return nil ,_dd .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_dfg ,_bdcg :=_ce .NewPdfRectangle (*_adf );if _bdcg !=nil {return nil ,_bdcg ;};_afe ,_eag :=_dfg .Width (),_dfg .Height ();_d .Log .Debug ("\u0043\u0068\u006f\u0069\u0063\u0065\u002c\u0020\u0077\u0061\u0020\u0042S\u003a\u0020\u0025\u0076",_gcea .BS );_gfge ,_bdcg :=_b .NewContentStreamParser (_gdb (_fbc .PdfField )).Parse ();if _bdcg !=nil {return nil ,_bdcg ;};_fef ,_gegc :=_aa .GetDict (_gcea .MK );if _gegc {_baa ,_ :=_aa .GetDict (_gcea .BS );_fecc :=_aagg .applyAppearanceCharacteristics (_fef ,_baa ,nil );if _fecc !=nil {return nil ,_fecc ;};};_cae :=_aa .MakeDict ();for _ ,_bcea :=range _fbc .Opt .Elements (){if _gbe ,_bgc :=_aa .GetArray (_bcea );_bgc &&_gbe .Len ()==2{_bcea =_gbe .Get (1);};var _gcdg string ;if _efg ,_dgbc :=_aa .GetString (_bcea );_dgbc {_gcdg =_efg .Decoded ();}else if _efc ,_acd :=_aa .GetName (_bcea );_acd {_gcdg =_efc .String ();}else {_d .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",_bcea );return nil ,_dd .New ("\u006e\u006f\u0074\u0020\u0061\u0020\u006e\u0061\u006d\u0065\u002f\u0073t\u0072\u0069\u006e\u0067");};if len (_gcdg )> 0{_faef ,_degf :=_ffe (_fbc .PdfField ,_afe ,_eag ,_gcdg ,_aagg ,_gfge ,_dfc .DR ,_fef );if _degf !=nil {return nil ,_degf ;};_cae .Set (*_aa .MakeName (_gcdg ),_faef .ToPdfObject ());};};_eab :=_aa .MakeDict ();_eab .Set ("\u004e",_cae );return _eab ,nil ;};func _fg (_gf CircleAnnotationDef )(*_aa .PdfObjectDictionary ,*_ce .PdfRectangle ,error ){_ae :=_ce .NewXObjectForm ();_ae .Resources =_ce .NewPdfPageResources ();_fc :="";if _gf .Opacity < 1.0{_bef :=_aa .MakeDict ();_bef .Set ("\u0063\u0061",_aa .MakeFloat (_gf .Opacity ));_bef .Set ("\u0043\u0041",_aa .MakeFloat (_gf .Opacity ));_gg :=_ae .Resources .AddExtGState ("\u0067\u0073\u0031",_bef );if _gg !=nil {_d .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 ,_gg ;};_fc ="\u0067\u0073\u0031";};_aab ,_cb ,_ceg ,_adc :=_ba (_gf ,_fc );if _adc !=nil {return nil ,nil ,_adc ;};_adc =_ae .SetContentStream (_aab ,nil );if _adc !=nil {return nil ,nil ,_adc ;};_ae .BBox =_cb .ToPdfObject ();_dg :=_aa .MakeDict ();_dg .Set ("\u004e",_ae .ToPdfObject ());return _dg ,_ceg ,nil ;};func _gcga (_db *_ce .PdfAnnotationWidget ,_deg *_ce .PdfFieldText ,_aaa *_ce .PdfPageResources ,_dce AppearanceStyle )(*_aa .PdfObjectDictionary ,error ){_edf :=_ce .NewPdfPageResources ();_eac ,_acf :=_aa .GetArray (_db .Rect );if !_acf {return nil ,_dd .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_fff ,_cba :=_ce .NewPdfRectangle (*_eac );if _cba !=nil {return nil ,_cba ;};_acbg ,_ggf :=_fff .Width (),_fff .Height ();_fag ,_aaea :=_acbg ,_ggf ;_daf ,_bbd :=_aa .GetDict (_db .MK );if _bbd {_ded ,_ :=_aa .GetDict (_db .BS );_dga :=_dce .applyAppearanceCharacteristics (_daf ,_ded ,nil );if _dga !=nil {return nil ,_dga ;};};_gcd ,_bbd :=_aa .GetIntVal (_deg .MaxLen );if !_bbd {return nil ,_dd .New ("\u006d\u0061\u0078\u006c\u0065\u006e\u0020\u006e\u006ft\u0020\u0073\u0065\u0074");};if _gcd <=0{return nil ,_dd .New ("\u006d\u0061\u0078\u004c\u0065\u006e\u0020\u0069\u006ev\u0061\u006c\u0069\u0064");};_dbe :=float64 (_acbg )/float64 (_gcd );_feae ,_cba :=_b .NewContentStreamParser (_gdb (_deg .PdfField )).Parse ();if _cba !=nil {return nil ,_cba ;};_dcg :=_b .NewContentCreator ();if _dce .BorderSize > 0{_ebe (_dcg ,_dce ,_ac
|
|
|
|
|
|
|
|
// CheckboxFieldOptions defines optional parameters for a checkbox field a form.
|
|
|
|
type CheckboxFieldOptions struct{Checked bool ;};func _dgcg (_fdbf RectangleAnnotationDef )(*_aa .PdfObjectDictionary ,*_ce .PdfRectangle ,error ){_bfcd :=_ce .NewXObjectForm ();_bfcd .Resources =_ce .NewPdfPageResources ();_fcb :="";if _fdbf .Opacity < 1.0{_fdff :=_aa .MakeDict ();_fdff .Set ("\u0063\u0061",_aa .MakeFloat (_fdbf .Opacity ));_fdff .Set ("\u0043\u0041",_aa .MakeFloat (_fdbf .Opacity ));_geeb :=_bfcd .Resources .AddExtGState ("\u0067\u0073\u0031",_fdff );if _geeb !=nil {_d .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 ,_geeb ;};_fcb ="\u0067\u0073\u0031";};_afdb ,_efd ,_cbec ,_dbb :=_eebd (_fdbf ,_fcb );if _dbb !=nil {return nil ,nil ,_dbb ;};_dbb =_bfcd .SetContentStream (_afdb ,nil );if _dbb !=nil {return nil ,nil ,_dbb ;};_bfcd .BBox =_efd .ToPdfObject ();_bgcd :=_aa .MakeDict ();_bgcd .Set ("\u004e",_bfcd .ToPdfObject ());return _bgcd ,_cbec ,nil ;};
|
|
|
|
|
|
|
|
// ComboboxFieldOptions defines optional parameters for a combobox form field.
|
|
|
|
type ComboboxFieldOptions struct{
|
|
|
|
|
|
|
|
// Choices is the list of string values that can be selected.
|
|
|
|
Choices []string ;};
|
|
|
|
|
|
|
|
// CreateLineAnnotation creates a line annotation object that can be added to page PDF annotations.
|
|
|
|
func CreateLineAnnotation (lineDef LineAnnotationDef )(*_ce .PdfAnnotation ,error ){_ebba :=_ce .NewPdfAnnotationLine ();_ebba .L =_aa .MakeArrayFromFloats ([]float64 {lineDef .X1 ,lineDef .Y1 ,lineDef .X2 ,lineDef .Y2 });_cedc :=_aa .MakeName ("\u004e\u006f\u006e\u0065");if lineDef .LineEndingStyle1 ==_be .LineEndingStyleArrow {_cedc =_aa .MakeName ("C\u006c\u006f\u0073\u0065\u0064\u0041\u0072\u0072\u006f\u0077");};_afge :=_aa .MakeName ("\u004e\u006f\u006e\u0065");if lineDef .LineEndingStyle2 ==_be .LineEndingStyleArrow {_afge =_aa .MakeName ("C\u006c\u006f\u0073\u0065\u0064\u0041\u0072\u0072\u006f\u0077");};_ebba .LE =_aa .MakeArray (_cedc ,_afge );if lineDef .Opacity < 1.0{_ebba .CA =_aa .MakeFloat (lineDef .Opacity );};_ccge ,_dfcg ,_bfce :=lineDef .LineColor .R (),lineDef .LineColor .G (),lineDef .LineColor .B ();_ebba .IC =_aa .MakeArrayFromFloats ([]float64 {_ccge ,_dfcg ,_bfce });_ebba .C =_aa .MakeArrayFromFloats ([]float64 {_ccge ,_dfcg ,_bfce });_cgbe :=_ce .NewBorderStyle ();_cgbe .SetBorderWidth (lineDef .LineWidth );_ebba .BS =_cgbe .ToPdfObject ();_efcd ,_gege ,_cecc :=_dgc (lineDef );if _cecc !=nil {return nil ,_cecc ;};_ebba .AP =_efcd ;_ebba .Rect =_aa .MakeArrayFromFloats ([]float64 {_gege .Llx ,_gege .Lly ,_gege .Urx ,_gege .Ury });return _ebba .PdfAnnotation ,nil ;};func _adbc (_eddc LineAnnotationDef ,_bfbe string )([]byte ,*_ce .PdfRectangle ,*_ce .PdfRectangle ,error ){_dcef :=_be .Line {X1 :0,Y1 :0,X2 :_eddc .X2 -_eddc .X1 ,Y2 :_eddc .Y2 -_eddc .Y1 ,LineColor :_eddc .LineColor ,Opacity :_eddc .Opacity ,LineWidth :_eddc .LineWidth ,LineEndingStyle1 :_eddc .LineEndingStyle1 ,LineEndingStyle2 :_eddc .LineEndingStyle2 };_fcgg ,_cdfc ,_edg :=_dcef .Draw (_bfbe );if _edg !=nil {return nil ,nil ,nil ,_edg ;};_fgdd :=&_ce .PdfRectangle {};_fgdd .Llx =_eddc .X1 +_cdfc .Llx ;_fgdd .Lly =_eddc .Y1 +_cdfc .Lly ;_fgdd .Urx =_eddc .X1 +_cdfc .Urx ;_fgdd .Ury =_eddc .Y1 +_cdfc .Ury ;return _fcgg ,_cdfc ,_fgdd ,nil ;};
|
|
|
|
|
|
|
|
// GenerateAppearanceDict generates an appearance dictionary for widget annotation `wa` for the `field` in `form`.
|
|
|
|
// Implements interface model.FieldAppearanceGenerator.
|
|
|
|
func (_fea FieldAppearance )GenerateAppearanceDict (form *_ce .PdfAcroForm ,field *_ce .PdfField ,wa *_ce .PdfAnnotationWidget )(*_aa .PdfObjectDictionary ,error ){_d .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 );_ ,_gfe :=field .GetContext ().(*_ce .PdfFieldText );_fdb ,_ab :=_aa .GetDict (wa .AP );if _ab &&_fea .OnlyIfMissing &&(!_gfe ||!_fea .RegenerateTextFields ){_d .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 _fdb ,nil ;};if form .DR ==nil {form .DR =_ce .NewPdfPageResources ();};switch _fce :=field .GetContext ().(type ){case *_ce .PdfFieldText :_gb :=_fce ;switch {case _gb .Flags ().Has (_ce .FieldFlagPassword ):return nil ,nil ;case _gb .Flags ().Has (_ce .FieldFlagFileSelect ):return nil ,nil ;case _gb .Flags ().Has (_ce .FieldFlagComb ):if _gb .MaxLen !=nil {_ee ,_aabb :=_gcga (wa ,_gb ,form .DR ,_fea .Style ());if _aabb !=nil {return nil ,_aabb ;};return _ee ,nil ;};};_aae ,_eg :=_ec (wa ,_gb ,form .DR ,_fea .Style ());if _eg !=nil {return nil ,_eg ;};return _aae ,nil ;case *_ce .PdfFieldButton :_cca :=_fce ;if _cca .IsCheckbox (){_aed ,_fdc :=_efef (wa ,_cca ,form .DR ,_fea .Style ());if _fdc !=nil {return nil ,_fdc ;};return _aed ,nil ;};_d .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",_cca .GetType ());case *_ce .PdfFieldChoice :_bdg :=_fce ;switch {case _bdg .Flags ().Has (_ce .FieldFlagCombo ):_cege ,_df :=_dcee (form ,wa ,_bdg ,_fea .Style ());if _df !=nil {return nil ,_df ;};return _cege ,nil ;default:_d .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",_bdg .Flags ().String ());};default:_d .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",_fce );};return nil ,nil ;};func _dgc (_bee LineAnnotationDef )(*_aa .PdfObjectDictionary ,*_ce .PdfRectangle ,error ){_fefb :=_ce .NewXObjectForm ();_fefb .Resources =_ce .NewPdfPageResources ();_gbf :="";if _bee .Opacity < 1.0{_babb :=_aa .MakeDict ();_babb .Set ("\u0063\u0061",_aa .MakeFloat (_bee .Opacity ));_edfff :=_fefb .Resources .AddExtGState ("\u0067\u0073\u0031",_babb );if _edfff !=nil {_d .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 ,_edfff ;};_gbf ="\u0067\u0073\u0031";};_abe ,_fcg ,_bgac ,_efbe :=_adbc (_bee ,_gbf );if _efbe !=nil {return nil ,nil ,_efbe ;};_efbe =_fefb .SetContentStream (_abe ,nil );if _efbe !=nil {return nil ,nil ,_efbe ;};_fefb .BBox =_fcg .ToPdfObject ();_abfa :=_aa .MakeDict ();_abfa .Set ("\u004e",_fefb .ToPdfObject ());return _abfa ,_bgac ,nil ;};func _ebe (_ceaa *_b .ContentCreator ,_ebd AppearanceStyle ,_dgad ,_fcf float64 ){_ceaa .Add_q ().Add_re (0,0,_dgad ,_fcf ).Add_w (_ebd .BorderSize ).SetStrokingColor (_ebd .BorderColor ).SetNonStrokingColor (_ebd .FillColor ).Add_B ().Add_Q ();};
|
|
|
|
|
|
|
|
// 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 *_ce .PdfPage ,name string ,rect []float64 ,opt ComboboxFieldOptions )(*_ce .PdfFieldChoice ,error ){if page ==nil {return nil ,_dd .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_dd .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 ,_dd .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_afcb :=_ce .NewPdfField ();_afdcb :=&_ce .PdfFieldChoice {};_afcb .SetContext (_afdcb );_afdcb .PdfField =_afcb ;_afdcb .T =_aa .MakeString (name );_afdcb .Opt =_aa .MakeArray ();for _ ,_bdef :=range opt .Choices {_afdcb .Opt .Append (_aa .MakeString (_bdef ));};_afdcb .SetFlag (_ce .FieldFlagCombo );_eada :=_ce .NewPdfAnnotationWidget ();_eada .Rect =_aa .MakeArrayFromFloats (rect );_eada .P =page .ToPdfObject ();_eada .F =_aa .MakeInteger (4);_eada .Parent =_afdcb .ToPdfObject ();_afdcb .Annotations =append (_afdcb .Annotations ,_eada );return _afdcb ,nil ;};
|
|
|
|
|
|
|
|
// TextFieldOptions defines optional parameter for a text field in a form.
|
|
|
|
type TextFieldOptions struct{MaxLen int ;Value string ;};
|