- Add a link to PDF with an external destination
- Add a link with an internal destination to PDF
- Add a note to PDF
- Add barcodes to PDF
- Add bookmarks to PDF
- Add footer to PDF
- Add simple html text to PDF
- Add hyperlink to PDF
- Add Long Term Validation (LTV) data to an existing signature
- Add multiline text to a PDF document
- Add a rubber stamp annotation with a custom icon
- Add single-line text to PDF
- Add Stamp to PDF
- Add tags to existing PDF
- Add text field to PDF
- Add a Diagonal Watermark to PDF in C# - TallComponents - PDF Library
- pdfkit5 - detailed changes to the API - Tall Components
- Append two or more existing PDF files
- Change the color inside a PDF
- Change the formatting of a numeric field
- Change page orientation PDF
- Clip PDF page content in C#
- .NET Core console app on MacOS
- Convert PDF to plain text
- Convert SVG to PDF
- Create a text annotation in PDF with rich text
- Create formfields in PDF documents
- Create a new digitally signed PDF document
- Create rectangles with rounded corners
- Create tagged PDF
- Create text with decorations
- How to create a tiling for shapes in PDF
- Crop content on a PDF page
- Determine the content bounding box
- Determine if a PDF only contains images
- Digitally sign a PDF form in C# or VB.NET
- Disable submit button after submitting
- How to downscale all images in a PDF
- Download and convert image to PDF
- How to downscale all images in a PDF
- Vector graphics in PDF
- Fill XFA form and export XDP data
- Fill and save dynamic XFA form
- Merge XDP data with dynamic XFA form
- Dynamic XFA
- How to embed files in a PDF document
- Embed TrueType font in PDF
- EMF to PDF as vector image
- Export FDF from PDF form
- Extract embedded files from PDF
- Extract glyph boxes from PDF
- Extract glyphs and sort by reading order
- Extract graphics from PDF
- Extract images from PDF
- Fill in a template PDF document
- Fill PDF form
- Fit image to PDF page
- Flatten Markup Annotation
- Flatten PDF form
- How to generate and export certificates
- How do I extract page destinations from bookmarks?
- Highlight fields in PDF
- How to add autosized text to PDF
- How to sign and verify updates to a PDF document
- Import FDF into PDF
- Licensing and .NET Standard
- Merge PDF files in C# .NET
- How to mirror PDF pages and other shapes
- Layout text with MultilineTextShape
- pdfkit5 and .NET Core
- pdfkit5 .NET Standard API
- Read and write meta data from PDF
- Read PDF tags
- How to reduce PDF file size
- Reduce PDF size
- Remove graphics from PDF
- Remove PDF security settings
- Replace field with image
- Resize PDF pages
- Rotate a PDF page
- How to scale content of PDF
- Search text in PDF
- PDF Viewer Preferences
- Create a custom signature handler to sign and verify PDF documents
- Split PDF pages in C# and VB.NET
- Tagged PDF
- TIFF to PDF C#
- Translate PDF page content
- Use multiple licenses
- Use TrueType font collections
- Write Document to HttpResponse
- Use pdfkit5 with a Xamarin.Forms app
- pdfkit5 and Xamarin
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