@@ -79,6 +79,10 @@ This file is part of the iText (R) project.
79
79
import java .security .UnrecoverableKeyException ;
80
80
import java .security .cert .Certificate ;
81
81
import java .security .cert .CertificateException ;
82
+ import java .util .Arrays ;
83
+ import java .util .HashMap ;
84
+ import java .util .List ;
85
+ import java .util .Map ;
82
86
83
87
@ Category (IntegrationTest .class )
84
88
public class PdfSignatureAppearanceTest extends ExtendedITextTest {
@@ -169,6 +173,61 @@ public void textAutoscaleTest06() throws GeneralSecurityException, IOException,
169
173
assertAppearanceFontSize (dest , 6.26f );
170
174
}
171
175
176
+ @ Test
177
+ public void testSigningInAppendModeWithHybridDocument () throws IOException , GeneralSecurityException , InterruptedException {
178
+ String src = sourceFolder + "hybrid.pdf" ;
179
+ String dest = destinationFolder + "signed_hybrid.pdf" ;
180
+ String cmp = sourceFolder + "cmp_signed_hybrid.pdf" ;
181
+
182
+ PdfSigner signer = new PdfSigner (new PdfReader (src ), new FileOutputStream (dest ), new StampingProperties ().useAppendMode ());
183
+
184
+ PdfSignatureAppearance appearance = signer .getSignatureAppearance ();
185
+
186
+ appearance .setLayer2FontSize (13.8f )
187
+ .setPageRect (new Rectangle (36 , 748 , 200 , 100 ))
188
+ .setPageNumber (1 )
189
+ .setReason ("Test" )
190
+ .setLocation ("Nagpur" );
191
+
192
+ signer .setFieldName ("Sign1" );
193
+
194
+ signer .setCertificationLevel (PdfSigner .NOT_CERTIFIED );
195
+
196
+ IExternalSignature pks = new PrivateKeySignature (pk , DigestAlgorithms .SHA256 , BouncyCastleProvider .PROVIDER_NAME );
197
+ signer .signDetached (new BouncyCastleDigest (), pks , chain , null , null , null , 0 , PdfSigner .CryptoStandard .CADES );
198
+
199
+ // Make sure iText can open the document
200
+ new PdfDocument (new PdfReader (dest )).close ();
201
+
202
+ // Assert that the document can be rendered correctly
203
+ Assert .assertNull (new CompareTool ().compareVisually (dest , cmp , destinationFolder , "diff_" ,
204
+ getIgnoredAreaTestMap (new Rectangle (36 , 748 , 200 , 100 ))));
205
+ }
206
+
207
+ @ Test
208
+ public void fontColorTest01 () throws GeneralSecurityException , IOException , InterruptedException {
209
+ String fileName = "fontColorTest01.pdf" ;
210
+ String dest = destinationFolder + fileName ;
211
+
212
+ Rectangle rect = new Rectangle (36 , 648 , 100 , 50 );
213
+ String src = sourceFolder + "simpleDocument.pdf" ;
214
+
215
+ PdfSigner signer = new PdfSigner (new PdfReader (src ), new FileOutputStream (dest ), new StampingProperties ());
216
+ // Creating the appearance
217
+ signer .getSignatureAppearance ()
218
+ .setLayer2FontColor (ColorConstants .RED )
219
+ .setLayer2Text ("Verified and signed by me." )
220
+ .setPageRect (rect );
221
+
222
+ signer .setFieldName ("Signature1" );
223
+ // Creating the signature
224
+ IExternalSignature pks = new PrivateKeySignature (pk , DigestAlgorithms .SHA256 , BouncyCastleProvider .PROVIDER_NAME );
225
+ signer .signDetached (new BouncyCastleDigest (), pks , chain , null , null , null , 0 , PdfSigner .CryptoStandard .CADES );
226
+
227
+ Assert .assertNull (new CompareTool ().compareVisually (dest , sourceFolder + "cmp_" + fileName , destinationFolder ,
228
+ "diff_" ));
229
+ }
230
+
172
231
private void testSignatureAppearanceAutoscale (String dest , Rectangle rect , PdfSignatureAppearance .RenderingMode renderingMode ) throws IOException , GeneralSecurityException {
173
232
String src = sourceFolder + "simpleDocument.pdf" ;
174
233
@@ -206,28 +265,10 @@ private static void assertAppearanceFontSize(String filename, float expectedFont
206
265
Assert .assertTrue (MessageFormatUtil .format ("Font size: exptected {0}, found {1}" , expectedFontSize , fontSize ), Math .abs (foundFontSize - expectedFontSize ) < 0.1 * expectedFontSize );
207
266
}
208
267
209
- @ Test
210
- public void fontColorTest01 () throws GeneralSecurityException , IOException , InterruptedException {
211
- String fileName = "fontColorTest01.pdf" ;
212
- String dest = destinationFolder + fileName ;
213
-
214
- Rectangle rect = new Rectangle (36 , 648 , 100 , 50 );
215
- String src = sourceFolder + "simpleDocument.pdf" ;
216
-
217
- PdfSigner signer = new PdfSigner (new PdfReader (src ), new FileOutputStream (dest ), new StampingProperties ());
218
- // Creating the appearance
219
- signer .getSignatureAppearance ()
220
- .setLayer2FontColor (ColorConstants .RED )
221
- .setLayer2Text ("Verified and signed by me." )
222
- .setPageRect (rect );
223
-
224
- signer .setFieldName ("Signature1" );
225
- // Creating the signature
226
- IExternalSignature pks = new PrivateKeySignature (pk , DigestAlgorithms .SHA256 , BouncyCastleProvider .PROVIDER_NAME );
227
- signer .signDetached (new BouncyCastleDigest (), pks , chain , null , null , null , 0 , PdfSigner .CryptoStandard .CADES );
228
-
229
- Assert .assertNull (new CompareTool ().compareVisually (dest , sourceFolder + "cmp_" + fileName , destinationFolder ,
230
- "diff_" ));
268
+ private static Map <Integer , List <Rectangle >> getIgnoredAreaTestMap (Rectangle ignoredArea ) {
269
+ Map <Integer , List <Rectangle >> result = new HashMap <Integer , List <Rectangle >>();
270
+ result .put (1 , Arrays .asList (ignoredArea ));
271
+ return result ;
231
272
}
232
273
233
274
}
0 commit comments