1- using PdfSharpCore . Pdf ;
1+ using PdfSharpCore . Drawing ;
2+ using PdfSharpCore . Pdf ;
23using PdfSharpCore . Pdf . AcroForms ;
34using PdfSharpCore . Pdf . Advanced ;
45using PdfSharpCore . Pdf . Annotations ;
56using PdfSharpCore . Pdf . Signatures ;
67using System ;
78using System . Collections . Generic ;
89using System . IO ;
10+ using System . Net . Security ;
911using System . Security . Cryptography . Pkcs ;
1012using System . Security . Cryptography . X509Certificates ;
1113using System . Text ;
14+ using System . Xml . Linq ;
1215using static PdfSharpCore . Pdf . AcroForms . PdfAcroField ;
1316
1417namespace PdfSharpCore . Pdf . Signatures
@@ -38,24 +41,25 @@ public void AttachToDocument(PdfDocument documentToSign)
3841 this . Document . BeforeSave += AddSignatureComponents ;
3942 this . Document . AfterSave += ComputeSignatureAndRange ;
4043
41- if ( ! maximumSignatureLength . HasValue )
44+ if ( signer != null && ! maximumSignatureLength . HasValue )
4245 {
4346 maximumSignatureLength = signer . GetSignedCms ( new MemoryStream ( new byte [ ] { 0 } ) ) . Length ;
4447 SignatureSizeComputed ( this , new IntEventArgs ( ) { Value = maximumSignatureLength . Value } ) ;
4548 }
4649 }
4750
48- public PdfSignatureHandler ( ISigner signer , int ? signatureMaximumLength , PdfSignatureOptions options )
51+ public PdfSignatureHandler ( ISigner signer , PdfSignatureOptions options , int ? signatureMaximumLength = null )
4952 {
5053 this . signer = signer ;
51-
5254
5355 this . maximumSignatureLength = signatureMaximumLength ;
5456 this . Options = options ;
5557 }
5658
5759 private void ComputeSignatureAndRange ( object sender , PdfDocumentEventArgs e )
5860 {
61+ if ( signer == null ) return ;
62+
5963 var writer = e . Writer ;
6064 writer . Stream . Position = rangeTracker . Start ;
6165 var rangeArray = new PdfArray ( new PdfInteger ( 0 ) ,
@@ -94,7 +98,7 @@ private RangedStream GetRangeToSign(Stream stream)
9498 new RangedStream . Range ( contentsTraker . End , stream . Length - contentsTraker . End )
9599 } ) ;
96100
97- }
101+ }
98102
99103 private void AddSignatureComponents ( object sender , EventArgs e )
100104 {
@@ -103,36 +107,62 @@ private void AddSignatureComponents(object sender, EventArgs e)
103107 if ( catalog . AcroForm == null )
104108 catalog . AcroForm = new PdfAcroForm ( Document ) ;
105109
106- catalog . AcroForm . Elements . Add ( PdfAcroForm . Keys . SigFlags , new PdfInteger ( 3 ) ) ;
110+ if ( signer != null )
111+ {
112+ if ( catalog . AcroForm . Elements . ContainsKey ( PdfAcroForm . Keys . SigFlags ) )
113+ {
114+ catalog . AcroForm . Elements . Remove ( PdfAcroForm . Keys . SigFlags ) ;
115+ }
116+ catalog . AcroForm . Elements . Add ( PdfAcroForm . Keys . SigFlags , new PdfInteger ( 3 ) ) ;
117+ }
107118
108- var signature = new PdfSignatureField ( Document ) ;
119+ PdfSignatureField signature = Options . FieldName == null ? null : catalog . AcroForm . Fields [ Options . FieldName ] as PdfSignatureField ;
120+ bool isNew = signature == null ;
109121
110- var paddedContents = new PdfString ( "" , PdfStringFlags . HexLiteral , maximumSignatureLength . Value ) ;
111- var paddedRange = new PdfArray ( Document , byteRangePaddingLength , new PdfInteger ( 0 ) , new PdfInteger ( 0 ) , new PdfInteger ( 0 ) , new PdfInteger ( 0 ) ) ;
122+ if ( isNew )
123+ {
124+ signature = new PdfSignatureField ( Document ) ;
125+ signature . Elements [ Keys . T ] = new PdfString ( Options . FieldName ?? "Signature1" ) ;
126+ }
112127
113- signature . Contents = paddedContents ;
114- signature . ByteRange = paddedRange ;
115- signature . Reason = Options . Reason ;
116- signature . Location = Options . Location ;
117- signature . Rectangle = new PdfRectangle ( Options . Rectangle ) ;
128+ if ( isNew || ( Options . Rectangle != XRect . Empty && Options . Rectangle != default ) )
129+ {
130+ signature . Rectangle = new PdfRectangle ( Options . Rectangle ) ;
131+ }
132+ if ( signer != null )
133+ {
134+ var paddedContents = new PdfString ( "" , PdfStringFlags . HexLiteral , maximumSignatureLength . Value ) ;
135+ var paddedRange = new PdfArray ( Document , byteRangePaddingLength , new PdfInteger ( 0 ) , new PdfInteger ( 0 ) , new PdfInteger ( 0 ) , new PdfInteger ( 0 ) ) ;
136+
137+ this . contentsTraker = new PositionTracker ( paddedContents ) ;
138+ this . rangeTracker = new PositionTracker ( paddedRange ) ;
139+
140+ signature . Contents = paddedContents ;
141+ signature . ByteRange = paddedRange ;
142+ signature . Reason = Options . Reason ;
143+ signature . Location = Options . Location ;
144+ }
145+ else
146+ {
147+ signature . Elements . Remove ( Keys . V ) ;
148+ }
118149 signature . AppearanceHandler = Options . AppearanceHandler ?? new DefaultAppearanceHandler ( )
119150 {
120151 Location = Options . Location ,
121152 Reason = Options . Reason ,
122- Signer = signer . GetName ( )
153+ Signer = signer ? . GetName ( )
123154 } ;
124- signature . PrepareForSave ( ) ;
155+ signature . PrepareForSave ( ) ;
125156
126- this . contentsTraker = new PositionTracker ( paddedContents ) ;
127- this . rangeTracker = new PositionTracker ( paddedRange ) ;
128-
129- if ( ! Document . Pages [ 0 ] . Elements . ContainsKey ( PdfPage . Keys . Annots ) )
130- Document . Pages [ 0 ] . Elements . Add ( PdfPage . Keys . Annots , new PdfArray ( Document ) ) ;
157+ if ( isNew )
158+ {
159+ if ( ! Document . Pages [ 0 ] . Elements . ContainsKey ( PdfPage . Keys . Annots ) )
160+ Document . Pages [ 0 ] . Elements . Add ( PdfPage . Keys . Annots , new PdfArray ( Document ) ) ;
131161
132- ( Document . Pages [ 0 ] . Elements [ PdfPage . Keys . Annots ] as PdfArray ) . Elements . Add ( signature ) ;
133-
134- catalog . AcroForm . Fields . Elements . Add ( signature ) ;
162+ ( Document . Pages [ 0 ] . Elements [ PdfPage . Keys . Annots ] as PdfArray ) . Elements . Add ( signature ) ;
135163
164+ catalog . AcroForm . Fields . Elements . Add ( signature ) ;
165+ }
136166 }
137167 }
138168}
0 commit comments