Free Support Forum - aspose.com

True Type Fonts and Custom Fonts

Dear Customers,

We’re rewriting the features of True Type Fonts and Custom Fonts to greatly simplify their use. You will be able to write xml like this to support any custom fonts:

<Segment FontName=“Pipe-Dream” FontAfmFile=“d:/fonts/PIPED___.AFM”
FontPfmFile="d:/fonts/PIPED___.PFM"
FontOutlineFile=“d:/fonts/PIPED___.PFB”
FontEncoding=“cp1250” FontEncodingFile=“d:/fonts/cp1250.cpg”>
PIPE DREAM

We will also add properties FontPfmFile, FontOutlineFile, FontEncoding and FontEncodingFile in the APIs.

Since this will take aproximately 5 days, it will be available either this weekend or early next week.

I apologize for any inconvenience this delay may cause.

If this delay is a problem for you, please let me know.

Dear Customers,

1. Please check the latest hot fix to support custom fonts in your pdf documents.

2. Like any other features of Aspose.Pdf, custom fonts can be used in XML, API or combined XML and API. You can find the latest xml schema. Sorry the latest API Reference is not available as we’re enhancing xml examples for each class. Please consult the examples below to use custom fonts in your vb/c# code.

3. We use Jonathan Paterson’s Pipe-Dream font to demostrate how to use custom fonts. Below are the examples in Xml, Visual Basic and C#. Please note:

(1). For TrueType fonts, although it is not necessary to assign FontOutlineFile, you need to make sure the machine to view your Pdf documents has installed the necessary TrueType fonts. When you assign the FontName, please choose the Family Name when you double click the TrueType font in the Fonts of the Control Panel, instead of the Full Name displayed at the first line. Generally each TrueType font has two versions: Bold and Italic. You can use IsTrueTypeFontBold and IsTrueTypeFontItalic to choose which version you want to use.

(2). For PostScript custom fonts, you need to assign either FontAfmFile or FontPfmFile. If both are assigned AFM file will be evaluated first. Also you need to assign FontEncoding and FontEncodingFile. Please donwload the commonly used encoding files.

(3). Assign IsFontEmbedded to be true if you want to embed PostScript custom fonts into your pdf documents. Also you need to assign FontOutlineFile.


[Xml]
<?xml version="1.0" encoding="utf-8" ?>




this is text content



<Segment FontSize=“20” FontName=“Arial Narrow”
IsTrueTypeFontBold=“true” IsTrueTypeFontItalic=“true”>
This is TrueType font ‘Arial Narrow Bold Italic’.



<Segment FontSize=“20” FontName=“Pipe-Dream” FontEncoding=“cp1250”
IsFontEmbedded=“true” FontAfmFile=“d:/fonts/PIPED___.AFM”
FontOutlineFile=“d:/fonts/PIPED___.PFB” FontEncodingFile=“d:/fonts/cp1250.cpg”>
CUSTOM PIPE DREAM FONT





[Visual Basic]

Imports Aspose.Pdf

Public Class TextInfoExamples
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim pdf As Pdf = New Pdf()

Dim section As Section = New Section(pdf)
pdf.Sections = New Sections()
pdf.Sections.Add(section)

Dim text1 As Text = New Text(section)
text1.Margin.Top = 30
text1.TextInfo.BackgroundColor = New Aspose.Pdf.Color()
text1.TextInfo.BackgroundColor.ColorSpaceType = ColorSpaceType.Rgb
text1.TextInfo.BackgroundColor.RgbColorSpace = System.Drawing.Color.Beige
section.Paragraphs = New Paragraphs()
section.Paragraphs.Add(text1)

Dim segment1 As Segment = New Segment(text1)
text1.Segments = New Segments()
text1.Segments.Add(segment1)
segment1.Content = “this is text content”
segment1.TextInfo.Alignment = AlignmentType.Center
Dim color1 As Aspose.Pdf.Color = New Aspose.Pdf.Color()
color1.ColorSpaceType = ColorSpaceType.Rgb
color1.RgbColorSpace = System.Drawing.Color.Red
segment1.TextInfo.Color = color1
segment1.TextInfo.FontSize = 16
segment1.TextInfo.IsUnderline = True

'TrueType font example
Dim text2 As Text = New Text(section)
text2.Margin.Top = 30
section.Paragraphs.Add(text2)

Dim segment2 As Segment = New Segment(text2)
text2.Segments = New Segments()
text2.Segments.Add(segment2)
segment2.Content = “This is TrueType font ‘Arial Narrow Bold Italic’.”
segment2.TextInfo.FontSize = 20
segment2.TextInfo.FontName = “Arial Narrow”
segment2.TextInfo.IsTrueTypeFontBold = True
segment2.TextInfo.IsTrueTypeFontItalic = True

'Embeded custom postscript font example
Dim text3 As Text = New Text(section)
text3.Margin.Top = 30
section.Paragraphs.Add(text3)

Dim segment3 As Segment = New Segment(text3)
text3.Segments = New Segments()
text3.Segments.Add(segment3)
segment3.Content = “CUSTOM PIPE DREAM FONT”
segment3.TextInfo.FontSize = 20
segment3.TextInfo.FontName = “Pipe-Dream”
segment3.TextInfo.FontAfmFile = “d:/fonts/PIPED___.AFM”
segment3.TextInfo.FontOutlineFile = “d:/fonts/PIPED___.PFB”
segment3.TextInfo.FontEncodingFile = “d:/fonts/cp1250.cpg”
segment3.TextInfo.FontEncoding = “cp1250”
segment3.TextInfo.IsFontEmbedded = True

pdf.Save(Response)
Response.End()


End Sub

End Class


[C#]

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Aspose.Pdf;

namespace Examples_CS
{
///


/// Summary description for TextInfoExamples.
///

public class TextInfoExamples : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Pdf pdf = new Pdf();

Section section = new Section(pdf);
pdf.Sections = new Sections();
pdf.Sections.Add(section);

Text text1 = new Text(section);
text1.Margin.Top = 30;
text1.TextInfo.BackgroundColor = new Aspose.Pdf.Color();
text1.TextInfo.BackgroundColor.ColorSpaceType = ColorSpaceType.Rgb;
text1.TextInfo.BackgroundColor.RgbColorSpace = System.Drawing.Color.Beige;
section.Paragraphs = new Paragraphs();
section.Paragraphs.Add(text1);

Segment segment1 = new Segment(text1);
text1.Segments = new Segments();
text1.Segments.Add(segment1);
segment1.Content = “this is text content”;
segment1.TextInfo.Alignment = AlignmentType.Center;
Aspose.Pdf.Color color1 = new Aspose.Pdf.Color();
color1.ColorSpaceType = ColorSpaceType.Rgb;
color1.RgbColorSpace = System.Drawing.Color.Red;
segment1.TextInfo.Color = color1;
segment1.TextInfo.FontSize = 16;
segment1.TextInfo.IsUnderline = true;

//TrueType font example
Text text2 = new Text(section);
text2.Margin.Top = 30;
section.Paragraphs.Add(text2);

Segment segment2 = new Segment(text2);
text2.Segments = new Segments();
text2.Segments.Add(segment2);
segment2.Content = “This is TrueType font ‘Arial Narrow Bold Italic’.”;
segment2.TextInfo.FontSize = 20;
segment2.TextInfo.FontName = “Arial Narrow”;
segment2.TextInfo.IsTrueTypeFontBold = true;
segment2.TextInfo.IsTrueTypeFontItalic = true;

//Embeded custom postscript font example
Text text3 = new Text(section);
text3.Margin.Top = 30;
section.Paragraphs.Add(text3);

Segment segment3 = new Segment(text3);
text3.Segments = new Segments();
text3.Segments.Add(segment3);
segment3.Content = “CUSTOM PIPE DREAM FONT”;
segment3.TextInfo.FontSize = 20;
segment3.TextInfo.FontName = “Pipe-Dream”;
segment3.TextInfo.FontAfmFile = “d:/fonts/PIPED___.AFM”;
segment3.TextInfo.FontOutlineFile = “d:/fonts/PIPED___.PFB”;
segment3.TextInfo.FontEncodingFile = “d:/fonts/cp1250.cpg”;
segment3.TextInfo.FontEncoding = “cp1250”;
segment3.TextInfo.IsFontEmbedded = true;

pdf.Save(Response);
Response.End();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

Do TrueType fonts get embedded into the PDF? If not, this is something that should be added to ensure true portability. In addition, how is one supposed to know what type of encoding to use - or is this even neccessary with TrueType fonts?

A suggestion; a top level child nodes to the node where one can define style and fonts that will be used throughout the document. For example, in the xmlpdf product, which I have also evaluated, there is XML which looks like this …

























This allows for, one, the fonts to be loaded at once, and embedded, at the beginning of the document generation process. Two, this allows the generated XML to much less verbose and smaller. For example, one can do something like this …


A Custom Data Feed is a compilation of data feeds to which a Gerson Lehrman Group client subscribes. Within each data feed group are indexes relating to a specific topic. Gerson Lehrman Group continually captures data from The Council of Advisors to update its indexes. Index values should always be considered in conjunction with index volume. The volume measures the number of Council Members that contributed to that particular index value. The index values are means unless otherwise specified. High and low values are dropped from all datasets. Index values with volumes of less that 10 are not reported for that period. Please see the end of this report for other general information relating to the interpretation of these data.


Notice that, by using classes for styles, redundant repeating of attribute in every text-type element is avoided.

Matt

Unfortunatley, this solution doesn’t seem to work for TrueType fonts unless you’re using the default font for a given type face. For example, I’m trying to use Arial and want, for part of the text, to make it italics. I’ve tried several different ways to achieve this. First, if we try something like …

some text

there is no error and the PDF generates fine, but the font never changes to italic. Now, if we look at the C:\Windows\Fonts directory, we’ll see that Arial Italic, while it’s Typeface name does, in fact, equal Arial, the glyphs for the font are kept in a separate file, namely, C:\Windows\Fonts\ARIALI.TTF. If we use …

some text

or

some text

we get runtime exceptions thrown by Aspose.Pdf.dll. I’ve tried a couple of other permutations and every other method, besides the first one, will throw an exception. Typically, this exception is of the form "No font file information for custom font … ".

Matt

Dear Matt,

Thanks for your consideration.

I have tested your example and it works well.

We can use the following code to create a truetype font which will be italic:

System.Drawing.Font font = new System.Drawing.Font(“Arial”,13,System.Drawing.FontStyle.Italic);

Make sure you can run this code and get the correct font object.

In addition, Truetype fonts do not need custom encoding. We will be making some changes to the custom encoding settings. When we are finished, we will provide an explanation on how to use custom encodings.

Wang,

As you can see from the above example, I’m not using the API but, rather, XML for generation purposes. Unless there is something missing from the above XML, or something missing from the documentation you provided earlier in this post, I would still concluded that the hotfix provided doesn’t provide a complete solution for the problem. While it may work by making API calls, this doesn’t really help me.

As an example, this is the XML that I used to test the hotfix …




A?
Custom Data Feed
?is a compilation of data feeds to which a Gerson Lehrman Group client subscribes. Within each data feed group are indexes relating to a specific topic.
Gerson Lehrman Group continually captures data from The Council of Advisors to update its indexes. Index values should always be considered in conjunction with index volume. The volume measures the number of Council Members that contributed to that particular index value. The index values are means unless otherwise specified. High and low values are dropped from all datasets. Index values with volumes of less that 10 are not reported for that period. Please see the end of this report for other general information relating to the interpretation of these data.




This follows very closely the example that was given in this post. I believe if you run this through the BindXML method, you’ll see that the generated PDF, while it loads the Arial Narrow TrueType font, never recognizes the italics in the second segment but rather uses standard Arial Narrow for all three segment nodes.

Matt

Something else I just noticed; while it’s certianly very easy and straightforward to load fonts using System.Drawing.Font, this simply give you a reference to a managed Font object. However, even from the API code listed above, there isn’t really anything you can do with it. In looking through the interface for the Segment object, for example, there isn’t simply a Font accessor where one could assign the loaded Font object. That being said, what good does it do me to load a font object? I’m guessing what you’re doing inside the API itself, is once somebody does something like the following …

Segment seg1 = new Segment(paragraph1);
paragraph1.Segments = new Segments();
paragraph1.Segments.Add(seg1);

Aspose.Pdf.Color color = new Aspose.Pdf.Color();
color.ColorSpaceType = ColorSpaceType.Rgb;
color.RgbColorSpace = System.Drawing.Color.Black;

seg1.TextInfo.Color = color;
seg1.TextInfo.FontName = “Arial Narrow”;
seg1.TextInfo.FontSize = 12;
seg1.TextInfo.IsTrueTypeFontBold = false;
seg1.TextInfo.IsTrueTypeFontItalic = true;

You would do something like the following from within the TextInfo object, for example …

public class TextInfo
{
private Font m_font;



public virtual string FontName
{
get
{
return m_font.Name;
}
set
{
m_font = new Font(value);
}

public virtual float FontSize
{
get
{
return m_font.Size
}
set
{
m_font.Size = value;
}
}

public virtual bool IsTrueTypeBold
{
get
{
return (m_font.Style & FontStyle.Bold != 0)
}
set
{
if (value)
{
m_font.Style = m_font.Style | FontStyle.Bold;
}
}
}

public virtual bool IsTrueTypeItalic
{
get
{
return (m_font.Style & FontStyle.Italic != 0)
}
set
{
if (value)
{
m_font.Style = m_font.Style | FontStyle.Italic;
}
}
}


}

Therefore, unless there was a direct assignment withing the TextInfo object to the inner Font object, it doesn’t really do any good for the client code to load a Font object. However, this still doesn’t address the XML issue.

Matt

Sorry for posting several replies, but I’ve found the problem with the hotfix. What seems to be happening is that whatever font is loaded first for a node is the font, including the FontStyle, that gets used, at least partially, for all nodes in that block (this goes for both the API and XML interfaces). For example, if you insert the following code right after the node is set up and before segment2 (obviously, we don’t repeat newing the Segments collection in the existing segment2 block) …

Segment segment3 = new Segment(text2);
text2.Segments = new Segments();
text2.Segments.Add(segment3);
segment3.Content = “Watch, inserting this segment will cause the second segment’s formatting to get screwed up …”;
segment3.TextInfo.FontSize = 10;
segment3.TextInfo.FontName = “Arial Narrow”;
segment3.TextInfo.IsTrueTypeFontBold = false;
segment3.TextInfo.IsTrueTypeFontItalic = false;

… you’ll see that that segment2 is longer in italic - even though it retains the correct FontSize and is Bold.

Hope this helps fix the problem,

Matt

Dear Matt,

Thanks for your consideration.

We will fix this bug in several days. Thanks for your help!