Unicode C Requires Chilkat v11.7.0+
Unicode C
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 Unicode C Downloads
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
HCkPdfW pdf2;
HCkJsonObjectW json2;
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 = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/quote_subset_font.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
// 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...
CkJsonObjectW_UpdateString(json,L"labels[0].label",L"Quote #:");
CkJsonObjectW_UpdateString(json,L"labels[0].value",L"58314");
success = CkPdfW_TextReplace(pdf,L"check",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"58314: ok = %d\n",CkJsonObjectW_BoolOf(jsonOut,L"ok"));
// ...but "Q" and "-" are not in the subset (nothing in the quotation's regular text uses them).
CkJsonObjectW_UpdateString(json,L"labels[0].value",L"Q-58341");
success = CkPdfW_TextReplace(pdf,L"check",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"Q-58341: ok = %d\n",CkJsonObjectW_BoolOf(jsonOut,L"ok"));
if (CkJsonObjectW_BoolOf(jsonOut,L"ok") == FALSE) {
// The problem names the font and the characters it cannot draw.
wprintf(L" reason: %s\n",CkJsonObjectW_stringOf(jsonOut,L"problems[0].reason"));
wprintf(L" font: %s\n",CkJsonObjectW_stringOf(jsonOut,L"problems[0].font"));
wprintf(L" missing: %s\n",CkJsonObjectW_stringOf(jsonOut,L"problems[0].chars"));
}
// "apply" with a problem returns false and changes nothing.
success = CkPdfW_TextReplace(pdf,L"apply",json,jsonOut);
wprintf(L"apply returned %d, applied = %d\n",success,CkJsonObjectW_BoolOf(jsonOut,L"applied"));
// 2. A font that is not embedded and not one of the standard 14.
pdf2 = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf2,L"qa_data/pdf/letter_verdana.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf2));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(pdf2);
return;
}
json2 = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json2,L"labels[0].label",L"Account Number:");
CkJsonObjectW_UpdateString(json2,L"labels[0].value",L"0000099999");
success = CkPdfW_TextReplace(pdf2,L"check",json2,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf2));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(pdf2);
CkJsonObjectW_Dispose(json2);
return;
}
wprintf(L"Verdana, not embedded: ok = %d, reason: %s, font: %s\n",CkJsonObjectW_BoolOf(jsonOut,L"ok"),CkJsonObjectW_stringOf(jsonOut,L"problems[0].reason"),CkJsonObjectW_stringOf(jsonOut,L"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
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkPdfW_Dispose(pdf2);
CkJsonObjectW_Dispose(json2);
}