Using the below packages:
<package id="PDFsharp-MigraDoc-gdi" version="1.50.4000-beta3b" targetFramework="net45" />
I am using PdfSharp apis, not the MigraDoc. I am form filling a pdf based on a template. I am replacing iTextSharp with PDFSharp.
The textboxes are filled in correctly, but when viewing the pdf in Adobe, the textbox font is smaller (and may be a different font). Then once I set focus to the textbox does it change to the correct font, or to a more preferable font. When I added the below, it fixes the behavior of focus/not focus, and the font is correct.
Code:
if (form.Elements.ContainsKey(PdfAcroForm.Keys.NeedAppearances))
{
form.Elements[PdfAcroForm.Keys.NeedAppearances] = new PdfSharp.Pdf.PdfBoolean(true);
}
else
{
form.Elements.Add(PdfAcroForm.Keys.NeedAppearances, new PdfSharp.Pdf.PdfBoolean(true));
}
However, when it is opened in Adobe, Adobe modifies it to remove it and requires save on close, even if just viewing it. A post for iTextSharp mentioned having to do this as well.
Code:
form.Elements.Remove("/NeedAppearances");
But, then it continues to have same behavior on focus/not focus.
Using a tool PdfExplorer to compare the adobe prompted/saved version, with the removed "NeedAppearances" version, it has added base fonts to AcroForms -> DR -> Font for "TimesNewRomanPSMT" and "TimesNewRoman". I do not see any other changes at this time between the two versions. iTextSharp also seems to have this, though the structure is not quite the same. In the case of Adobe, it looks like it is embedding it and making the files size increase an significant amount, because there is a FontFile element. With iTextSharp, it does not look to be embedded.
So, in order to add the base fonts using PDFSharp, do I need to do something like this or is there an easier way? I have been seeing references to FontResolver, but it is described to use it for WPF or if it is coming from a non-installed font, which neither is the case. I would figure something like PdfTrueTypeFont would be available as PdfItem, but even PdfTrueTypeFont is internal.
Code:
PdfDictionary dr = form.Elements.GetDictionary("/DR");
PdfDictionary fonts = dr.Elements.GetDictionary("/Font");
if (fonts == null)
{
fonts = new PdfDictionary();
dr.Elements.Add("/Font", new PdfDictionary());
}
if (!fonts.Elements.ContainsKey("/TimesNewRomanPSMT"))
{
//var timeNewRoman = new PdfDictionary();
//timeNewRoman.Elements.Add("/LastChar", new PdfInteger(255));
//timeNewRoman.Elements.Add("/Encoding", new PdfString("/WinAnsiEncoding"));
//timeNewRoman.Elements.Add("/BaseFont", new PdfString("/TimesNewRoman"));
//timeNewRoman.Elements.Add("/Type", new PdfString("/Font"));
//var fontDescriptor = new PdfDictionary();
//timeNewRoman.Elements.Add("/FontDescriptor", new PdfFontDescriptor()("/Font"));
//timeNewRoman.Elements.Add("/Subtype", new PdfString("/TrueType"));
//timeNewRoman.Elements.Add("/Widths", new PdfArray());
//timeNewRoman.Elements.Add("/FirstChar", new PdfInteger(0));
//// fonts.Elements.Add("/TimesNewRomanPSMT", timeNewRoman);
}
EDIT:
Thanks,