Java Requires Chilkat v11.7.0+
Chilkat2 Java
How a PDF Watermark Is Stored: artifact, annotation, layer or content
See more PDF Watermark Examples
Demonstrates the approach member, which decides how the watermark is stored and therefore what can be done with it afterwards. artifact (the default) is marked content that viewers draw, text extraction leaves out and Chilkat can remove later. annotation is a Watermark annotation, always on top, and the only approach allowed on a signed PDF while PreserveSignatures is true. layer is an optional-content layer the viewer can hide and that can be made print-only. content draws straight into the page with no marker: visible everywhere and the tamper-resistant choice, but by design it can never be listed or removed again.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 two_page_memo.pdf
JsonObject json = new JsonObject();
JsonObject jsonOut = new JsonObject();
JsonObject filter = new JsonObject();
// The same watermark stored four different ways, each in its own copy of the document.
Pdf pdf = new Pdf();
try {
pdf.loadFile("qa_data/pdf/two_page_memo.pdf");
json.updateString("type", "text");
json.updateString("text", "DRAFT");
json.updateString("id", "draft");
json.updateString("approach", "artifact");
pdf.watermark("add", json, jsonOut);
// detect answers whether anything matching the filter is in the document. Finding nothing is
// still success: "found" is simply false.
filter.updateString("id", "draft");
pdf.watermark("detect", filter, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("artifact: found = " + jsonOut.boolOf("found"));
// An annotation: always drawn on top, and the one approach a signed PDF accepts.
Pdf pdf2 = new Pdf();
try {
pdf2.loadFile("qa_data/pdf/two_page_memo.pdf");
json.updateString("approach", "annotation");
pdf2.watermark("add", json, jsonOut);
pdf2.watermark("detect", filter, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("annotation: found = " + jsonOut.boolOf("found"));
// A layer, which the viewer's layers panel can switch off.
Pdf pdf3 = new Pdf();
try {
pdf3.loadFile("qa_data/pdf/two_page_memo.pdf");
json.updateString("approach", "layer");
json.updateString("layer.name", "Draft marking");
pdf3.watermark("add", json, jsonOut);
pdf3.watermark("detect", filter, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("layer: found = " + jsonOut.boolOf("found"));
// Drawn into the page content with no marker. There is nothing to find afterwards, which is the
// point of it: it cannot be stripped out by a tool that removes watermarks.
Pdf pdf4 = new Pdf();
try {
pdf4.loadFile("qa_data/pdf/two_page_memo.pdf");
json.updateString("approach", "content");
pdf4.watermark("add", json, jsonOut);
pdf4.watermark("detect", filter, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("content: found = " + jsonOut.boolOf("found"));
// Sample output:
// artifact: found = True
// annotation: found = True
// layer: found = True
// content: found = False
}
}