- Add Long Term Validation (LTV) data to an existing signature
- How to downscale all images in a PDF
- How to generate and export certificates
- How to downscale all images in a PDF
- Add Stamp to PDF
- How to reduce PDF file size
- Highlight fields in PDF
- Add a note to PDF
- Resize PDF pages
- Extract glyph boxes from PDF
- Use TrueType font collections
- Layout text with MultilineTextShape
- Merge PDF files in C# .NET
- How do I extract page destinations from bookmarks?
- Clip PDF page content in C#
- Fill PDF form
- Extract glyphs and sort by reading order
- Add bookmarks to PDF
- How to scale content of PDF
- Create rectangles with rounded corners
- Create text with decorations
- TIFF to PDF C#
- Crop content on a PDF page
- How to embed files in a PDF document
- Remove graphics from PDF
- Change the color inside a PDF
- Import FDF into PDF
- Flatten PDF form
- Digitally sign a PDF form in C# or VB.NET
- Vector graphics in PDF
- Translate PDF page content
- Extract graphics from PDF
- Determine the content bounding box
- Search text in PDF
- Convert PDF to plain text
- Flatten Markup Annotation
- Add text field to PDF
- Extract embedded files from PDF
- Extract images from PDF
- Add a Diagonal Watermark to PDF in C#
- Fit image to PDF page
- Add simple html text to PDF
- Add multiline text to a PDF document
- Add single-line text to PDF
- Create a new digitally signed PDF document
- PDF Viewer Preferences
- Change page orientation PDF
- Split PDF pages in C# and VB.NET
- Append two or more existing PDF files
- Determine if a PDF only contains images
- Add footer to PDF
- Convert SVG to PDF
- Add hyperlink to PDF
- Rotate a PDF page
- Change the formatting of a numeric field
- How to mirror PDF pages and other shapes
- Fill in a template PDF document
- How to add autosized text to PDF
- Create formfields in PDF documents
- Export FDF from PDF form
- Add a link with an internal destination to PDF
- Remove PDF security settings
- Add a link to PDF with an external destination
- How to sign and verify updates to a PDF document
- Embed TrueType font
- How to create a tiling for shapes in PDF
- EMF to PDF as vector image
- Replace field with image
- Add a rubber stamp annotation with a custom icon
- Create a text annotation in PDF with rich text
- Read and write meta data from PDF
- Create a custom signature handler to sign and verify PDF documents
- Download and convert image to PDF
- Add barcodes to PDF
- Tagged PDF
- PDFKit.NET 5.0 - detailed changes to the API
- Create tagged PDF
- PDFKit.NET 5.0 and .NET Core
- PDFKit.NET 5.0 and Xamarin
- Dynamic XFA
- PDFKit.NET 5.0 .NET Standard API
- .NET Core console app on MacOS
- Add tags to existing PDF
- Read PDF tags
- Merge XDP data with dynamic XFA form
- Fill XFA form and export XDP data
- Fill and save dynamic XFA form
- Use PDFKit.NET 5.0 with a Xamarin.Forms app
- Use multiple licenses
- Licensing and .NET Standard
- Reduce PDF size
- Disable submit button after submitting
- Write Document to HttpResponse
Use TrueType font collections
This article describes how to select a font from a fontcollection and how this is embedded in a PDF document
Select a font from a fontcollection
Truetype fonts are usually stored in a file that is named like “arial.ttf”. In windows these files can be found in C:\Windows\Fonts”. Alongside these files you can also find font collections, these are named like “meiryo.ttc” and they contain a collection of truetype fonts.
To use such a font in a PDF, you must know 2 things; the name of the fontcollection and the name of the font itself. The first one is easy to obtain, to see which collections are installed you can execute:
dir c:\windows\fonts\*.ttc
This will give you the installed fontcollections:
![TTCfonts.PNG]
The second one, the font name, can also easily be obtained using PDFKit.NET:
``` csharp
var meiryoPath = @"C:\windows\fonts\meiryo.ttc";
var meiryoCollection = Font.GetTrueTypeFontNames(meiryoPath);
Console.WriteLine(meiryoPath);
foreach (string ttfFontName in meiryoCollection)
{
Console.WriteLine("\t" + ttfFontName);
}
Which will result in:
Now we know both the file containing the font collection and the name of the font we can use these, e.g. to create a textShape:
var meiryoPath = @"C:\windows\fonts\meiryo.ttc";
var meiryoCollection = Font.GetTrueTypeFontNames(meiryoPath);
var font = Font.TrueType(meiryoPath, meiryoCollection[0]);
var text = "Hello world in meiryo ハローワールド";
var textShape = new TextShape(50, 100, text, font, 20.0);
shapes.Add(textShape);
And you will get this:
Embed glyphs from a font
If a font is installed on one computer that creates the PDF, it does not necessarily mean that the same font is installed on the computers that dispays the PDF. This is where the embedding of fonts will kick in. This is a mechanism that embeds the used glyphs in the created PDF. Note the term ‘used glyphs’ here, as you can see in the listing of the “*.ttc” fonts above the size of the meiryo collection is more than 9Mbytes. It is not a recommended way of working to to include all used glyphs of all used fonts into each PDF document, they will become unusable large that way. (Note that there are also the default fonts that are considered to be always available).
Only embedding the used glyphs is the default behaviour of PDFKit.NET, and in normal situations you will not want to change this behaviour. However there are cases in which you may want to change this, for instance in case of an user-editable field in which all used glyphs are not known beforehand. Therefore you can change the embedding of glyphs using this construction:
// EmbedMode:
// Auto: The embed mode will be determined based on how the font is used.
// If the font is used to render static text only, then the subset will be
// embedded. If the font is associated with an editable field, then the full
// font will be embedded. This is the default embed mode of a newly created
// TrueType font.
// ReferenceOnly: The font is not embedded. Only a reference is stored inside the PDF.
// Subset: A subset of the font is embdedded inside the PDF.
// Full: he font is fully embedded inside the PDF.
var font2 = Font.TrueType(meiryoPath, meiryoCollection[0]);
font2.EmbedMode = EmbedMode.Subset;
font2.AddToSubset(new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
This way a subset of the font is added to the PDF, including the used glyphs and also the glyphs for the digits 0…9. The latter may be usefull if a the user is expected to fill in a a number in a field.
All the code you may need:
Document doc = new Document();
Page page = new Page(PageSize.A4);
doc.Pages.Add(page);
ShapeCollection shapes = new ShapeCollection(100, 100, 400, 400);
page.Overlay.Add(shapes);
// Get the font "MS-PGothic" from the collection "MSGothic"
// Embed a part of it in the resulting PDF: only the used glyphs, not the entire font
Font font1 = Font.TrueType(@"C:\windows\fonts\msgothic.ttc", "MS-PGothic");
font1.EmbedMode = EmbedMode.Subset;
// create a textShape that will contain some text in MS-PGothic
String text1 = "Hello world in MS PGothic ハローワールド";
TextShape textShape1 = new TextShape(50, 200, text1, font1, 20.0);
shapes.Add(textShape1);
// create another textShape that will contain text in Meiryo
String meiryoPath = @"C:\windows\fonts\meiryo.ttc";
String[] meiryoCollection = Font.GetTrueTypeFontNames(meiryoPath);
Font font2 = Font.TrueType(meiryoPath, meiryoCollection[0]);
font2.EmbedMode = EmbedMode.Subset;
font2.AddToSubset(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
String text2 = "Hello world in meiryo ハローワールド";
TextShape textShape2 = new TextShape(50, 100, text2, font2, 20.0);
shapes.Add(textShape2);
using (FileStream file = new FileStream("../../FontCollections.pdf", FileMode.Create))
{
doc.Write(file);
}
Dim doc As New Document()
Dim page As New Page(PageSize.A4)
doc.Pages.Add(page)
Dim shapes As New ShapeCollection(100, 100, 400, 400)
page.Overlay.Add(shapes)
' Get the font "MS-PGothic" from the collection "MSGothic"
' Embed a part of it in the resulting PDF: only the used glyphs, not the entire font
Dim font1 As Font = Font.TrueType("C:\windows\fonts\msgothic.ttc", "MS-PGothic")
font1.EmbedMode = EmbedMode.Subset
' create a textShape that will contain some text in MS-PGothic
Dim text1 As [String] = "Hello world in MS PGothic ハローワールド"
Dim textShape1 As New TextShape(50, 200, text1, font1, 20.0)
shapes.Add(textShape1)
' create another textShape that will contain text in Meiryo
Dim meiryoPath As [String] = "C:\windows\fonts\meiryo.ttc"
Dim meiryoCollection As [String]() = Font.GetTrueTypeFontNames(meiryoPath)
Dim font2 As Font = Font.TrueType(meiryoPath, meiryoCollection(0))
font2.EmbedMode = EmbedMode.Subset
font2.AddToSubset(New Char() {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c,
"6"c, "7"c, "8"c, "9"c})
Dim text2 As [String] = "Hello world in meiryo ハローワー���ド"
Dim textShape2 As New TextShape(50, 100, text2, font2, 20.0)
shapes.Add(textShape2)
Using file As New FileStream("../../FontCollections.pdf", FileMode.Create)
doc.Write(file)
End Using
This will produce

We have sent an email with a download link.