Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old


Replaces text on the page.

Component Version macOS Windows Linux Server iOS SDK
DynaPDF 14.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "DynaPDF.Parser.ReplaceSelText"; PDF { ; Text } )   More


Parameter Description Example Flags
PDF The PDF reference. $pdf
Text The new text.
Can be empty to replace text with nothing.


Returns OK or error.


Replaces text on the page.
The function replaces or deletes the text that was found by DynaPDF.Parser.FindText. Coordinates of surrounding text are not changed, this means that the new text can overlap surrounding text if the width of the new text is larger than the original text.

So, text replacement has its limitations since at some point text must might be new formatted or re-aligned. However, replacing placeholders with text is usually no problem as long as there is enough room for the new text.

Note that placeholders should not contain space characters since spaces are often not stored in PDF files and this can lead to issues finding the text.

Font substitution
Text replacement depends on the availability of the fonts which are used in a PDF file. If the original font is not available the function loads an alternate font that matches the characteristics of the original font as close as possible. However, font substitution is not perfect and a substituted font looks sometimes more different as expected.

To improve text replacement it is possible to set one or more alternate fonts which should be used if the original font cannot be found on the system. Alternate fonts can be loaded with DynaPDF.Parser.SetAltFont. However, a font loaded by DynaPDF.SetFont, DynaPDF.SetFontEx or DynaPDF.SetCIDFont works too. In order to activate a font loaded by a regular font loading function call DynaPDF.Parser.ChangeAltFont.

It is possible to load more than one alternate font but only the active font will be used when replacing text. If more than one font must be loaded, store the handle returned by DynaPDF.Parser.SetAltFont and change the font with DynaPDF.Parser.ChangeAltFont whenever needed.


Perform find and replace:

# initialize parser
Set Variable [ $OptimizeFlags ; Value: 0 ]
Set Variable [ $r ; Value: MBS( "DynaPDF.Parser.Create"; $pdf; $OptimizeFlags ) ]
Set Variable [ $r ; Value: MBS( "DynaPDF.Parser.SetAltFont"; $pdf; "Helvetica"; 0; 12) ]
# Now parse a page
Set Variable [ $r ; Value: MBS("DynaPDF.Parser.ParsePage"; $pdf; 1; "EnableTextSelection") ]
If [ MBS("IsError") ]
    Show Custom Dialog [ "Failed to parse page" ; $r ]
    # Run a find
    Set Variable [ $continueFind ; Value: 0 ]
    Set Variable [ $r ; Value: MBS( "DynaPDF.Parser.FindText"; $pdf; DynaPDF Replace Text::SearchText; "CaseInsensitive"; $continueFind) ]
    If [ $r = 1 ]
        Loop [ Flush: Always ]
            # Do the replace
            Set Variable [ $r ; Value: MBS( "DynaPDF.Parser.ReplaceSelText"; $pdf; DynaPDF Replace Text::ReplaceText) ]
            # Continue search
            Set Variable [ $continueFind ; Value: 1 ]
            Set Variable [ $r ; Value: MBS( "DynaPDF.Parser.FindText"; $pdf; DynaPDF Replace Text::SearchText; "CaseInsensitive"; $continueFind) ]
            Exit Loop If [ $r ≠ 1 ]
        End Loop
        # Save changes back
        Set Variable [ $r ; Value: MBS( "DynaPDF.Parser.WriteToPage"; $pdf; $OptimizeFlags) ]
    End If
End If

See also

Release notes

Example Databases

Blog Entries

This function checks for a license.

Created 23th November 2023, last changed 23th November 2023

DynaPDF.Parser.ParsePage - DynaPDF.Parser.Reset