@@ -464,6 +464,25 @@ else if (keyType.equalsIgnoreCase(KEY_TYPE_PKCS12))
464464 ContentReader pdfReader = getReader (targetNodeRef );
465465 PdfReader reader = new PdfReader (pdfReader .getContentInputStream ());
466466
467+ // If the page number is 0 because it couldn't be parsed or for
468+ // some other reason, set it to the first page, which is 1.
469+ // If the page number is negative, assume the intent is to "wrap".
470+ // For example, -1 would always be the last page.
471+ int numPages = reader .getNumberOfPages ();
472+ if (pageNumber < 1 && pageNumber == 0 ) {
473+ pageNumber = 1 ; // use the first page
474+ } else {
475+ // page number is negative
476+ pageNumber = numPages + 1 + pageNumber ;
477+ if (pageNumber <= 0 ) pageNumber = 1 ;
478+ }
479+
480+ // if the page number specified is more than the num of pages,
481+ // use the last page
482+ if (pageNumber > numPages ) {
483+ pageNumber = numPages ;
484+ }
485+
467486 // create temp dir to store file
468487 File alfTempDir = TempFileProvider .getTempDir ();
469488 tempDir = new File (alfTempDir .getPath () + File .separatorChar + targetNodeRef .getId ());
@@ -486,7 +505,7 @@ else if (keyType.equalsIgnoreCase(KEY_TYPE_PKCS12))
486505 // set reason for signature and location of signer
487506 sap .setReason (reason );
488507 sap .setLocation (location );
489-
508+
490509 if (visibility .equalsIgnoreCase (VISIBILITY_VISIBLE ))
491510 {
492511 //create the signature rectangle using either the provided position or
0 commit comments