Java Requires Chilkat v11.7.0+
Chilkat2 Java
Add a CONFIDENTIAL Watermark to Every Page of a PDF
See more PDF Watermark Examples
Demonstrates the simplest watermark: a line of text across every page. "rotation" set to diagonal runs the text from the bottom-left corner to the top-right, computed for each page size, and "scale" with fit page sizes it to fill 80 percent of the page. The watermark is stored as a marked artifact by default, which means viewers show it, text extraction leaves it out, and it can be found and removed again later. Nothing is written until WriteFile is called.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");
json.updateString("text", "CONFIDENTIAL");
// An id of your own choosing, so that the watermark can be found and removed later. One is
// generated when you do not give one.
json.updateString("id", "confidential");
json.updateString("color", "#C00000");
json.updateNumber("opacity", "0.25");
json.updateString("rotation", "diagonal");
json.updateString("scale.fit", "page");
json.updateNumber("scale.fitPercent", "80");
JsonObject jsonOut = new JsonObject();
try {
pdf.watermark("add", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The id, and the pages it was stamped on. "pages" defaults to all.
System.out.println("id: " + jsonOut.stringOf("id"));
System.out.println("pages: " + jsonOut.stringOf("pages"));
try {
pdf.writeFile("qa_output/report_confidential.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Sample output:
// id: confidential
// pages: 1-3
}
}