Articles in this section
Category / Section

How to add text in PDF document using the font file from local file system using C# and VB.NET

6 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 text in a PDF document using the font file from the local file system using C# and VB.NET.

Steps to add text in PDF document using the font file from local file system programmatically:

  1. Create a new C# Console application project.

Create a console application project

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.

NuGet package reference

  1. 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 text in a PDF document using the font file from the local file system.

C#

//Create a new PDF document.
PdfDocument document = new PdfDocument();
 
//Add a page to the document.
PdfPage page = document.Pages.Add();
 
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
 
//Provide the path of the local *.ttf file.
PdfFont font = new PdfTrueTypeFont("Arial.ttf", 14);
 
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
 
//Save the document.
document.Save("Output.pdf");
 
//Close the document.
document.Close(true);
 
//This will open the PDF file and the result will be seen in the default PDF Viewer.
Process.Start("Output.pdf");

 

VB.NET

'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument()
 
'Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
 
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
 
'Provide the path of the local *.ttf file.
Dim font As PdfFont = New PdfTrueTypeFont("Arial.ttf", 14)
 
'Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
 
'Save the document.
document.Save("Output.pdf")
 
'Close the document.
document.Close(True)
 
'This will open the PDF file and the result will be seen in default PDF Viewer.
Process.Start("Output.pdf")

 

A complete working sample can be download from CreatePDFSample.zip.

 

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

 

Output document screenshot

 

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.

 

Also see:

Create PDF using a standard font

Add RTL text in PDF document

 

 

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