Articles in this section
Category / Section

How to Embed a Predefined Font into a PDF Document

2 mins read

The Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add embed a predefined font into a PDF document using C# and VB.NET.

Steps to add embed a predefined font into a PDF document:

  1. Create a new C# Console application project.console.pngScreenshot 2024-01-08 150408.png
  2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.
    nuget.png
  3. Include the following namespaces in Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing
  1. Use the following code sample to add embed a predefined font into a PDF document.

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;
// Adding a font that can be used to draw Text.
Font font = new Font("Microsoft Sans Serif", 12f, FontStyle.Regular);
// Provide the path of the local *.ttf file.
PdfFont pdfFont = new PdfTrueTypeFont(font,true);
// 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);

VB.NET

' Create a new PDF document.
Dim document As New PdfDocument()
' Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
' et the graphics object for the page.
Dim graphics As PdfGraphics = page.Graphics
' Adding a font that can be used to draw Text.
Dim font As New Font("Microsoft Sans Serif", 12.0F, FontStyle.Regular)
' Provide the path of the local *.ttf file.
Dim pdfFont As New PdfTrueTypeFont(font, True)
' 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 download from Embed_predefined_font_in_Pdf.zip.

By executing the program, you will get the output document as follows,output.png

Take a moment to peruse the documentation. You can find options like add text using standard fonts, system installed fonts, and CJK fonts. Add Unicode text to PDF document, Draw RTL text, Add HTML styled text, create multi-column text, insert RTF text, add ordered or unordered list, replace 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 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 trail message.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied