Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.7.0+

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  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

    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();
    CkJsonObject filter = new CkJsonObject();

    //  The same watermark stored four different ways, each in its own copy of the document.
    CkPdf pdf = new CkPdf();
    success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    json.UpdateString("type","text");
    json.UpdateString("text","DRAFT");
    json.UpdateString("id","draft");
    json.UpdateString("approach","artifact");
    success = pdf.Watermark("add",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  detect answers whether anything matching the filter is in the document.  Finding nothing is
    //  still success: "found" is simply false.
    filter.UpdateString("id","draft");
    success = pdf.Watermark("detect",filter,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "artifact: found = " + String.valueOf(jsonOut.BoolOf("found")));

    //  An annotation: always drawn on top, and the one approach a signed PDF accepts.
    CkPdf pdf2 = new CkPdf();
    success = pdf2.LoadFile("qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        Log.i(TAG, pdf2.lastErrorText());
        return;
        }

    json.UpdateString("approach","annotation");
    success = pdf2.Watermark("add",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf2.lastErrorText());
        return;
        }

    success = pdf2.Watermark("detect",filter,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf2.lastErrorText());
        return;
        }

    Log.i(TAG, "annotation: found = " + String.valueOf(jsonOut.BoolOf("found")));

    //  A layer, which the viewer's layers panel can switch off.
    CkPdf pdf3 = new CkPdf();
    success = pdf3.LoadFile("qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        Log.i(TAG, pdf3.lastErrorText());
        return;
        }

    json.UpdateString("approach","layer");
    json.UpdateString("layer.name","Draft marking");
    success = pdf3.Watermark("add",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf3.lastErrorText());
        return;
        }

    success = pdf3.Watermark("detect",filter,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf3.lastErrorText());
        return;
        }

    Log.i(TAG, "layer: found = " + String.valueOf(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.
    CkPdf pdf4 = new CkPdf();
    success = pdf4.LoadFile("qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        Log.i(TAG, pdf4.lastErrorText());
        return;
        }

    json.UpdateString("approach","content");
    success = pdf4.Watermark("add",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf4.lastErrorText());
        return;
        }

    success = pdf4.Watermark("detect",filter,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf4.lastErrorText());
        return;
        }

    Log.i(TAG, "content: found = " + String.valueOf(jsonOut.BoolOf("found")));

    //  Sample output:

    //  artifact: found = True
    //  annotation: found = True
    //  layer: found = True
    //  content: found = False

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}