Sample code for 30+ languages & platforms
Java 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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;

public class ChilkatExample {

    public static void main(String[] args) {
        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.
        Pdf pdf = new Pdf();
        try {
            pdf.loadFile("qa_data/pdf/quote_subset_font.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json = new JsonObject();
        JsonObject jsonOut = new JsonObject();

        // 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");
        try {
            pdf.textReplace("check", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("58314: ok = " + 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");
        try {
            pdf.textReplace("check", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Q-58341: ok = " + jsonOut.boolOf("ok"));
        if (!jsonOut.boolOf("ok")) {
            // The problem names the font and the characters it cannot draw.
            System.out.println("    reason: " + jsonOut.stringOf("problems[0].reason"));
            System.out.println("    font: " + jsonOut.stringOf("problems[0].font"));
            System.out.println("    missing: " + jsonOut.stringOf("problems[0].chars"));
        }

        // "apply" with a problem returns false and changes nothing.
        try {
            pdf.textReplace("apply", json, jsonOut);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        System.out.println("apply returned " + success + ", applied = " + jsonOut.boolOf("applied"));

        // 2. A font that is not embedded and not one of the standard 14.
        Pdf pdf2 = new Pdf();
        try {
            pdf2.loadFile("qa_data/pdf/letter_verdana.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json2 = new JsonObject();
        json2.updateString("labels[0].label", "Account Number:");
        json2.updateString("labels[0].value", "0000099999");
        try {
            pdf2.textReplace("check", json2, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Verdana, not embedded: ok = " + 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
    }
}