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

Watermark Only Some of a PDF's Pages

See more PDF Watermark Examples

Demonstrates choosing the pages to stamp. "pages" takes all (the default), first, last, odd, even, or a list of 1-based pages and ranges such as 1,3,5-9,12- where a trailing dash means "to the end". "excludePages" takes the same syntax and is applied afterwards, which is how you say "every page except the cover". The result reports the pages the watermark actually landed on.

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 four_pages.pdf

    CkPdf pdf = new CkPdf();
    success = pdf.LoadFile("qa_data/pdf/four_pages.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "pages in the document: " + String.valueOf(pdf.get_NumPages()));

    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();
    json.UpdateString("type","text");
    json.UpdateString("text","INTERNAL");
    json.UpdateString("id","internal");

    //  Every page except the first.
    json.UpdateString("pages","all");
    json.UpdateString("excludePages","1");
    success = pdf.Watermark("add",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "every page but the cover: " + jsonOut.stringOf("pages"));

    //  A list of pages and ranges, on a second copy of the document.
    CkPdf pdf2 = new CkPdf();
    success = pdf2.LoadFile("qa_data/pdf/four_pages.pdf");
    if (success == false) {
        Log.i(TAG, pdf2.lastErrorText());
        return;
        }

    CkJsonObject json2 = new CkJsonObject();
    json2.UpdateString("type","text");
    json2.UpdateString("text","APPENDIX");
    json2.UpdateString("id","appendix");
    json2.UpdateString("pages","2,4-");
    success = pdf2.Watermark("add",json2,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf2.lastErrorText());
        return;
        }

    Log.i(TAG, "page 2 and 4 onwards: " + jsonOut.stringOf("pages"));

    success = pdf.WriteFile("qa_output/four_pages_internal.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  Sample output:

    //  pages in the document: 4
    //  every page but the cover: 2-4
    //  page 2 and 4 onwards: 2,4

  }

  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."
  }
}