How to Use Custom Fonts in a PDF
The Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add custom fonts in a PDF by using the PdfTrueTypeFont class available under the Syncfusion.Pdf.Graphics namespace. You have to initialize the PdfTrueTypefont with the input font file. Refer to the following code example.
Steps to add custom fonts into a PDF document
- Create a new C# Console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing
- Add custom fonts to a PDF document using the following code sample.
C#
// Create a new PDF document.
PdfDocument document = new PdfDocument();
// Add a page to the document.
PdfPage page = document.Pages.Add();
// Get the graphics object for the page.
PdfGraphics graphics = page.Graphics;
// Loading the custom TTF font.
PdfFont pdfFont = new PdfTrueTypeFont(@"../../ARIALUNI.ttf", 24);
// Draw the text.
graphics.DrawString("Hello World!", pdfFont, PdfBrushes.Black, new PointF(10, 10));
// Save the document to a file.
document.Save("Output.pdf");
// Close the document.
document.Close(true);
' Create a new PDF document.
Dim document As New PdfDocument()
' Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
' Get the graphics object for the page.
Dim graphics As PdfGraphics = page.Graphics
' Loading the custom TTF font.
Dim pdfFont As New PdfTrueTypeFont("..\..\ARIALUNI.ttf", 24)
' Draw the text.
graphics.DrawString("Hello World!", pdfFont, PdfBrushes.Black, New PointF(10, 10))
' Save the document to a file.
document.Save("Output.pdf")
' Close the document.
document.Close(True)
A complete working sample can be downloaded from Custom_font_in_Pdf.zip.
By executing the program, you will get the output document as follows:
Take a moment to peruse the documentation. You can find options like adding text using standard fonts, system-installed fonts, and CJK fonts, Adding Unicode text to PDF documents, Drawing RTL text, Adding styled text, creating multi-column text, inserting RTF text, adding ordered or unordered lists, replacing fonts in an existing PDF and draw text using OTF.
Refer to this to explore a rich set of Syncfusion Essential PDF features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or the NuGet feed, include a license key in your product. Refer to this link to learn about generating and registering the Syncfusion license key in your application to use the components without a trail message.