Q: I am attempting to search and replace text. I create a new page
and copy the source page to the new and replace text as I go. I create
a new element using ElementBuilder and copy info from the source
element to the new element (see code below). The problem is that if
the new text has any lowercase characters they are overlayed on top of
each other but uppercase looks fine. The font is \'TimesNewRomanPSMT\'
but if I create some other font for the new element then it works.
Element element2 = builder.CreateTextBegin(element.GetGState().GetFont
(), element.GetGState().GetFontSize());
//System.Drawing.Font font = new System.Drawing.Font(\"Time New Roman
\", 1, eStyle); //Element element2 = builder.CreateTextBegin
(pdftron.PDF.Font.CreateTrueTypeFont(m_doc, font, true, true),
element.GetGState().GetFontSize());
writer.WriteElement(element2);
element2 = builder.CreateTextRun(\"fred\"); // \'FRED\' would work
element2.SetTextMatrix(element.GetTextMatrix());
GState gs = element.GetGState();
GState gs2 = element2.GetGState();
gs2.SetCharSpacing(gs.GetCharSpacing());
gs2.SetTransform(gs.GetTransform());
gs2.SetFillColorSpace(gs.GetFillColorSpace());
gs2.SetFillColor(gs.GetFillColor());
gs2.SetStrokeColorSpace(gs.GetStrokeColorSpace());
gs2.SetStrokeColor(gs.GetStrokeColor());
gs2.SetFillOpacity(gs.GetFillOpacity());
gs2.SetStrokeOpacity(gs.GetStrokeOpacity());
gs2.SetWordSpacing(gs.GetWordSpacing());
writer.WriteElement(element2);
writer.WriteElement(builder.CreateTextEnd());
------------------
A: I assume that the problem is caused because you are replacing text
using an existing PDF font. Some PDF creators subset-fonts (remove
unreferenced glyphs) or omit information for glyphs that are not
referenced. In your case it is possible that 'Width' array in the font
dictionary is missing advance widths (or they are 0) for required
lowercase characters.
All of this makes it hard to reuse existing PDF fonts from generic
documents. As a workaround, you could find a substitute font with a
similar name or other characteristics (similar to the commented-out
line in your code). To keep the file size low you could cache this
font (e.g. as a static variable) and reused for all editing operations
throughout the document.