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