With the introduction of the pdfOffice add-on to the iText 7 Suite, we have now extended our capabilities to cover one of the most relevant document conversion functionalities - converting MS Office documents into PDF. 

Given the diverse range of modules and add-ons available, iText has the flexibility and potential to address many of the most common customer use cases. One such use case is the digital signing of documents. 

Code Snippet:

In the following example we will look into a possible situation where a customer may wish to convert an MS Office document into a digitally signed PDF. As with our other pdfOffice examples for this release, we'll be using an extract from Lewis Carroll's classic novel Alice's Adventures in Wonderland (commonly known as simply Alice in Wonderland) which we've saved in .docx format.

In order to achieve this use case, the pdfOffice add-on is combined with iText 7 Core's "sign" module. First we perform the conversion, and then we create the signature appearance and sign the document.

import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.StampingProperties; import com.itextpdf.licensekey.LicenseKey; import com.itextpdf.pdfoffice.OfficeConverter; import com.itextpdf.signatures.BouncyCastleDigest; import com.itextpdf.signatures.DigestAlgorithms; import com.itextpdf.signatures.IExternalDigest; import com.itextpdf.signatures.IExternalSignature; import com.itextpdf.signatures.PdfSignatureAppearance; import com.itextpdf.signatures.PdfSigner; import com.itextpdf.signatures.PrivateKeySignature; import; import; import; import; import; import; import; import; import org.bouncycastle.jce.provider.BouncyCastleProvider; public class PdfOfficeSigner{ public static String KEY = "pdfOffice_commercial_key.xml"; public static final String KEYSTORE = "./SUP4126/src/main/resources/encryption/ks"; public static final char[] PASSWORD = "pass123".toCharArray(); public static void main(String[] args) throws Exception { LicenseKey.loadLicenseFile(KEY); ByteArrayOutputStream baos = new ByteArrayOutputStream(0); OfficeConverter.convertOfficeDocumentToPdf(new FileInputStream("Alice in Wonderland Excerpt.docx"), baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); PdfReader reader = new PdfReader(bais); PdfSigner signer = new PdfSigner(reader, new FileOutputStream("Alice in Wonderland Excerpt.pdf"), new StampingProperties()); // Create the signature appearance Rectangle rect = new Rectangle(36, 36, 200, 100); PdfSignatureAppearance appearance = signer.getSignatureAppearance(); appearance .setReason("Test 1") .setLocation("Ghent") .setReuseAppearance(false) .setPageRect(rect) .setPageNumber(1); signer.setFieldName("sig"); BouncyCastleProvider provider = new BouncyCastleProvider(); Security.addProvider(provider); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(new FileInputStream(KEYSTORE), PASSWORD); String alias = ks.aliases().nextElement(); PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD); Certificate[] chain = ks.getCertificateChain(alias); IExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, provider.getName()); IExternalDigest digest = new BouncyCastleDigest(); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.signDetached(digest, pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CMS); } }


Right-click the link below and select "Save link as..." to download