Aspose.Pdf

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 . 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 . Unicode is also supported with TrueType font.

 

Using TrueType Font

 

Before using a TrueType font in your PDF documents using Aspose.Pdf , 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

Text t1 = new Aspose.Pdf.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.Text = New Aspose.Pdf.Text(sec1, "Courier New font")

 

'Set font name of a specific text segment to courier new

t1.Segments(0).TextInfo.FontName = "Courier New"

 

[JAVA]

 

//Create a text object in the section

Text text1 = new Text(sec1, "Courier New font");

 

//Set font name of a specific text segment to courier new

text1.getSegments().getAt(0).getTextInfo().setFontName("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:

 

 

Note: 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

Text t1 = new Aspose.Pdf.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.Text = New Aspose.Pdf.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

 

[JAVA]

 

//Create a text object in the section

Text t1 = new Text(sec1, "Courier New Bold font");

 

//Set font name of a specific text segment to courier new

t1.getSegments().getAt(0).getTextInfo().setFontName("Courier New");

 

//Set the font to bold

t1.getSegments().getAt(0).getTextInfo().setTrueTypeBold(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#]

 

Pdf pdf = new Pdf();

pdf.IsTruetypeFontMapCached = true;

Aspose.Pdf.Section sec1 = pdf.Sections.Add();

 

//to assign a unicode character by it's coding

Text t1 = new Text(((char)0x25a0).ToString());

t1.Segments[0].TextInfo.FontName = "Times New Roman";

t1.Segments[0].TextInfo.IsUnicode = true;

sec1.Paragraphs.Add(t1);

pdf.Save("Unicode.pdf");

 

[VB.NET]

 

Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf

pdf.IsTruetypeFontMapCached = True

Dim sec1 As Aspose.Pdf.Section = pdf.Sections.Add()

 

'to assign a unicode character by it's coding

Dim t1 As Aspose.Pdf.Text = New Aspose.Pdf.Text((Microsoft.VisualBasic.ChrW(&H25A0).ToString()))

 

t1.Segments(0).TextInfo.FontName = "Times New Roman"

t1.Segments(0).TextInfo.IsUnicode = True

sec1.Paragraphs.Add(t1)

pdf.Save("VBUnicode.pdf")

 

[JAVA]

 

Pdf pdf1 = new Pdf();

Section sec1 = pdf1.getSections().add();

 

// to assign a unicode character by it's coding

Text text1 = new Text(sec1,String.valueOf(((char)0x25a0)));                           

 

text1.getSegments().getAt(0).getTextInfo().setFontName("Times New Roman");

text1.getSegments().getAt(0).getTextInfo().setIsUnicode(true);

sec1.getParagraphs().add(text1);                           

 

FileOutputStream out = new FileOutputStream(new File("Unicode.pdf"));

pdf1.save(out);

 

 

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 , 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.