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:

1 dir c:\windows\fonts\*.ttc

This will give you the installed fontcollections:

The second one, the font name, can also easily be obtained using PDFKit.NET:

1 var meiryoPath = @"C:\windows\fonts\meiryo.ttc"; 2 var meiryoCollection = Font.GetTrueTypeFontNames(meiryoPath); 3 Console.WriteLine(meiryoPath); 4 foreach (string ttfFontName in meiryoCollection) 5 { 6 Console.WriteLine("\t" + ttfFontName); 7 }

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:

1 var meiryoPath = @"C:\windows\fonts\meiryo.ttc"; 2 var meiryoCollection = Font.GetTrueTypeFontNames(meiryoPath); 3 var font = Font.TrueType(meiryoPath, meiryoCollection[0]); 4 var text = "Hello world in meiryo ハローワールド"; 5 var textShape = new TextShape(50, 100, text, font, 20.0); 6 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:

1 // EmbedMode: 2 // Auto: The embed mode will be determined based on how the font is used. 3 // If the font is used to render static text only, then the subset will be 4 // embedded. If the font is associated with an editable field, then the full 5 // font will be embedded. This is the default embed mode of a newly created 6 // TrueType font. 7 // ReferenceOnly: The font is not embedded. Only a reference is stored inside the PDF. 8 // Subset: A subset of the font is embdedded inside the PDF. 9 // Full: he font is fully embedded inside the PDF. 10 var font2 = Font.TrueType(meiryoPath, meiryoCollection[0]); 11 font2.EmbedMode = EmbedMode.Subset; 12 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:

C# code sample

1 Document doc = new Document(); 2 Page page = new Page(PageSize.A4); 3 doc.Pages.Add(page); 4 ShapeCollection shapes = new ShapeCollection(100, 100, 400, 400); 5 page.Overlay.Add(shapes); 6 7 // Get the font "MS-PGothic" from the collection "MSGothic" 8 // Embed a part of it in the resulting PDF: only the used glyphs, not the entire font 9 Font font1 = Font.TrueType(@"C:\windows\fonts\msgothic.ttc", "MS-PGothic"); 10 font1.EmbedMode = EmbedMode.Subset; 11 12 // create a textShape that will contain some text in MS-PGothic 13 String text1 = "Hello world in MS PGothic ハローワールド"; 14 TextShape textShape1 = new TextShape(50, 200, text1, font1, 20.0); 15 shapes.Add(textShape1); 16 17 // create another textShape that will contain text in Meiryo 18 String meiryoPath = @"C:\windows\fonts\meiryo.ttc"; 19 String[] meiryoCollection = Font.GetTrueTypeFontNames(meiryoPath); 20 Font font2 = Font.TrueType(meiryoPath, meiryoCollection[0]); 21 font2.EmbedMode = EmbedMode.Subset; 22 font2.AddToSubset(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }); 23 String text2 = "Hello world in meiryo ハローワールド"; 24 TextShape textShape2 = new TextShape(50, 100, text2, font2, 20.0); 25 shapes.Add(textShape2); 26 27 using (FileStream file = new FileStream("../../FontCollections.pdf", FileMode.Create)) 28 { 29 doc.Write(file); 30 }

VB.NET code sample

1 Dim doc As New Document() 2 Dim page As New Page(PageSize.A4) 3 doc.Pages.Add(page) 4 Dim shapes As New ShapeCollection(100, 100, 400, 400) 5 page.Overlay.Add(shapes) 6 7 ' Get the font "MS-PGothic" from the collection "MSGothic" 8 ' Embed a part of it in the resulting PDF: only the used glyphs, not the entire font 9 Dim font1 As Font = Font.TrueType("C:\windows\fonts\msgothic.ttc", "MS-PGothic") 10 font1.EmbedMode = EmbedMode.Subset 11 12 ' create a textShape that will contain some text in MS-PGothic 13 Dim text1 As [String] = "Hello world in MS PGothic ハローワールド" 14 Dim textShape1 As New TextShape(50, 200, text1, font1, 20.0) 15 shapes.Add(textShape1) 16 17 ' create another textShape that will contain text in Meiryo 18 Dim meiryoPath As [String] = "C:\windows\fonts\meiryo.ttc" 19 Dim meiryoCollection As [String]() = Font.GetTrueTypeFontNames(meiryoPath) 20 Dim font2 As Font = Font.TrueType(meiryoPath, meiryoCollection(0)) 21 font2.EmbedMode = EmbedMode.Subset 22 font2.AddToSubset(New Char() {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, 23 "6"c, "7"c, "8"c, "9"c}) 24 Dim text2 As [String] = "Hello world in meiryo ハローワールド" 25 Dim textShape2 As New TextShape(50, 100, text2, font2, 20.0) 26 shapes.Add(textShape2) 27 28 Using file As New FileStream("../../FontCollections.pdf", FileMode.Create) 29 doc.Write(file) 30 End Using

This will produce: