Java Requires Chilkat v11.7.0+
Chilkat2 Java
Watermark a Signed PDF Without Breaking the Signature
See more PDF Watermark Examples
Demonstrates the one approach a signed PDF accepts while its signatures are being preserved. Adding a watermark to the page content rewrites the page, which invalidates every existing signature, so with PreserveSignatures true (the default) only the annotation approach is allowed: an annotation is added as an incremental update that leaves the signed bytes untouched. Removing anything is refused for the same reason. Set PreserveSignatures to false to allow the other approaches, and the signatures in the file stop verifying.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at invoice_signed.pdf
Pdf pdf = new Pdf();
try {
pdf.loadFile("qa_data/pdf/invoice_signed.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("signatures: " + pdf.getNumSignatures());
JsonObject json = new JsonObject();
JsonObject jsonOut = new JsonObject();
json.updateString("type", "text");
json.updateString("text", "PAID");
json.updateString("id", "paid");
json.updateString("color", "#2E7D32");
json.updateString("position.align", "topRight");
json.updateNumber("position.margin", "36");
// An artifact watermark changes the page content, so it is refused while the signatures matter.
json.updateString("approach", "artifact");
try {
pdf.watermark("add", json, jsonOut);
} catch (ChilkatException e) {
System.out.println("The artifact approach was refused on a signed PDF, as it should be.");
}
// An annotation is added without touching the signed bytes.
json.updateString("approach", "annotation");
try {
pdf.watermark("add", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("annotation added, pages: " + jsonOut.stringOf("pages"));
try {
pdf.writeFile("qa_output/invoice_paid.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The signature still verifies: the original bytes are unchanged and the annotation was appended.
Pdf check = new Pdf();
try {
check.loadFile("qa_output/invoice_paid.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject sigInfo = new JsonObject();
boolean verified;
try {
check.verifySignature(0, sigInfo);
verified = true;
} catch (ChilkatException e) {
verified = false;
}
System.out.println("signature still verifies: " + verified);
// Sample output:
// signatures: 1
// The artifact approach was refused on a signed PDF, as it should be.
// annotation added, pages: 1
// signature still verifies: True
}
}