Skip to content

Commit a3cfd4e

Browse files
committed
Cover several sign methods.
More previsely, getWidgetPageNumber and tempFile processing DEVSIX-6110
1 parent 60ef8fa commit a3cfd4e

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

sign/src/test/java/com/itextpdf/signatures/sign/SimpleSigningTest.java

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.kernel.geom.Rectangle;
4646
import com.itextpdf.kernel.pdf.PdfReader;
47+
import com.itextpdf.kernel.pdf.PdfWriter;
4748
import com.itextpdf.kernel.pdf.StampingProperties;
4849
import com.itextpdf.kernel.utils.CompareTool;
4950
import com.itextpdf.signatures.BouncyCastleDigest;
@@ -121,6 +122,51 @@ public void pdfDocWithParagraphSigningTest() throws GeneralSecurityException, IO
121122
Assert.assertNull(SignaturesCompareTool.compareSignatures(outPdf, cmpPdf));
122123
}
123124

125+
@Test
126+
public void signWithoutPKeyTest() throws GeneralSecurityException, IOException, InterruptedException {
127+
String srcFile = SOURCE_FOLDER + "emptySignatureWithoutPKey.pdf";
128+
String cmpPdf = SOURCE_FOLDER + "cmp_signedWithoutPKey.pdf";
129+
String outPdf = DESTINATION_FOLDER + "signedWithoutPKey.pdf";
130+
131+
Rectangle rect = new Rectangle(36, 648, 200, 100);
132+
133+
String fieldName = "Signature1";
134+
sign(srcFile, fieldName, outPdf, chain, pk, DigestAlgorithms.SHA256, PdfSigner.CryptoStandard.CADES, "Test 1",
135+
"TestCity", rect, false, false, PdfSigner.NOT_CERTIFIED, 12f);
136+
137+
Assert.assertNull(new CompareTool().compareVisually(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_",
138+
getTestMap(rect)));
139+
140+
Assert.assertNull(SignaturesCompareTool.compareSignatures(outPdf, cmpPdf));
141+
}
142+
143+
@Test
144+
public void signWithTempFileTest() throws GeneralSecurityException, IOException, InterruptedException {
145+
String srcFile = SOURCE_FOLDER + "simpleDocument.pdf";
146+
String cmpPdf = SOURCE_FOLDER + "cmp_signedWithTempFile.pdf";
147+
String outPdf = DESTINATION_FOLDER + "signedWithTempFile.pdf";
148+
149+
String tempFileName = "tempFile";
150+
PdfSigner signer = new PdfSigner(
151+
new PdfReader(srcFile),
152+
new PdfWriter(outPdf), DESTINATION_FOLDER + tempFileName, new StampingProperties());
153+
Rectangle rect = new Rectangle(36, 648, 200, 100);
154+
155+
signer.setCertificationLevel(PdfSigner.NOT_CERTIFIED);
156+
signer.setFieldName("Signature1");
157+
158+
// Creating the appearance
159+
createAppearance(signer, "Test 1", "TestCity", false, rect, 12f);
160+
161+
// Creating the signature
162+
IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, BouncyCastleProvider.PROVIDER_NAME);
163+
signer.signDetached(new BouncyCastleDigest(), pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
164+
165+
Assert.assertNull(new CompareTool().compareVisually(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_",
166+
getTestMap(rect)));
167+
Assert.assertNull(SignaturesCompareTool.compareSignatures(outPdf, cmpPdf));
168+
}
169+
124170
protected void sign(String src, String name, String dest,
125171
Certificate[] chain, PrivateKey pk,
126172
String digestAlgorithm, PdfSigner.CryptoStandard subfilter,
@@ -138,17 +184,7 @@ protected void sign(String src, String name, String dest,
138184
signer.setCertificationLevel(certificationLevel);
139185

140186
// Creating the appearance
141-
PdfSignatureAppearance appearance = signer.getSignatureAppearance()
142-
.setReason(reason)
143-
.setLocation(location)
144-
.setReuseAppearance(setReuseAppearance);
145-
146-
if (rectangleForNewField != null) {
147-
appearance.setPageRect(rectangleForNewField);
148-
}
149-
if (fontSize != null) {
150-
appearance.setLayer2FontSize((float) fontSize);
151-
}
187+
createAppearance(signer, reason, location, setReuseAppearance, rectangleForNewField, fontSize);
152188

153189
signer.setFieldName(name);
154190
// Creating the signature
@@ -161,4 +197,19 @@ private static Map<Integer, List<Rectangle>> getTestMap(Rectangle ignoredArea) {
161197
result.put(1, Arrays.asList(ignoredArea));
162198
return result;
163199
}
200+
201+
private static void createAppearance(PdfSigner signer, String reason, String location, boolean setReuseAppearance,
202+
Rectangle rectangleForNewField, Float fontSize) {
203+
PdfSignatureAppearance appearance = signer.getSignatureAppearance()
204+
.setReason(reason)
205+
.setLocation(location)
206+
.setReuseAppearance(setReuseAppearance);
207+
208+
if (rectangleForNewField != null) {
209+
appearance.setPageRect(rectangleForNewField);
210+
}
211+
if (fontSize != null) {
212+
appearance.setLayer2FontSize((float) fontSize);
213+
}
214+
}
164215
}

0 commit comments

Comments
 (0)