Hi.
I am opening a pdf file from filelocation and drawing header and footer using PdfSharp. The code was working fine. Since yesterday, I started getting this error in the production machine. It was running fine till the day before. It is still running fine in development and test machines.
Here's how I am adding header and footer.
Code:
PdfDocument doc = PdfSharp.Pdf.IO.PdfReader.Open(path);
XFont fontReportHeader = new XFont("Verdana", 16, XFontStyle.Bold);
XFont fontFooterAddress = new XFont("Verdana", 8, XFontStyle.Regular);
XColor myColour = XColor.FromArgb(216, 30, 5);
XPen pen = new XPen(myColour, 1);
Ximage imgLogo = XImage.FromFile(Server.MapPath(@"~/Images/Logo.png"));
foreach (PdfPage page in doc.Pages)
{
XUnit pageWidth = page.Width;
XUnit pageHeight = page.Height;
XUnit headerTop = 25;
XUnit headerBottom = 50;
XUnit footerTop = pageHeight - 50;
XUnit footerBottom = pageHeight - 25;
XLinearGradientBrush brush = new XLinearGradientBrush(new XRect(0, 0, pageWidth, pageHeight), myColour, myColour, XLinearGradientMode.Horizontal);
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
// header
gfx.DrawLine(pen, 15, headerBottom, pageWidth, headerBottom);
gfx.DrawString(reportName, fontReportHeader, brush, 15, 45, XStringFormat.Default); // reportName is a string variable
// footer
gfx.DrawLine(pen, 15, footerTop, pageWidth, footerBottom);
gfx.DrawImage(imgLogo, 15, footerTop + imgLogo.PointHeight + 2, imgLogo.PointWidth, imgLogo.PointHeight);
gfx.DrawString(address, fontFooterAddress, brush, 15, footerTop + imgLogo.PointHeight + 2, XStringFormat.TopLeft); // address is string variable
}
doc.Save(path);
Now, I am logging the expection and get this is what I get.
Error: System.InvalidOperationException: Internal error. Font data could not retrieved.
at PdfSharp.Fonts.OpenType.FontData.CreateGdiFontImage(XFont font, XPdfFontOptions options)
at PdfSharp.Fonts.OpenType.FontData..ctor(XFont font, XPdfFontOptions options)
at PdfSharp.Fonts.OpenType.OpenTypeDescriptor..ctor(XFont font, XPdfFontOptions options)
at PdfSharp.Fonts.FontDescriptorStock.CreateDescriptor(XFont font)
at PdfSharp.Drawing.XFont.get_Metrics()
at PdfSharp.Drawing.XFont.Initialize()
at GenerateReport.ibtnGeneratePdf_Click(Object sender, EventArgs e)
The error doesn't give the line number where error occurs but it looks like it should be while creating a new instance of XFont.
Have you guys ever come across such problem? What might be the cause of it and what would be the solution to fix it?
Thanks.