title: PDF signature itext pkcs7 multi sign
body: <p>I would like to do multiple signature on pdf document, like in a workflow.
  I am using following code to sign pdf which I wrote, which works fine.</p>
  
  <p>To get hash</p>
  
  <pre><code>public String getHash() {
      LOGGER.debug("PDFSigner.getHash : method invoked");
      String pdfHashValue = null;
      try {
          int contentEstimated = PDFSigner.CONTENT_ESTIMATED;//8192
          HashMap&lt;PdfName, Integer&gt; exc = new HashMap&lt;&gt;();
          exc.put(PdfName.CONTENTS, contentEstimated * 2 + 2);
          PdfSignature pdfSignature = new PdfSignature(PdfName.ADOBE_PPKLITE,
                  PdfName.ADBE_PKCS7_DETACHED);
          pdfSignature.setReason(appearance.getReason());
          pdfSignature.setLocation(appearance.getLocation());
          pdfSignature.setContact(appearance.getContact());
          pdfSignature.setDate(new PdfDate(appearance.getSignDate()));
          appearance.setCryptoDictionary(pdfSignature);
          appearance.preClose(exc);
          InputStream data = appearance.getRangeStream();
          MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
          byte buf[] = new byte[contentEstimated];
          int n = 0;
          while ((n = data.read(buf, 0, contentEstimated)) &gt; 0) {
              messageDigest.update(buf, 0, n);
          }
          byte hash[] = messageDigest.digest();
          byte[] reqBytesdata = Hex.encode(hash);
          pdfHashValue = new String(reqBytesdata, "UTF8"); 
      } catch (Exception exp) {
          LOGGER.error("PDFSigner error occured getHash", exp);
      }
      return pdfHashValue;
  }
  </code></pre>
  
  <p>To sign document</p>
  
  <pre><code>//dSignature is the received encoded signature pkcs7 (SHA256).
  //starts with MIIOWQYJKoZIhvcNAQcCo.....
      public boolean doSign(String dSignature) throws IOException, DocumentException {
  
          boolean pdfGenerationStatus = false;
          try {
              byte[] PKCS7Response = Base64.decode(dSignature
                      .getBytes("UTF8"));
              byte[] paddedSig = new byte[PDFSigner.CONTENT_ESTIMATED];
              System.arraycopy(PKCS7Response, 0, paddedSig, 0,
                      PKCS7Response.length);
              PdfDictionary pdfDictionary = new PdfDictionary();
              pdfDictionary.put(PdfName.CONTENTS,
                      new PdfString(paddedSig).setHexWriting(true));  
              appearance.close(pdfDictionary);
              pdfGenerationStatus = true;
  
          } catch (Exception exp) {
              LOGGER.error("doSign ", exp);
          }
          return pdfGenerationStatus; 
  
      }
  </code></pre>
  
  <p>And the above code works fine.
  My new requirement is to add multiple signature. Is there any way to reuse this code snippet for the same.
  I have gone through <a href="https://stackoverflow.com/questions/38374027/signing-a-pdf-using-pkcs7-using-itext">this</a>, <a href="https://stackoverflow.com/questions/29344953/itext-java-signing-pdf-documentexception-not-enough-space">this</a> and <a href="http://cysorz-software-hardware.blogspot.in/2008/11/how-to-sign-pdf-using-itext-and.html" rel="nofollow noreferrer">this</a> but no luck.</p>
  
  <p>Other than this what I tried is, Created blank multiple blank signature and tried to attach the signature. But It resulted in creating corrupted files. I also tried creating file using method mentioned in this <a href="https://stackoverflow.com/questions/29344953/itext-java-signing-pdf-documentexception-not-enough-space">link</a>. <code>MakeSignature.signExternalContainer</code>
  Also gone through a great document Digital Signatures for PDF documents</p>
  
  <p>The use-case is like</p>
  
  <ol>
  <li>Create pdf </li>
  <li>Generate doc hash</li>
  <li>send to external server </li>
  <li>external server will return a pkcs7 base64 encoded string </li>
  <li>append the signature to the pdf</li>
  </ol>
  
  <p><strong>UPDATE</strong></p>
  
  <p>code change made was for 'append mode', following code change made my code to support multisign, thanks to @Paulo Soares, @mlk</p>
  
  <pre><code>private void initAppearanceAppend(String customerName) throws IOException, DocumentException {
      System.out.println("PDFSigner.initAppearanceAppend");
      PdfReader readerpdf = new PdfReader(this.getInputPdfFilePath());
      int lastPageNumber = readerpdf.getNumberOfPages();
      this.pdfSignatureMetaData.setPageNumber(lastPageNumber);
      this.pdfSignatureMetaData.setSignerName(customerName);
      //this.pdfSignatureMetaData.setPageNumber(PDFSigner.SIGNATURE_PAGE_NUMBER);
      OutputStream fout = new FileOutputStream(this.outputPdfFilePath);
      //PdfStamper stamperpdf = PdfStamper.createSignature(readerpdf, fout, '\0'); OLD CODE WITHOUT APPEND MODE
      PdfStamper stamperpdf = PdfStamper.createSignature(readerpdf, fout, '\0', new File("E://temp"), true);
  
      this.appearance = stamperpdf.getSignatureAppearance();
      LOGGER.debug("PDFSigner.initAppearanceAppend : default configurations are made");
  }
  </code></pre>