I am using the MigraDoc DLLs (WPF build) for generating a pdf and I have added the code given below that does the work of adding a new private font:
XPrivateFontCollection pfc = XPrivateFontCollection.Global;
Uri myuri = new Uri(Server.MapPath("/Assets/Rupee_Foradian.ttf"));
pfc.Add(myuri, "./#Rupee Foradian");
The PDF generates successfully with the font loading properly and displaying as expected, but when I try for the second time to create the PDF I get an ArgumentException: An entry with the specified family name already exists
Can anyone please help me out on this issue?
As the name XPrivateFontCollection.Global
implies the font collection is global and exists only once.
Your code should add the font only once (for the first document) and not every time you create a document.
Edit: To execute code only once you can use another global variable (e.g. a static class member):
static bool _privateFontsInstalled;
private static void LoadPrivateFonts()
{
if (!_privateFontsInstalled)
{
try
{
Uri uri = new Uri("pack://application:,,,/");
PdfSharp.Drawing.XPrivateFontCollection.Add(uri, "...");
PdfSharp.Drawing.XPrivateFontCollection.Add(uri, "...");
PdfSharp.Drawing.XPrivateFontCollection.Add(uri, "...");
_privateFontsInstalled = true;
}
catch
{
Debug.Assert(false);
}
}
}