How to work with PDF font and text in C#
This article will explain how to work with PDF font and text using C#, such as embed TTF font, get the font information of text, create text with multiple font styles, extract text, find and highlight text and get the coordinate of text in PDF.
Background
We are working on a project and are requested to embed some TTF fonts into PDF, the trouble is the TTF fonts can't be installed on our system, we have to embed them directly from the TTF font files and use them to create text in PDF. We searched the internet and finally found Free Spire.PDF which works fine for us and easy to use. Then we decide to evaluate it further more and share our solution here for guys who suffer the same problem with us.
Introduction
In this article, we'll describe how to work with PDF font and text by using a free library – Free Spire.PDF for .NET. In the following contents, we'll explain six common scenarios:
• Embed font from a TTF font file into PDF
• Get the font information of text in PDF
• Create text with multiple font styles in PDF
• Extract text from PDF
• Find and highlight text in PDF
• Get the coordinate of text in PDF
Preparations
Create a project, download Free Spire.PDF for .NET Package from NuGet and then add the Spire.Pdf.dll reference to the project or install it directly through NuGet Package Manager as shown below.
Using the code
Embed font from a TTF font file
The library provides a class named PdfTrueTypeFont to help us accomplish this task conveniently. With this class, you can also create some text with non-English characters, such as Asian characters.
//Create a pdf document and add a page
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//Get the TTF font file path
String fontFile = @"E:\Program Files\Xerox Serif Wide Bold.ttf";
//Create font with the TTF font file
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(fontFile, 20f);
//Draw text
page.Canvas.DrawString("Embedding font from TTF font file", trueTypeFont, new PdfSolidBrush(Color.Black), 10, 10);
doc.SaveToFile("TTF Font.pdf");
Screenshot
Get the font information of text
Use the UsedFonts property of the PdfDocument class to access the used fonts of the PDF file, and then print out the font name, size, type and style.
//Load the PDF file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"E:\Program Files\PDF.pdf");
//Access the used fonts
PdfUsedFont[] font = doc.UsedFonts;
//Get the font name, size, type and style
foreach (PdfUsedFont f in font)
{
Console.WriteLine("{0}, {1}, {2}, {3}", f.Name, f.Size, f.Type, f.Style);
}
Console.ReadKey();
Screenshot
Create text with multiple font styles
//Create a pdf document and add a page
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins());
//Create fonts with different styles
PdfFont font1 = new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Italic | PdfFontStyle.Underline);
PdfFont font2 = new PdfFont(PdfFontFamily.Courier, 10f, PdfFontStyle.Bold | PdfFontStyle.Strikeout);
PdfFont font3 = new PdfFont(PdfFontFamily.TimesRoman, 10f, PdfFontStyle.Bold | PdfFontStyle.Underline);
PdfSolidBrush brush1 = new PdfSolidBrush(Color.Blue);
PdfSolidBrush brush2 = new PdfSolidBrush(Color.Gray);
PdfSolidBrush brush3 = new PdfSolidBrush(Color.Green);
//Draw text
page.Canvas.DrawString("Italic and underline", font1, brush1, new PointF(10, 10));
page.Canvas.DrawString("Bold and strikeout", font2, brush2, new PointF(10, 40));
page.Canvas.DrawString("Bold and underline", font3, brush3, new PointF(10, 70));
doc.SaveToFile("Format Text.pdf");
Screenshot
Extract text
//Load the PDF file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"E:\Program Files\Sample.pdf");
//Extract text to a TXT file
StringBuilder s = new StringBuilder();
foreach (PdfPageBase page in doc.Pages)
{
s.AppendLine(page.ExtractText());
}
File.WriteAllText("Extract text.txt", s.ToString());
Screenshot
Find and highlight text
//Load the PDF file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"E:\Program Files\Sample.pdf");
PdfTextFind[] result = null;
foreach (PdfPageBase page in doc.Pages)
{
//Find text
result = page.FindText("PDF").Finds;
//Highlight text
foreach (PdfTextFind text in result)
{
text.ApplyHighLight();
}
}
doc.SaveToFile("Highlight text.pdf");
Screenshot
Get the coordinate of text
//Load the PDF file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"E:\Program Files\Sample.pdf");
PdfTextFind[] result = null;
foreach (PdfPageBase page in doc.Pages)
{
//Find the text
result = page.FindText("PDF").Finds;
//Get the coordinate of text
foreach (PdfTextFind text in result)
{
PointF p = text.Position;
Console.WriteLine(p);
}
}
Screenshot
Conclusion
In this article, we only evaluated the font and text part, but Free Spire.PDF also supports many rich features like security setting, PDF attachment/image extract, PDF merge/split, metadata update, section and paragraph optimizing, graph/image drawing and inserting, table creation and processing, and importing data etc. You may try it yourself.
Hi,
Thanks for sharing, you explained the things in detail. with screenshots. I just have one question, Free Spire.PDF is available in all versions of nuget? or specific to some nuget version.
can you please let me know, the version of nuget to install If i want to work with this same Spire.PDF dll