Editing / replacing text content in PDF.


We are attempting to perform text replacement in a PDF page content
stream and need some advice. Essentially, we're iterating through the
Elements on a page, and when a text element is found we check to see
if it matches our search criteria, and if so, replace it with a new
text string.

We have researched this on your forums and are taking the suggested
approach of creating a new text Element to replace the existing one,
instead of trying to edit the existing one. This is fine, except that
we need to create a new font based on the font used in the existing
text element, since the existing font only contains a subset of
characters. The problem we have is trying to determine the name of the
font to create. If it is a standard PDF font, it's straightforward
since we can use the GetStandardType1FontType() function to determine
which font to create. If, however, it is a non-standard font, we need
to somehow determine the system font name so that we can match that to
the actual font filename, which is then used in the Font.CreateX()
method. Calling the Font.GetName()/Font.GetFamilyName() functions only
returns the ABCDEF+FontName format, and as far as I can tell the
original font name is not stored anywhere in the PDF. Do you have any
suggestions on how to accomplish this?

You can remove 'ABCDEF+' prefix returned by Font.GetName(). Also you
should remove any trailing text following a comma (as in
TimesNewRoman,Bold -> TimesNewRoman).

Something along the following lines:

Sting fn = font.GetName();
int idx = fn.find_first_of('+');
if (idx==6 && fn.size()>7) { // remove font substitution prefix
  fn = fn.substr(idx+1);

// extract name for TrueType fonts with extra styles
idx = fn.find_first_of (',');
if (idx != string::npos) {
  string style = fn.substr(idx+1);
  fn = fn.substr(0, idx);
  if (style == "Bold") {
    is_bold = true;
  else if (style == "Italic") {
    is_italic = true;
  else if (style == "BoldItalic") {
    is_bold = true;
    is_italic = true;

Given the font name you can try to select it as follows:

// In C#
System.Drawing.Font myfont = new System.Drawing.Font(fn, 12);
pdftron.PDF.Font fnt = Font.CreateCIDTrueTypeFont(doc, myfont, true,

Or you can search for the font file yourself instead of relying
on .NET Framework.