Search in sources :

Example 1 with CreateVisibleSignature

use of org.apache.pdfbox.examples.signature.CreateVisibleSignature in project pdfbox by apache.

the class TestCreateSignature method testCreateVisibleSignature.

/**
 * Test creating visual signature.
 *
 * @throws IOException
 * @throws CMSException
 * @throws OperatorCreationException
 * @throws GeneralSecurityException
 */
@Test
public void testCreateVisibleSignature() throws IOException, CMSException, OperatorCreationException, GeneralSecurityException {
    // load the keystore
    KeyStore keystore = KeyStore.getInstance("PKCS12");
    keystore.load(new FileInputStream(keystorePath), password.toCharArray());
    // sign PDF
    String inPath = inDir + "sign_me.pdf";
    File destFile;
    try (FileInputStream fis = new FileInputStream(jpegPath)) {
        CreateVisibleSignature signing = new CreateVisibleSignature(keystore, password.toCharArray());
        signing.setVisibleSignDesigner(inPath, 0, 0, -50, fis, 1);
        signing.setVisibleSignatureProperties("name", "location", "Security", 0, 1, true);
        signing.setExternalSigning(externallySign);
        destFile = new File(outDir + getOutputFileName("signed{0}_visible.pdf"));
        signing.signPDF(new File(inPath), destFile, null);
    }
    checkSignature(destFile);
}
Also used : COSString(org.apache.pdfbox.cos.COSString) CreateVisibleSignature(org.apache.pdfbox.examples.signature.CreateVisibleSignature) KeyStore(java.security.KeyStore) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with CreateVisibleSignature

use of org.apache.pdfbox.examples.signature.CreateVisibleSignature in project pdfbox by apache.

the class TestCreateSignature method testPDFBox3978.

/**
 * Test when visually signing externally on an existing signature field on a file which has
 * been signed before.
 *
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws KeyStoreException
 * @throws CertificateException
 * @throws UnrecoverableKeyException
 * @throws CMSException
 * @throws OperatorCreationException
 * @throws GeneralSecurityException
 */
@Test
public void testPDFBox3978() throws IOException, NoSuchAlgorithmException, KeyStoreException, CertificateException, UnrecoverableKeyException, CMSException, OperatorCreationException, GeneralSecurityException {
    String filename = outDir + "EmptySignatureForm.pdf";
    String filenameSigned1 = outDir + "EmptySignatureForm-signed1.pdf";
    String filenameSigned2 = outDir + "EmptySignatureForm-signed2.pdf";
    if (!externallySign) {
        return;
    }
    // load the keystore
    KeyStore keystore = KeyStore.getInstance("PKCS12");
    keystore.load(new FileInputStream(keystorePath), password.toCharArray());
    // create file with empty signature
    CreateEmptySignatureForm.main(new String[] { filename });
    // sign PDF
    CreateSignature signing1 = new CreateSignature(keystore, password.toCharArray());
    signing1.setExternalSigning(false);
    signing1.signDetached(new File(filename), new File(filenameSigned1));
    checkSignature(new File(filenameSigned1));
    try (PDDocument doc1 = PDDocument.load(new File(filenameSigned1))) {
        List<PDSignature> signatureDictionaries = doc1.getSignatureDictionaries();
        Assert.assertEquals(1, signatureDictionaries.size());
    }
    // do visual signing in the field
    try (FileInputStream fis = new FileInputStream(jpegPath)) {
        CreateVisibleSignature signing2 = new CreateVisibleSignature(keystore, password.toCharArray());
        signing2.setVisibleSignDesigner(filenameSigned1, 0, 0, -50, fis, 1);
        signing2.setVisibleSignatureProperties("name", "location", "Security", 0, 1, true);
        signing2.setExternalSigning(externallySign);
        signing2.signPDF(new File(filenameSigned1), new File(filenameSigned2), null, "Signature1");
    }
    checkSignature(new File(filenameSigned2));
    try (PDDocument doc2 = PDDocument.load(new File(filenameSigned2))) {
        List<PDSignature> signatureDictionaries = doc2.getSignatureDictionaries();
        Assert.assertEquals(2, signatureDictionaries.size());
    }
}
Also used : CreateSignature(org.apache.pdfbox.examples.signature.CreateSignature) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) COSString(org.apache.pdfbox.cos.COSString) CreateVisibleSignature(org.apache.pdfbox.examples.signature.CreateVisibleSignature) KeyStore(java.security.KeyStore) File(java.io.File) PDSignature(org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 KeyStore (java.security.KeyStore)2 COSString (org.apache.pdfbox.cos.COSString)2 CreateVisibleSignature (org.apache.pdfbox.examples.signature.CreateVisibleSignature)2 Test (org.junit.Test)2 CreateSignature (org.apache.pdfbox.examples.signature.CreateSignature)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDSignature (org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature)1