Aspose.Pdf

Fonts embedding while creating Pdf

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.

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

Pdf pdf1 = new Pdf();

 

//Create a section in the Pdf object                      

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

 

//Create a text paragraph inheriting text format settings from the section

Text text1 = new Text(sec1);

 

//Add the text paragraph to the section

sec1.Paragraphs.Add(text1);

 

//Create a text segment

Segment s1 = new 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 Pdf = New Pdf()

 

‘Create a section in the Pdf object                      

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

 

‘Create a text paragraph inheriting text format settings from the section

Dim text1  As Text = New Text(sec1)

 

‘Add the text paragraph to the section

sec1.Paragraphs.Add(text1);

 

‘Create a text segment

Dim s1 As Segment = New 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;

 

Font Embedding while Word2Pdf Conversion
 

While converting a Word document containing custom font information into Pdf, we must call SetUnicode(); in order to set all fonts in the doucment to unicode and this method should be called before saving the Pdf file. Please refer to following code snippet.

 

[C#]

// New a Doc object.

Document doc = new Document(@"C:\my.doc");

 

// Save the doc in a xml fille that can be handled by Aspose.Pdf.

doc.Save(@"C:\ my.xml", SaveFormat.AsposePdf);

 

// New a pdf object

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

 

// Bind named xml file to read the contents from Aspose.Pdf.Xml format into Aspose.Pdf.

pdf.BindXML(@"C:\pdftest\1\my.xml", null);

 

//pdf.IsTruetypeFontMapCached = true;

//pdf.TruetypeFontMapPath = @"C:\\temp";

// Call the function to convert all fonts to Unicode

pdf.SetUnicode();

 

// Save the result

pdf.Save(@"C:\pdftest\1\my.pdf");

 

[VB.NET]

‘New a Doc object.

Dim doc As Document = New Document("MyDocument.doc")

 

‘Save the doc in a xml fille that can be handled by Aspose.Pdf.

doc.Save("MyDocument.xml", SaveFormat.AsposePdf)

 

‘New a pdf object

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

 

'Bind named xml file to read the contents from Aspose.Pdf.Xml format into Aspose.Pdf.

pdf.BindXML("MyDocument.xml", Nothing)

 

// Call the function to convert all fonts to unicode

pdf.SetUnicode();

 

'Produce the PDF file.

pdf.Save("MyDocument.pdf")

 

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.
 

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 IsUnicode=true.

[C#]

//Instantiate a pdf document

Pdf pdf1 = new Pdf();

 

//Create a section in the pdf document

Section sec1 = pdf1.Sections.Add();

 

//Create string variables with text containing html tags

string s = "<html><body><font isUnicode='true' face='Almonte Snow' 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

Text t1 = new 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");    

* notice that ‘Almonte Snow’ is the custom font installed over the system.

 

[VB.NET]

‘Instantiate a pdf document

Dim pdf As Pdf = New Pdf()

 

‘Create a section in the pdf document

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

 

‘Create string variables with text containing html tags

Dim s As String = "<html><head></head><body><font isUnicode='true' face='Almonte Snow' 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 Text = New 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")