Font Handling

Skip to end of metadata
Go to start of metadata
Use PDF Core Fonts
Acrobat reader supports 14 PDF core fonts. Developers don't need to embed these fonts in any PDF file while working with Aspose.Pdf for .NET. These 14 fonts are:
  • Courier
  • Courier-Bold
  • Courier-BoldOblique
  • Courier-Oblique
  • Helvetica
  • Helvetica-Bold
  • Helvetica-BoldOblique
  • Helvetica-Oblique
  • Symbol
  • Times-Bold
  • Times-BoldItalic
  • Times-Italic
  • Times-Roman

If developers need to use any of these fonts in their PDF files using Aspose.Pdf for .NET then only their font name should be assigned as a string to TextInfo.FontName property of the Segment.

The default font is Times-Roman.

The 14 core fonts are shown in the following figure:

Symbol and ZapfDingbats are symbol fonts.

An example is given below to demonstrate the use of Symbol font.

C#
//Instantiate Pdf instance by calling it empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();

//Create a section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();

//Create a text paragraph inheriting text format settings from the section
Aspose.Pdf.Generator.Text text1 = new Aspose.Pdf.Generator.Text(sec1);

//Add the text paragraph to the section
sec1.Paragraphs.Add(text1);

//Create 1st text segment
Aspose.Pdf.Generator.Segment s1 = new Aspose.Pdf.Generator.Segment(((char)183).ToString());

//Set the font name to the TextInfo.FontName property of segment
s1.TextInfo.FontName = "Symbol";

//Add 1st text segment to the text paragraph
text1.Segments.Add(s1);

//Create 2nd text segment
Aspose.Pdf.Generator.Segment s2 = new Aspose.Pdf.Generator.Segment(" the first item");

//Add 2nd text segment to the text paragraph
text1.Segments.Add(s2);

//Save the Pdf
pdf1.Save("Symbol.pdf"); 
 
VB.NET
'Instantiate Pdf instance by calling it empty constructor
Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()

'Create a section in the Pdf object
Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()

'Create a text paragraph inheriting text format settings from the section
Dim text1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(sec1)

'Add the text paragraph to the section
sec1.Paragraphs.Add(text1)

'Create 1st text segment
Dim s1 As Aspose.Pdf.Generator.Segment = New Aspose.Pdf.Generator.Segment(Microsoft.VisualBasic.ChrW(183).ToString())

'Set the font name to the TextInfo.FontName property of segment
s1.TextInfo.FontName = "Symbol"

'Add 1st text segment to the text paragraph
text1.Segments.Add(s1)

'Create 2nd text segment
Dim s2 As Aspose.Pdf.Generator.Segment = New Aspose.Pdf.Generator.Segment(" the first item")

'Add 2nd text segment to the text paragraph
text1.Segments.Add(s2)

'Save the Pdf
pdf1.Save("Symbol.pdf") 
 
XML
 <Pdf xmlns="Aspose.Pdf"> 
     <Section> 
         <Text> 
             <Segment FontName="Symbol">•</Segment> 
             <Segment>the first item</Segment> 
         </Text> 
     </Section> 
 </Pdf>
 
Use PostScript Type1 Fonts
Adobe's PostScript Type 1 fonts are stored in two common formats, .PFA (PostScript Font ASCII) and .PFB (PostScript Font Binary). These contain descriptions of the character shapes, with each character being generated by a small program that calls on other small programs to compute common parts of the characters in the font. In both cases, the character descriptions are encrypted.

Aspose.Pdf for .NET supports all common formats for PostScript Type1 fonts. Let's see in the below sub-topics that how Aspose.Pdf for .NET allows developers to use these fonts.

Font Metrics
In Aspose.Pdf for .NET, two types of font metrics are supported:
  • The platform-independent, AFM (Adobe Font Metrics)
  • The windows-specific, PFM (Printer Font Metrics)

The TextInfo.FontAfmFile and TextInfo.FontPfmFile are used to set font metric files. It is not needed to set both font metrics, AFM and PFM together. If both are set, AFM will be used first. The following example shows how to use PFM.

Code Snippet

C#
 //Create a text object in a section
 Aspose.Pdf.Generator.Text t1 = new Aspose.Pdf.Generator.Text(sec1,"Arial Bold MT"); 

 //Set the font name of a segment in the text object
 t1.Segments[0].TextInfo.FontName = "Arial-BoldMT";

 //Set the PFM file for the text segment 
 t1.Segments[0].TextInfo.FontPfmFile = "_AB_____.PFM"; 

 //Set the font encoding file for the text segment
 t1.Segments[0].TextInfo.FontEncodingFile = @"CP1250.txt"; 

 //Set the font encoding name of the text segment
 t1.Segments[0].TextInfo.FontEncoding = "cp1250"; 
 
VB.NET
'Create a text object in a section
Dim t1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(sec1, "Arial Bold MT")

'Set the font name of a segment in the text object
t1.Segments(0).TextInfo.FontName = "Arial-BoldMT"

'Set the PFM file for the text segment
t1.Segments(0).TextInfo.FontPfmFile = "Font\PFM\_AB_____.PFM"

'Set the font encoding file for the text segment
t1.Segments(0).TextInfo.FontEncodingFile = "CP1250.txt"

'Set the font encoding name of the text segment
t1.Segments(0).TextInfo.FontEncoding = "cp1250"   
 
XML
 <Text> 
    <Segment FontName="Arial-BoldMT" FontEncodingFile="CP1250.txt" 
        FontPfmFile="_AB_____.PFM"         FontEncoding="cp1250"> 
                Arial Bold MT 
    </Segment> 
 </Text> 
 
Font Embedding and Font Outline
Aspose.Pdf for .NET also supports embedding font descriptions into the generated output PDF document. If a font is not embedded in a PDF document, Acrobat will take it from the operating system if available, or construct a substitute font according to the font descriptor in the PDF.

TextInfo.IsFontEmbedded is used to set Font Embedding. If the TextInfo.IsFontEmbedded is set to true then TextInfo.FontOutlineFile property must be set too. For the font outline information, platform-independent PFA (Printer Font ASCII) and the Windows-specific PFB (Printer Font Binary) formats are supported.

The following example shows how to embed font with PFB outline.

C#
 //Create a text object in a section
 Aspose.Pdf.Generator.Text t1 = new Aspose.Pdf.Generator.Text(sec1,"Arial Bold MT"); 

 //Set the font name of a segment in the text object
 t1.Segments[0].TextInfo.FontName = "Arial-BoldMT"; 

 //Set the PFM file for the text segment
 t1.Segments[0].TextInfo.FontPfmFile = "_AB_____.PFM"; 

 //Set the font encoding file for the text segment
 t1.Segments[0].TextInfo.FontEncodingFile = "CP1250.txt"; 

 //Set the font encoding name of the text segment
 t1.Segments[0].TextInfo.FontEncoding = "cp1250";

 //Set the font outline file for the text segment
 t1.Segments[0].TextInfo.FontOutlineFile = "_AB_____.PFB"; 

 //Set IsFontEmbedded to true
 t1.Segments[0].TextInfo.IsFontEmbedded = true; 
 
VB.NET
'Create a text object in a section
Dim t1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(sec1, "Arial Bold MT")

'Set the font name of a segment in the text object
t1.Segments(0).TextInfo.FontName = "Arial-BoldMT"

'Set the PFM file for the text segment
t1.Segments(0).TextInfo.FontPfmFile = "_AB_____.PFM"

'Set the font encoding file for the text segment
t1.Segments(0).TextInfo.FontEncodingFile = "CP1250.txt"

'Set the font encoding name of the text segment
t1.Segments(0).TextInfo.FontEncoding = "cp1250"

'Set the font outline file for the text segment
t1.Segments[0].TextInfo.FontOutlineFile = "_AB_____.PFB"

'Set IsFontEmbedded to true
t1.Segments[0].TextInfo.IsFontEmbedded = true
 
XML
 <Text> 
    <Segment FontName="Arial-BoldMT" FontEncodingFile="CP1250.txt"
        FontPfmFile="_AB_____.PFM"         FontEncoding="cp1250"
        FontOutlineFile="_AB_____.PFB" IsFontEmbedded="true">
                Arial Bold MT 
    </Segment> 
 </Text> 
 
PostScript Font Names
When using PostScript fonts, you MUST set correct font name. If you don't know the exact font name, you should open the font metric or outline files with a text editer and find the font name. The following figures show how to find font name in AFM, PFB and PFB files:
Figure 1: Finding font name in AFM file
Figure 2: Finding font name in PFM file
Figure 3: Finding font name in PFB file
PostScript font name may differ substantially from the Windows font menu name.
Use TrueType Fonts
TrueType is an outline font standard originally developed by Apple Computer in the late 1980s as a competitor to Adobe's Type 1 fonts used in PostScript. The primary strength of TrueType is that it offers font developers a high degree of control over precisely how their fonts are displayed, right down to particular pixels, at various font heights.

TrueType font is fully supported in Aspose.Pdf for .NET. It is endorsed to use TrueType font instead of PostScript font due to the fact that some types of PostScript fonts are not supported in Aspose.Pdf for .NET. Unicode is also supported with TrueType font.

Using TrueType Font
Before using a TrueType font in your PDF documents using Aspose.Pdf for .NET, it is required for that TrueType font to be already installed in your system. A simple way of installing a TrueType font is to copy the font file to the System's Fonts directory.

When the TrueType font has been installed in your system, you need only to set the font name to the TextInfo.FontName property of any Text Segment.

Code Snippet

C#
 //Create a text object in the section
 Aspose.Pdf.Generator.Text t1 = new Aspose.Pdf.Generator.Text(sec1,"Courier New font"); 

 //Set font name of a specific text segment to courier new
 t1.Segments[0].TextInfo.FontName = "Courier New"; 
 
VB.NET
'Create a text object in the section
Dim t1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(sec1, "Courier New font")

'Set font name of a specific text segment to courier new
t1.Segments(0).TextInfo.FontName = "Courier New"
 
XML
 <Text> 
     <Segment FontName="Courier New">Courier New font</Segment> 
 </Text>
 
TrueType Font Name
If you don't know the TrueType font name exactly, double-click the font file, the name after the Font Name: is the TrueType font name, as shown in the figure below:
Font name is NOT the name on the first line (that is Courier Type (OpenType)) because sometimes it is not same as the name on the third line.
Bold and Italic
The TextInfo.IsTrueTypeFontBold and TextInfo.IsTrueTypeFontItalic properties are used to set TrueType font to Bold or Italic. These are boolean properties and you can set them to true or false to have the desired font effect.

If you want to set TrueType font to Bold or Italic then first of all, make sure that the font has Bold or Italic version or not. For example, if you want to set Courier New font to Bold, make sure that the Courier New Bold font (COURBD.TTF) is installed in your system or not.

Code Snippet

C#
 //Create a text object in the section
 Aspose.Pdf.Generator.Text t1 = new Aspose.Pdf.Generator.Text(sec1,"Courier New Bold font"); 

 //Set font name of a specific text segment to courier new
 t1.Segments[0].TextInfo.FontName = "Courier New"; 

 //Set the font to bold
 t1.Segments[0].TextInfo.IsTrueTypeFontBold = true;
 
VB.NET
'Create a text object in the section
Dim t1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(sec1, "Courier New Bold font")

'Set font name of a specific text segment to courier new
t1.Segments(0).TextInfo.FontName = "Courier New"

'Set the font to bold
t1.Segments(0).TextInfo.IsTrueTypeFontBold = True
 
XML
 <Text> 
     <Segment FontName="Courier New" IsTrueTypeFontBold="true">
        Courier New Bold font
     </Segment> 
 </Text>
 
Using Unicode
Unicode, a 16-bit character set defined by ISO 10646, is similar to ASCII, used for representing commonly used symbols in a digital form. Unlike ASCII, however, Unicode uses a 16-bit dataspace, and so can support a wide variety of non-Roman alphabets including Cyrillic, Han Chinese, Japanese, Arabic, Korean, Bengali, and so on. Supporting common non-Roman alphabets is of interest to community networks, which may want to promote multicultural aspects of their systems.

When using unicode, you must specify the Font file name. After specifying the Font file name, font name is no longer needed. TextInfo.IsUnicode property is used for this purpose. TextInfo.IsUnicode is a boolean property that can be set to true or false according to the requirement.

Code Snippet

C#
 //Instantiate Pdf instance by calling it empty constructor
 Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
 //Set the property to make your application run faster. It will help load the Truetype font faster
 pdf.IsTruetypeFontMapCached = true;
 //Create a section in the Pdf object
 Aspose.Pdf.Generator.Section sec1 = pdf.Sections.Add();

 //to assign a unicode character by it's coding
 Aspose.Pdf.Generator.Text t1 = new Aspose.Pdf.Generator.Text(((char)0x25a0).ToString());
 //Specify the font face name for first segment in text object
 t1.Segments[0].TextInfo.FontName = "Times New Roman";
 //Specify that first segment in text is UniCode
 t1.Segments[0].TextInfo.IsUnicode = true;
 //Add text to paragraphs collection of section object
 sec1.Paragraphs.Add(t1);
 //Save the PDF document
 pdf.Save("Unicode.pdf");
 
VB.NET
'Instantiate Pdf instance by calling it empty constructor
Dim pdf As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf
'Set the property to make your application run faster. It will help load the Truetype font faster 
pdf.IsTruetypeFontMapCached = True
'Create a section in the Pdf object
Dim sec1 As Aspose.Pdf.Generator.Section = pdf.Sections.Add()

'to assign a unicode character by it's coding
Dim t1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text((Microsoft.VisualBasic.ChrW(&H25A0).ToString()))

'Create a section in the Pdf object
t1.Segments(0).TextInfo.FontName = "Times New Roman"
'Specify that first segment in text is UniCode
t1.Segments(0).TextInfo.IsUnicode = True
'Specify that first segment in text is UniCode
sec1.Paragraphs.Add(t1)
'Save the PDF document
pdf.Save("VBUnicode.pdf")
 
Using TrueType Collection
A TrueType Collection (TTC) is a means of delivering multiple OpenType fonts in a single file structure. TrueType Collections are most useful when the fonts to be delivered together share many glyphs in common. By allowing multiple fonts to share glyph sets, TTCs can result in a significant saving of file space.

For example, a group of Japanese fonts may each have their own designs for the kana glyphs, but share identical designs for the kanji. With ordinary OpenType font files, the only way to include the common kanji glyphs is to copy their glyph data into each font. Since the kanji represent much more data than the kana, this results in a great deal of wasteful duplication of glyph data. TTCs were defined to solve this problem.

Similarly, MSMINCHO.TTC contains two TrueType fonts: MS Mincho and MS PMincho. In Aspose.Pdf for .NET, If you want to use TTC, you should specify the font file name like "c:/windows/fonts/MSMINCHO.TTC,0". The string before the comma is the font collection file name and the number after the comma is the font number. "c:/windows/fonts/MSMINCHO.TTC,0" means the first font in the MS Mincho collection.

A Property Related to TrueType Font
The is a property, Pdf.IsTruetypeFontMapCached, technically designed for Truetype Font.

Truetype font map is a font name to font file name map which is used when using unicode. If unicode is used, setting this property to true can make your application run fast because it will help load the Truetype font fast. If this property is set to true and the system's fonts are changed (for example, new fonts are installed), delete the font map file (TruetypeFontMap.xml) and it will be generated again automatically.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.