Changing font associated with existing PDF text content

Q:

We are editing a pre-existing PDF (generated via Reporting Services)
which we would like to embed a font into - effectively replacing the
reference to the font that was used previously with the new embedded
font. Do you have a code snippet where a font is replaced within an
existing document?
---

A:
In PDF format it is not trivial to change font associated with a text
run. The problem is that fonts may be using different encodings so
switching from one font to another could produce garbage output. You
can change font associated with a given 'text run' only if you are
absolutely sure that font encodings match and that all required glyphs
are available in the new font.

To go around this issue, instead of modifying font on the existing
text run, you can replace the entire text run with a new run (created
using ElementBuilder, see ElementEdti sample project) using the new
font and Unicode string returned from old_element.GetTextString().

Q:

As for the compatibility of the fonts, I am not trying to change from
one font to another - I am simply trying to embed a font that is
defined in the document but not embedded (it is a barcode font that is
used to pay bills at the post office).
---
A:
I see, in this case you can embed the missing font and associate it
with an existing PDF font stub as follows:

// Import the font file...
Filters.StdFile embed_file = new StdFile("font.ttf",
StdFile.OpenMode.e_read_mode);
Filters.FilterReader mystm = new FilterReader(embed_file);
SDF.Obj fnt_stm = pdfdoc.CreateIndirectStream(mystm);
fnt_stm.Put("Length1", Obj.CreateNumber(embed_file.FileSize()))

PDF.Font font = ... existing PDF font that is missing font data...
font.GetDescriptor().Put("FontFile2", fnt_stm);