Search code examples
pdfasp.net-coresyncfusion

Using a "custom" font when creating a PDF with Syncfusion in .NET Core


I am trying to create a PDF it .NET Core, and I am using Syncfusion's PDF library to do it.

According to their docs, I should be able to use the following code:

PdfFont font = new PdfTrueTypeFont(new Font("Arial", 14));

However, the only CTORs I have are:

public PdfTrueTypeFont(Stream fontStream, float size);
public PdfTrueTypeFont(PdfTrueTypeFont prototype, float size);
public PdfTrueTypeFont(Stream fontStream, float size, PdfFontStyle style);

These are the Syncfusion packages I have installed

"Syncfusion.Compression.Portable": "15.1120.0.37",
"Syncfusion.Pdf.Portable": "15.1120.0.37"

I also tried the MVC versions as well, in place of the Portable versions.

I'm using the latest .NET Core, "netstandard1.6"

Am I missing a package, doing something wrong, or is this just not supported in .NET Core yet?

My end game is to be able to write text to a PDF in any of the web-safe fonts () from a .NET Core Library. I also need to have a way to be able to measure the text.


Solution

  • Since PdfTrueTypeFont does not have support for giving font name in .Net Core, you can give .ttf font as Stream to the PdfTrueTypeFont.

    string path = _hostingEnvironment.WebRootPath + "/Font/Arial.ttf";  
    
    Stream fontStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);  
    
    //Create a new PDF true type font.  
    PdfTrueTypeFont tFont = new PdfTrueTypeFont(fontStream, 12,PdfFontStyle.Regular); 
    

    We have created a sample to draw text in PDF with help of both PdfTrueTypeFont and PdfStandardFont. And also, used a variable to measure the size of the given text.

    Please find the sample from the below link:
    http://www.syncfusion.com/downloads/support/directtrac/174333/ze/sample-2079659962

    Please refer the below link for supported features in .Net Core:
    https://help.syncfusion.com/file-formats/pdf/supported-and-unsupported-features