Android™ Requires Chilkat v11.7.0+
Android™
Add an Image (Logo) Watermark to a PDF
See more PDF Watermark Examples
Demonstrates an image watermark placed in a corner of every page. The image is a PNG or JPEG given either by path ("image.path") or, as here, as base64 in the JSON itself ("image.base64"), which suits a server that keeps its logo in a database rather than on disk. PNG transparency is kept. Giving only a width keeps the aspect ratio, and the units member says those numbers are inches rather than points.Chilkat Android™ Downloads
// 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;
}
CkJsonObject json = new CkJsonObject();
json.UpdateString("type","image");
// A small PNG logo, base64 encoded. Use "image.path" instead to name a PNG or JPEG file:
// ignore = json.UpdateString("image.path","logo.png");
json.UpdateString("image.base64","iVBORw0KGgoAAAANSUhEUgAAAGAAAAAgCAYAAADtwH1UAAAApElEQVR42u3a7QmAIBgAYTdpo/ZphhZti6IBylLrlXoO/G3cIeFHSgAAFDKM08rCy8Jzg6Ug8UJ0JF+EDuSXRGgxX8+jufxlTk0nFuCCiF360aidXICMiDP5uQgCXAxQI782ggAHEu7IP4sgQOcBIMA35ItgBQhQEqBmtbX6xl//hAUI3gcIELwTFiD4LEiA4NNQAYLvA+BGzJ0wvIrwLggA8DgbwPbrr8c7+lkAAAAASUVORK5CYII=");
// The size to draw it at. Give only the width and the height follows the aspect ratio.
json.UpdateString("units","in");
json.UpdateNumber("image.width","1.5");
// Bottom-right, half an inch in from the page edges.
json.UpdateString("position.align","bottomRight");
json.UpdateNumber("position.margin","0.5");
json.UpdateNumber("opacity","0.85");
json.UpdateString("id","logo");
CkJsonObject jsonOut = new CkJsonObject();
success = pdf.Watermark("add",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "id: " + jsonOut.stringOf("id") + " pages: " + jsonOut.stringOf("pages"));
success = pdf.WriteFile("qa_output/four_pages_logo.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// Sample output:
// id: logo pages: 1-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."
}
}