Inspect digital signatures

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

GetN2fromSig.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27867868/how-can-i-decrypt-a-pdf-document-with-the-owner-password
  11.  */
  12. package com.itextpdf.samples.sandbox.security;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.forms.fields.PdfFormField;
  16. import com.itextpdf.kernel.pdf.*;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.Assert;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.IOException;
  25. import java.util.Map;
  26.  
  27. @Category(SampleTest.class)
  28. public class GetN2fromSig {
  29.     public static String SRC = "./src/test/resources/pdfs/signature_n2.pdf";
  30.     public static String CMP_RESULT = "BT\n" +
  31.             "1 0 0 1 0 49.55 Tm\n" +
  32.             "/F1 12 Tf\n" +
  33.             "(This document was signed by Bruno)Tj\n" +
  34.             "1 0 0 1 0 31.55 Tm\n" +
  35.             "(Specimen.)Tj\n" +
  36.             "ET";
  37.  
  38.     protected static String RESULT;
  39.  
  40.     @Before
  41.     public void before() throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));
  43.  
  44.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  45.         Map fields = form.getFormFields();
  46.  
  47.         PdfFormField field = fields.get("Signature1");
  48.         PdfDictionary widget = field.getWidgets().get(0).getPdfObject();
  49.         PdfDictionary ap = widget.getAsDictionary(PdfName.AP);
  50.         PdfStream normal = ap.getAsStream(PdfName.N);
  51.         PdfDictionary resources = normal.getAsDictionary(PdfName.Resources);
  52.         PdfDictionary xobject = resources.getAsDictionary(PdfName.XObject);
  53.         PdfStream frm = xobject.getAsStream(new PdfName("FRM"));
  54.         PdfDictionary res = frm.getAsDictionary(PdfName.Resources);
  55.         PdfDictionary xobj = res.getAsDictionary(PdfName.XObject);
  56.         PdfStream n2 = xobj.getAsStream(new PdfName("n2"));
  57.         byte[] stream = n2.getBytes();
  58.         RESULT = new String(stream);
  59.         System.out.println(RESULT);
  60.     }
  61.  
  62.     @Test
  63.     public void compareResults() throws IOException {
  64.         Assert.assertEquals(CMP_RESULT.trim(), RESULT.trim());
  65.     }
  66. }

Resources

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now