title: How to sign pdf in Java using pdfbox
body: <p>I am trying to sign pdf using pdfbox libraries.
  I have stuck now and realy need a help.</p>
  
  <p>This is my code:</p>
  
  <pre class="lang-java prettyprint-override"><code>private static void signPdf(PDDocument document) throws Exception 
  {
      PDSignature sig = new PDSignature();
      sig.setFilter(COSName.ADOBE_PPKLITE);
      sig.setSubFilter(COSName.ADBE_PKCS7_DETACHED);
      sig.setByteRange(new int[] {'a','a','a','a'});
      sig.setContents(new byte[]{(byte) 23, (byte) 23, (byte) 23, (byte) 23});
  
      SignatureOptions options = new SignatureOptions();
  
      document.addSignature(sig, new SignatureInterface() {
          public byte[] sign(InputStream content)
                  throws SignatureException, IOException       {        
               //this should be made MD5 checksum?           
              return new byte[]{(byte) 'a', (byte) 'a', (byte) 'a', (byte) 'a'};
          }
      }, options);
  }
  </code></pre>
  
  <p>Then Iam saving my pdf, but:
  1) I have noticed that sign method is never called
  2) Where should I attach certyficate? in sign method?</p>
  
  <p>pdf:</p>
  
  <pre><code>/Type /Sig
  /Filter /Adobe.PPKLite
  /SubFilter /adbe.pkcs7.sha1
  /Contents &lt;0000000000. a lot of zeros..000&gt;
  /ByteRange [0 1000000000 1000000000 1000000000]
  </code></pre>
  
  <p>I think that i miss something, but documentation says nothing about how to sign a file.</p>
  
  <p>Tahnks in advance JC.</p>
  
  <p>@Ed</p>
  
  <p>Here is how I save my pdf:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static void saveFile(COSDocument doc, String out)
          throws IOException, COSVisitorException {  
      java.io.OutputStream os = null;  
      COSWriter writer = null;  
      try {
          os = new java.io.FileOutputStream(out);
          writer = new COSWriter(os);
          writer.write(doc);
      } finally {
          if (os != null) {
              os.close();
          }
          if (writer != null) {
              writer.close();
          }
      }
  }
  </code></pre>