List of usage examples for com.itextpdf.text.pdf PdfLiteral getPosLength
public int getPosLength()
From source file:com.swisscom.ais.itext.PDF.java
License:Open Source License
/** * Add a signature to pdf document/* w w w . java 2s .c om*/ * * @param externalSignature The extern generated signature * @param estimatedSize Size of external signature * @throws Exception */ public void createSignedPdf(@Nonnull byte[] externalSignature, int estimatedSize) throws Exception { // Check if source pdf is not protected by a certification if (pdfReader.getCertificationLevel() == PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED) throw new Exception( "Could not apply signature because source file contains a certification that does not allow any changes to the document"); if (Soap._debugMode) { System.out.println("\nEstimated SignatureSize: " + estimatedSize); System.out.println("Actual SignatureSize: " + externalSignature.length); System.out.println("Remaining Size : " + (estimatedSize - externalSignature.length)); } if (estimatedSize < externalSignature.length) { throw new IOException( "\nNot enough space for signature (" + (estimatedSize - externalSignature.length) + " bytes)"); } PdfLiteral pdfLiteral = (PdfLiteral) pdfSignature.get(PdfName.CONTENTS); byte[] outc = new byte[(pdfLiteral.getPosLength() - 2) / 2]; Arrays.fill(outc, (byte) 0); System.arraycopy(externalSignature, 0, outc, 0, externalSignature.length); PdfDictionary dic2 = new PdfDictionary(); dic2.put(PdfName.CONTENTS, new PdfString(outc).setHexWriting(true)); pdfSignatureAppearance.close(dic2); // Save to file OutputStream outputStream = new FileOutputStream(outputFilePath); byteArrayOutputStream.writeTo(outputStream); if (Soap._debugMode) { System.out.println("\nOK writing signature to " + outputFilePath); } byteArrayOutputStream.close(); outputStream.close(); }