CreateplaceholderandGethash (...) auf einem bereits signierten PDF anscheinend die vorherige Signatur zu ersetzen oder zu überschreiben, anstatt ein neues als inkrementelles Update anzuhängen. Die neue PDF kehrte zurück, wenn die bereits signierte PDF durch die Funktion von Createplaceholder und CODE> ist als eine neue Signatur, die so aussieht, als hätte er die erste gültige Signatur ersetzt, und der PDF enthält nicht die vorhandene erste signierte Signatur - kann nur eine neue Platzhalter -Signatur mit/code ordnungsgemäßen. Signatur)
CreateplaceholderandGethash (...) Sollte ein neues Platzhalter/Signature -Feld (Signature2) erstellen, ohne Berühren zu berühren oder ungültig zu machen. /> < /ol>
Platzhalter-Erstellung und Hash-Extraktion < /strong> < /p>
Code: Select all
public PlaceholderAndHash createPlaceholderAndGetHash(InputStream pdfIn, int placeholderSize) throws IOException {
byte[] pdfBytes = pdfIn.readAllBytes();
try (PDDocument doc = Loader.loadPDF(pdfBytes)) {
doc.setAllSecurityToBeRemoved(false); // preserve previous signature
PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
if (form == null) {
form = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(form);
}
PDSignatureField field = new PDSignatureField(form);
field.setPartialName(FIELD_NAME); // Signature1, Signature2, etc.
form.getFields().add(field);
PDPage page = doc.getPage(0);
PDAnnotationWidget w = field.getWidgets().get(0);
w.setRectangle(new PDRectangle(0,0,0,0));
w.setPage(page);
page.getAnnotations().add(w);
PDSignature sig = new PDSignature();
sig.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
sig.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
field.setValue(sig);
int reserved = Math.max(placeholderSize, 16*1024);
SignatureOptions opts = new SignatureOptions();
opts.setPreferredSignatureSize(reserved);
doc.addSignature(sig, null, opts);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ExternalSigningSupport ext = doc.saveIncrementalForExternalSigning(bout);
byte[] toHash = ext.getContent().readAllBytes();
ext.setSignature(new byte[reserved]);
return new PlaceholderAndHash(bout.toByteArray(), toHash);
}
}
Code: Select all
public byte[] signPdf(InputStream pdfInputStream, byte[] derSignature, String fieldName) throws IOException {
// Read PDF
byte[] pdfBytes = pdfInputStream.readAllBytes();
try (PDDocument doc = Loader.loadPDF(pdfBytes)) {
PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
PDSignatureField field = (PDSignatureField) form.getField(fieldName);
PDSignature sig = field.getSignature();
int[] byteRange = sig.getByteRange();
int signatureOffset = byteRange[1] + 1;
byte[] signedPdfBytes = pdfBytes.clone();
String hexSignature = bytesToHex(derSignature);
byte[] hexSignatureBytes = hexSignature.getBytes("UTF-8");
COSString contents = (COSString) sig.getCOSObject().getDictionaryObject(COSName.CONTENTS);
int maxLength = contents.getBytes().length * 2;
if (hexSignatureBytes.length > maxLength) {
throw new IllegalArgumentException("Signature too large");
}
System.arraycopy(hexSignatureBytes, 0, signedPdfBytes, signatureOffset, hexSignatureBytes.length);
return signedPdfBytes;
}
}