'How to fix 'Unexpected byte range values defining scope of signed data' using pdfbox

How to fix 'Unexpected byte range values defining scope of signed data. Details: The signature byte range is invalid' while adding external signature in multiple pages using PDFBOX (2.0.12)

The following code is working for single page signing while multi page signing not working.

PDDocument pddoc = PDDocument.load(file);
PDSignature pds = new PDSignature();
SignatureOptions signatureOptions;
PDRectangle rect;//coordinates to visible signature
File imgFile;//background image in visible signature
String signDisplayInfo;//data to be displayed in visible signature
createVisualSignatureTemplate // generates the visible signature


for(int i=0;i<pageCount;i++) {
   signatureOptions.setVisualSignature(AddVisibleSignature.createVisualSignatureTemplate(pddoc, i, rect, imgFile,signDisplayInfo));
   signatureOptions.setPage(i);
   pddoc.addSignature(pds, signatureOptions);
}
externalSigning = pddoc.saveIncrementalForExternalSigning(fos);
InputStream dataToSign = externalSigning.getContent();
hash = DigestUtils.sha256Hex(dataToSign);

After preparing the hash, is being sent to another server to get the signature. After getting the response adding the signature into the pdf.

String pkcs7Response = responseSignature;
byte[] sigbytes = java.util.Base64.getDecoder().decode(pkcs7Response);                   
externalSigning.setSignature(sigbytes);
pddoc.save(fos);
pddoc.close();

Error during signature verification.

Unexpected byte range values defining scope of signed data.

Details: The signature byte range is invalid



Solution 1:[1]

Same problem.. Unexpected byte range value defining scope of signed data..

Using foxit..

Right click signature.. Then click show signed version..

You will get your signed version and save it..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arun Kumar R