I used two fonts in my PDF page: HelveticaNeueLTStd-Roman and HelveticaNeueLTStd-BdIt. The actual font that is embedded in the PDF by PDFSharp.NET is dependent on which font is used first during creation of the PDF; and then only the first font that is used is embedded. The text is rendered using that single font.
I looked through the source code and I found a bug which can be easily fixed. The FontSelector constructor clears styles Bold and Italic when these styles are not available to a font family. However, it does not check if a combination of both styles is supported. Here is the invalid code:
Code:
public class FontSelector
{
/// <summary>
/// Initializes a new instance of PdfFontSelector from an XFont.
/// </summary>
public FontSelector(XFont font)
{
this.name = font.Name;
// Ignore Strikeout and Underline
this.style = font.Style & (XFontStyle.Bold | XFontStyle.Italic);
// Clear styles that are not available as a separate type face to prevent embedding of identical font files
#if GDI && !WPF
if ((this.style & XFontStyle.Bold) == XFontStyle.Bold && !font.FontFamily.IsStyleAvailable(XFontStyle.Bold))
this.style &= ~XFontStyle.Bold;
if ((this.style & XFontStyle.Italic) == XFontStyle.Italic && !font.FontFamily.IsStyleAvailable(XFontStyle.Italic))
this.style &= ~XFontStyle.Italic;
And here is the bug fix I implemented:
Code:
public FontSelector(XFont font)
{
this.name = font.Name;
// Ignore Strikeout and Underline
this.style = font.Style & (XFontStyle.Bold | XFontStyle.Italic);
// Clear styles that are not available as a separate type face to prevent embedding of identical font files
#if GDI && !WPF
if ((this.style & XFontStyle.Bold) == XFontStyle.Bold &&
(this.style & XFontStyle.Italic) == XFontStyle.Italic)
{
if (!font.FontFamily.IsStyleAvailable(XFontStyle.Bold | XFontStyle.Italic))
{
this.style &= ~XFontStyle.Bold;
this.style &= ~XFontStyle.Italic;
}
}
else
{
if ((this.style & XFontStyle.Bold) == XFontStyle.Bold && !font.FontFamily.IsStyleAvailable(XFontStyle.Bold))
this.style &= ~XFontStyle.Bold;
if ((this.style & XFontStyle.Italic) == XFontStyle.Italic && !font.FontFamily.IsStyleAvailable(XFontStyle.Italic))
this.style &= ~XFontStyle.Italic;
}