Embed private font to pdf document via Spire.PDF

Change font can offer more variability to PDF files, now Spire.PDF can allow developers change font of pdf files without install the font to the disk. This article is talk about this realization process.

Below is the screenshot of the uninstalled font DeeDeeFlowers.ttf

Embed private font to pdf document via Spire.PDF

Here are the steps:

Step 1: Create a new blank PDF document.

PdfDocument doc = new PdfDocument();

Step 2: Add a new page to the PDF.

PdfPageBase page = doc.Pages.Add();

Step 3: Create a TrueType font object with DeeDeeFlowers.ttf as parameter

String fontFileName = "DeeDeeFlowers.ttf";
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(fontFileName, 20f);

Step 4: Add text and set property.

page.Canvas.DrawString("Years may wrinkle the skin,\n"
+ " but to give up enthusiasm wrinkles the soul.\n"
+ " Worry, fear, self-distrust bows the heart\n"
+" and turns the spirit back to dust.", trueTypeFont, new PdfSolidBrush(Color.Black), 10, 10);

Step 5: Save and review.

doc.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");

Here is the screenshot of result.pdf.

Embed private font to pdf document via Spire.PDF

Full Code:

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
String fontFileName = "DeeDeeFlowers.ttf";
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(fontFileName, 20f);
page.Canvas.DrawString("Years may wrinkle the skin,\n"
+ " but to give up enthusiasm wrinkles the soul.\n"
+ " Worry, fear, self-distrust bows the heart\n"
+" and turns the spirit back to dust.", trueTypeFont, new PdfSolidBrush(Color.Black), 10, 10);
doc.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");