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

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  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.
    DECLARE @pdf int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/quote_subset_font.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    --  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...
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].label', 'Quote #:'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].value', '58314'
    EXEC sp_OAMethod @pdf, 'TextReplace', @success OUT, 'check', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'ok'
    PRINT '58314: ok = ' + @iTmp0

    --  ...but "Q" and "-" are not in the subset (nothing in the quotation's regular text uses them).
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].value', 'Q-58341'
    EXEC sp_OAMethod @pdf, 'TextReplace', @success OUT, 'check', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'ok'
    PRINT 'Q-58341: ok = ' + @iTmp0
    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'ok'
    IF @iTmp0 = 0
      BEGIN
        --  The problem names the font and the characters it cannot draw.

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'problems[0].reason'
        PRINT '    reason: ' + @sTmp0

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'problems[0].font'
        PRINT '    font: ' + @sTmp0

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'problems[0].chars'
        PRINT '    missing: ' + @sTmp0
      END

    --  "apply" with a problem returns false and changes nothing.
    EXEC sp_OAMethod @pdf, 'TextReplace', @success OUT, 'apply', @json, @jsonOut


    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'applied'
    PRINT 'apply returned ' + @success + ', applied = ' + @iTmp0

    --  2. A font that is not embedded and not one of the standard 14.
    DECLARE @pdf2 int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf2 OUT

    EXEC sp_OAMethod @pdf2, 'LoadFile', @success OUT, 'qa_data/pdf/letter_verdana.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @pdf2
        RETURN
      END

    DECLARE @json2 int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json2 OUT

    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'labels[0].label', 'Account Number:'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'labels[0].value', '0000099999'
    EXEC sp_OAMethod @pdf2, 'TextReplace', @success OUT, 'check', @json2, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @pdf2
        EXEC @hr = sp_OADestroy @json2
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'ok'

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'problems[0].reason'

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'problems[0].font'
    PRINT 'Verdana, not embedded: ok = ' + @iTmp0 + ', reason: ' + @sTmp0 + ', font: ' + @sTmp1

    --  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

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut
    EXEC @hr = sp_OADestroy @pdf2
    EXEC @hr = sp_OADestroy @json2


END
GO