Use TrueType font collections

Fonts
3/14/2014

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:

MeiryoSubfonts.PNG

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:

HelloWorldMeiryo.PNG

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: truetype-fonts-pdf.png