How do I find a digital signature's byte offset in the pdf?

Skip to first unread message

Aaron Gravesdale

Dec 23, 2013, 5:00:56 PM12/23/13

I have a question regarding the DigitalSignaturesTest sample.  Is it possible to calculate the offset of the signature in the output file named "signed_doc.pdf"?  I’ve added the code below, but this gives me an offset of 98876; I was expecting an offset of 98953 (I see this value when I’m looking at the signed document with a hex-editor…see screenshot):

   static long getSignatureOffset(File pdfFile) throws PDFNetException {
      PDFDoc pdfDoc = new PDFDoc(pdfFile.getPath());
      try {
         Field field = pdfDoc.getField("Signature1");

         DictIterator dictIterator = field.getSDFObj().getDictIterator();
         while (dictIterator.hasNext() == true) {

            if (dictIterator.key() != null) {
               String key = dictIterator.key().getName();
               if ("V".equals(key)) {
                  Obj value = dictIterator.value();
                  Obj contents = value.findObj("Contents");
                  System.out.println("type=" + contents.getType());
                  System.out.println("offset=" + contents.getOffset());
                  return Long.valueOf(contents.getOffset());
                  // findByteRange("   ", "/" + key, dictIterator.value());

         return 0;
      } finally {


Your code is yielding the correct value.  The digital signature dict is located in the xref table at the offset 98876.  If you have cosedit.exe as part of the SDK you downloaded (which should be the case if you're using a Windows version of the SDK), then you can see it at that offset (object number 54).

According to the PDF Reference, the ByteRange array is an array of pairs of integers (starting byte offset, length in bytes) describing the exact byte range for the digest calculation. Multiple discontiguous byte ranges are used to describe a digest that does not include the signature value (the Contents entry) itself.  So 98953 is just a value in the ByteRange array referring to the scope of the signature's digest calculation.

Reply all
Reply to author
0 new messages