Java Requires Chilkat v11.7.0+
Chilkat2 Java
Tile a Watermark Across a PDF Page
See more PDF Watermark Examples
Demonstrates a tiled watermark, which repeats the stamp over the whole page instead of placing it once. Tiling overrides position.align; tile.spacingX and spacingY set the gaps, and tile.stagger offsets alternate rows by half a tile so the pattern does not line up in columns. A faint tiled stamp carrying the name or address is the usual way to mark a document so that a leaked copy can be traced back, and zOrder back keeps it behind the text where it does not hinder reading.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 long_report.pdf
Pdf pdf = new Pdf();
try {
pdf.loadFile("qa_data/pdf/long_report.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject json = new JsonObject();
json.updateString("type", "text");
// {date:FORMAT} is replaced per page when the watermark is added. The other placeholders are
// {page}, {pages} and {filename}; {{ gives a literal brace.
json.updateString("text", "jdoe@example.com {date:yyyy-MM-dd}");
json.updateString("id", "trace");
json.updateString("zOrder", "back");
json.updateNumber("font.size", "14");
json.updateNumber("opacity", "0.15");
json.updateNumber("rotation", "30");
json.updateBool("tile.enabled", true);
json.updateNumber("tile.spacingX", "60");
json.updateNumber("tile.spacingY", "80");
json.updateBool("tile.stagger", true);
JsonObject jsonOut = new JsonObject();
try {
pdf.watermark("add", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("id: " + jsonOut.stringOf("id") + " pages: " + jsonOut.stringOf("pages"));
try {
pdf.writeFile("qa_output/report_traced.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Sample output:
// id: trace pages: 1-3
}
}