1

I have the requirement to get the page index of the page containing a digital signature in a pdf document. How can I get it using Apache PDFBox?

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Stranger
  • 147
  • 2
  • 15

1 Answers1

1
try (PDDocument doc = PDDocument.load(new File("....")))
{
    PDPageTree pageTree = doc.getPages();
    PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
    for (PDField field : acroForm.getFieldTree())  // null check omitted
    {
        if (field instanceof PDSignatureField)
        {
            PDSignatureField sigField = (PDSignatureField) field;
            for (PDAnnotationWidget widget : sigField.getWidgets())
            {
                PDPage page = widget.getPage();
                if (page != null)
                {
                    System.out.println("Signature on page " + (pageTree.indexOf(widget.getPage()) + 1));
                }
            }
        }
    }
}
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • 1
    Actually the widget page attribute is optional, so `page` may indeed be `null` in spite of the signature being visible on some page. For such a case you should use the _plan B_ illustrated in [this answer](https://stackoverflow.com/a/22132921/1729265) and [this answer](https://stackoverflow.com/a/36894982/1729265). – mkl Jul 27 '18 at 16:21