Fonts embedding while creating PDF

Skip to end of metadata
Go to start of metadata
If you need to use any font other than the 14 core fonts supported by Adobe Reader, than you must embed the font description while generating Pdf file. If font information is not embedded, Adobe Reader will take it from the Operating System if it’s installed over the system, or it will construct a substitute font according to the font descriptor in the Pdf.
Solution

We use the property IsFontEmbedded to embed the font information into Pdf file. Setting the value of this property to ‘True’ will embed the complete font file into the Pdf, knowing the fact that it will increase the Pdf file size.
Following is the code snippet that can be used to embed the font information into Pdf.


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 a text segment
Aspose.Pdf.Generator.Segment s1 = new Aspose.Pdf.Generator.Segment(" This is a sample text using Custom font");

//Set the font name to the TextInfo.FontName property of segment, where ‘Almonto Snow’ is custom font name
s1.TextInfo.FontName = "Almonte Snow";

// Set the value for property to include the font description into Pdf file
s1.TextInfo.IsFontEmbedded = true;

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

//Save the Pdf
pdf1.Save(@"C:\ Font_Embeded.pdf");
 

VB.NET
'Instantiate Pdf instance by calling it empty constructor
Dim pdf As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()

'Create a section in the Pdf object                       
Dim sec1 As Aspose.Pdf.Generator.Section = pdf.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 a text segment
Dim s1 As Aspose.Pdf.Generator.Segment = New Aspose.Pdf.Generator.Segment (" This is a sample text using Custom font");

'Set the font name to the TextInfo.FontName property of segment, where ‘Almonto Snow’ is custom font name
s1.TextInfo.FontName = "Almonte Snow";

'Set the value for property to include the font description into Pdf file
s1.TextInfo.IsFontEmbedded = true;  

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

'Save the Pdf
pdf1.Save(@"C:\ Font_Embeded.pdf");
 

XML
<?xml version="1.0" encoding="utf-8" ?>
  <Pdf xmlns="Aspose.Pdf">
   <Section>
    <Text> 
            <Segment FontName="Almonte Snow" IsFontEmbedded = "true"> This is a sample text using Custom font</Segment> 
    </Text> 
   </Section> 
  </Pdf>
 

There is another way to embed the font information into the Pdf file by using IsUnicode property. The font subset will be embedded. That means not the complete font file is embedded, but the subset that is used in the PDF is embedded. The file size of the PDF may be smaller than that of complete embedding of font file using IsFontEmbedded .
In the upper specified code simply change one line and use the rest of code as it is.

// Set the value for property to include a subset of font into Pdf file
s1.TextInfo.IsUnicode = true;
 

We can also set the property value pdf1.TextInfo.IsFontEmbedded = true; instead of calling SetUnicode() As a result it will embed the whole font description into resultant Pdf and it may increase the size of Pdf.

Each font has a set of supported characters. Sometimes, users may assign a font to a Segment paragraph which doesn't support every character appear in the Segment. Since the release of Aspose.Pdf 4.0.0 a new feature has been introduced to assign proper font to segments according to its contents. For this sake a property named IsAutoFontAdjusted has been added to Pdf class. If its value is set to true, a proper font to segments is assigned automatically.

Font Embedding when using Text String containing HTML Tags

We often need to display the HTML contents in Pdf in the same HTML format. Therefore we use Text.IsHtmlTagSupported property to accomplish this requirement. While using In-line formatting we may use custom fonts and in order to embed the custom font into resultant Pdf we need to use I sUnicode=true.


C#
//Instantiate a pdf document
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();

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

//Create string variables with text containing html tags
string s = "<html><body><font isUnicode='true' face='Bete Noir NF' size=18><i>Sample text </i>with Custome font Embedded </font><br><font isUnicode='true' face='Courier New' size=10><s>Sample Text </s>in <u>Courier New</u> font</font></body></html>";  

//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text t1 = new Aspose.Pdf.Generator.Text(s);

//Enable the HTML tag support property
t1.IsHtmlTagSupported = true;

//Add the text paragraphs containing HTML text to the section
sec1.Paragraphs.Add(t1);

//Save the pdf document
pdf1.Save("inLineFormated_HtmlSuported.pdf");       
 
VB.NET
'Instantiate a pdf document
Dim pdf As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()

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

'Create string variables with text containing html tags
Dim s As String = "<html><head></head><body><font isUnicode='true' face=' Bete Noir NF' size=18><i>Sample text </i>with Custome font Embedded </font><br><font isUnicode='true' face='Courier New' size=10><s>Sample Text </s>in <u>Courier New</u> font</font></body></html>"

'Create text paragraphs containing HTML text
Dim t1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(s)

'Enable the HTML tag support property
t1.IsHtmlTagSupported = True

'Add the text paragraphs containing HTML text to the section
sec1.Paragraphs.Add(t1)

'Save the pdf document
pdf.Save("inlinehtml.pdf")
 




Fig: Output generated when HTML font is rendered into PDF and font is embedded.

‘Almonte Snow’ is the custom font installed over the system.
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.