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

Why a PDF Text Replacement Is Refused: the Font Rule

See more PDF Text Replacement Examples

A PDF stores glyphs drawn with particular fonts, so new text can only be drawn when the original font can draw it. This example shows the two common refusals. A font embedded as a subset carries only the glyphs the document used: a value made of other characters is refused with the reason missingGlyph, and the report names the font and the characters it lacks. A font that is neither one of the standard 14 nor embedded (here Verdana) is refused with nonStandardUnembeddedFont, because Chilkat cannot know what a viewer will draw for it. A non-embedded Arial, Times New Roman or Courier New is treated as the standard font every viewer substitutes for it.

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 quote_subset_font.pdf
    //  and letter_verdana.pdf

    //  1. An embedded subset font: only the glyphs the document uses are in the PDF.
    CkPdf pdf = new CkPdf();
    success = pdf.LoadFile("qa_data/pdf/quote_subset_font.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

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

    //  The regular face's subset carries the characters of the quotation's text, among them the digits of
    //  "58341".  A new number made of those digits can be drawn...
    json.UpdateString("labels[0].label","Quote #:");
    json.UpdateString("labels[0].value","58314");
    success = pdf.TextReplace("check",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

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

    //  ...but "Q" and "-" are not in the subset (nothing in the quotation's regular text uses them).
    json.UpdateString("labels[0].value","Q-58341");
    success = pdf.TextReplace("check",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "Q-58341: ok = " + String.valueOf(jsonOut.BoolOf("ok")));
    if (jsonOut.BoolOf("ok") == false) {
        //  The problem names the font and the characters it cannot draw.
        Log.i(TAG, "    reason: " + jsonOut.stringOf("problems[0].reason"));
        Log.i(TAG, "    font: " + jsonOut.stringOf("problems[0].font"));
        Log.i(TAG, "    missing: " + jsonOut.stringOf("problems[0].chars"));
        }

    //  "apply" with a problem returns false and changes nothing.
    success = pdf.TextReplace("apply",json,jsonOut);
    Log.i(TAG, "apply returned " + String.valueOf(success) + ", applied = " + String.valueOf(jsonOut.BoolOf("applied")));

    //  2. A font that is not embedded and not one of the standard 14.
    CkPdf pdf2 = new CkPdf();
    success = pdf2.LoadFile("qa_data/pdf/letter_verdana.pdf");
    if (success == false) {
        Log.i(TAG, pdf2.lastErrorText());
        return;
        }

    CkJsonObject json2 = new CkJsonObject();
    json2.UpdateString("labels[0].label","Account Number:");
    json2.UpdateString("labels[0].value","0000099999");
    success = pdf2.TextReplace("check",json2,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf2.lastErrorText());
        return;
        }

    Log.i(TAG, "Verdana, not embedded: ok = " + String.valueOf(jsonOut.BoolOf("ok")) + ", reason: " + jsonOut.stringOf("problems[0].reason") + ", font: " + jsonOut.stringOf("problems[0].font"));

    //  Sample output:

    //  58314: ok = true
    //  Q-58341: ok = false
    //      reason: missingGlyph
    //      font: AAAAAA+DejaVuSans
    //      missing: Q-
    //  apply returned false, applied = false
    //  Verdana, not embedded: ok = false, reason: nonStandardUnembeddedFont, font: Verdana

  }

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