Font

Font (5)

Now Spire.Doc supports to embed private fonts from font files into Word document when save as .docx file format. This article will show you the detail steps of how to accomplish this task by using Spire.Doc.

For demonstration, we used a font file DeeDeeFlowers.ttf.

Embed private font into Word document when save as .docx file format

In the following part, we will embed font from above file into a Word document and use it to create text.

Step 1: Create a blank Word document.

Document document = new Document();

Step 2: Add a section and a paragraph to the document.

Section section = document.AddSection();
Paragraph p = section.AddParagraph();

Step 3: Append text to the paragraph, then set the font name and font size for the text.

TextRange range = p.AppendText("Let life be beautiful like summer flowers\n"
    +"Life, thin and light-off time and time again\n"
    + "Frivolous tireless");
range.CharacterFormat.FontName = "DeeDeeFlowers";
range.CharacterFormat.FontSize = 20;

Step 4: Allow embedding font in document by setting the Boolean value of EmbedFontsInFile property to true.

document.EmbedFontsInFile = true;

Step 5: Embed private font from font file into the document.

document.PrivateFontList.Add(new PrivateFontPath("DeeDeeFlowers", @"E:\Program Files\DeeDeeFlowers.ttf"));   

Step 6: Save as .docx file format.

document.SaveToFile("result.docx", FileFormat.Docx);

After running the code, we'll get the following output:

Embed private font into Word document when save as .docx file format

Full code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace Embed_private_font_into_Word
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();           
            Section section = document.AddSection();
            Paragraph p = section.AddParagraph();

            TextRange range = p.AppendText("Let life be beautiful like summer flowers\n"
                +"Life, thin and light-off time and time again\n"
                + "Frivolous tireless");
            range.CharacterFormat.FontName = "DeeDeeFlowers";
            range.CharacterFormat.FontSize = 20;

            document.EmbedFontsInFile = true;
            document.PrivateFontList.Add(new PrivateFontPath("DeeDeeFlowers", @"E:\Program Files\DeeDeeFlowers.ttf"));   

            document.SaveToFile("result.docx", FileFormat.Docx);
        }
    }
}

We have already shown you how to use uninstalled font by font document when converting word to PDF. Now starts from Spire.Doc 5.6.3, Spire.Doc newly supports to set the font styles for the uninstalled fonts when convert word documents to PDF. Here comes to the code snippets of how to set the font styles for embed the uninstalled fonts by font documents:

Note: Before Start, please download the latest version of Spire.XLS and add Spire.xls.dll in the bin folder as the reference of Visual Studio.

Step 1: Create a new workbook and load from file.

Document document = new Document();
document.LoadFromFile("Testing.docx");

Step 2: Create an instance for class ToPdfParameterList named parms.

ToPdfParameterList parms = new ToPdfParameterList();

Step 3: Define the path of the uninstalled fonts.

   {
      new PrivateFontPath("Century Gothic",FontStyle.Regular,"fonts\\GOTHIC.TTF"),
      new PrivateFontPath("Century Gothic",FontStyle.Bold,"fonts\\GOTHICB.TTF"),
      new PrivateFontPath("Century Gothic",FontStyle.Italic,"fonts\\GOTHICI.TTF") ,
      new PrivateFontPath("Century Gothic",FontStyle.Bold|FontStyle.Italic,"fonts\\GOTHICBI.TTF")
                                                
   };

Step 4: Save the document to file and launch to preview it.

document.SaveToFile("Testing.pdf", parms);
System.Diagnostics.Process.Start("Testing.pdf");

Effective screenshot of the embedded uninstalled fonts by setting the font style after converts to PDF:

How to embed uninstalled fonts by font document when convert word to PDF

Now Spire.Doc support using uninstalled font when converting Doc to PDF to diversity text content. In this article, we'll talk about how to realize this function:

Step 1: Download a font uninstalled in system.

How to use uninstalled font when converting Doc to PDF via Spire.Doc

Step 2: Create a new blank Word document.

Document document = new Document();

Step 3: Add a section and create a new paragraph.

Section section = document.AddSection();
Paragraph paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

Step 4: Append text for a txtRange.

TextRange txtRange = paragraph.AppendText(text);

Step 5: Create an example for class ToPdfParameterList named to pdf, and create a new PrivateFontPathlist for property PrivateFontPaths, instantiate one PrivateFontPath with name and path of downloaded font.

ToPdfParameterList toPdf = new ToPdfParameterList()
{
    PrivateFontPaths = new List()
        {
          new PrivateFontPath("DeeDeeFlowers",@"D:\DeeDeeFlowers.ttf")
        }
};

Step 6: Set the new font for the txtaRange.

txtRange.CharacterFormat.FontName = "DeeDeeFlowers";

Step 7: Convert the Doc to PDF.

document.SaveToFile("result.pdf", toPdf);

Step 8: Review converted PDF files.

System.Diagnostics.Process.Start("result.pdf");

Result screenshot:

How to use uninstalled font when converting Doc to PDF via Spire.Doc

Full Code Below:

Document document = new Document();
           
//Add the first secition
Section section = document.AddSection();
//Create a new paragraph and get the first paragraph
Paragraph paragraph
    = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

//Append Text
String text
    = "This paragraph is demo of text font and color. "
    + "The font name of this paragraph is Tahoma. "
    + "The font size of this paragraph is 20. "
    + "The under line style of this paragraph is DotDot. "
    + "The color of this paragraph is Blue. ";
 TextRange txtRange = paragraph.AppendText(text);

//Import the font
 ToPdfParameterList toPdf = new ToPdfParameterList()
 {
     PrivateFontPaths = new List<PrivateFontPath>()
         {
          new PrivateFontPath("DeeDeeFlowers",@"D:\DeeDeeFlowers.ttf")
         }
};
//Make use of the font.
txtRange.CharacterFormat.FontName = "DeeDeeFlowers";

document.SaveToFile("result.pdf", toPdf);

System.Diagnostics.Process.Start("result.pdf");

Set Word Font in C#, VB.NET

2011-01-13 08:01:53 Written by Administrator

Word Font setting allows users to change text font style and size in a document. With wonderful font settings, the document layout and appearance will be more appealed. What’s more, in order to make some special words, phrases or paragraphs more obvious, users can set different font styles or size. For example, the title is often set as different font style with bigger size from body.

Spire.Doc for .NET, a professional .NET Word component, enables users to set font in Word document. This guide will shows how to set by using C#, VB.NET via Spire.Doc for .NET and the following screenshot presents result after setting.

Word Text Font Style

  • Load the document and get the paragraph which you want to set font.
  • Then, declare a new paragraph style and set FontName and FontSize properties of this style.
  • Finally, apply the style for paragraph and save document. Code as following:
[C#]
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordImage
{
    class ImageinWord
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Humor Them.docx");

            //Get Paragraph
            Section s = document.Sections[0];
            Paragraph p = s.Paragraphs[1];

            //Set Font Style and Size
            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = "FontStyle";
            style.CharacterFormat.FontName = "Century Gothic";
            style.CharacterFormat.FontSize = 20;
            document.Styles.Add(style);
            p.ApplyStyle(style.Name);

            //Save and Launch
            document.SaveToFile("font.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("font.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordImage
    Friend Class ImageinWord
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\Humor Them.docx")

            'Get Paragraph
            Dim s As Section = document.Sections(0)
            Dim p As Paragraph = s.Paragraphs(1)

            'Set Font Style and Size
            Dim style As New ParagraphStyle(document)
            style.Name = "FontStyle"
            style.CharacterFormat.FontName = "Century Gothic"
            style.CharacterFormat.FontSize = 20
            document.Styles.Add(style)
            p.ApplyStyle(style.Name)

            'Save and Launch
            document.SaveToFile("font.docx", FileFormat.Docx2010)
            System.Diagnostics.Process.Start("font.docx")
        End Sub
    End Class
End Namespace

Spire.Doc, a professional Word component, can be used to generate, load, write, edit and save Word documents for .NET, Silverlight and WPF.

If you want to emphasize a specific paragraph or text in your Word document, you can change its font color. This article will demonstrate how to change font color in Word in C# and VB.NET using Spire.Doc for .NET library.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Change Font Color of a Paragraph in C# and VB.NET

The following are the steps to change the font color of a paragraph in a Word document:

  • Create a Document instance.
  • Load the Word document using Document.LoadFromFile() method.
  • Get the desired section using Document.Sections[sectionIndex] property.
  • Get the desired paragraph that you want to change the font color of using Section.Paragraphs[paragraphIndex] property.
  • Create a ParagraphStyle instance.
  • Set the style name and font color using ParagraphStyle.Name and ParagraphStyle.CharacterFormat.TextColor properties.
  • Add the style to the document using Document.Styles.Add() method.
  • Apply the style to the paragraph using Paragraph.ApplyStyle() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace ChangeFontColorForParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Get the first section
            Section section = document.Sections[0];

            //Change text color of the first Paragraph
            Paragraph p1 = section.Paragraphs[0];
            ParagraphStyle s1 = new ParagraphStyle(document);
            s1.Name = "Color1";
            s1.CharacterFormat.TextColor = Color.RosyBrown;
            document.Styles.Add(s1);
            p1.ApplyStyle(s1.Name);

            //Change text color of the second Paragraph
            Paragraph p2 = section.Paragraphs[1];
            ParagraphStyle s2 = new ParagraphStyle(document);
            s2.Name = "Color2";
            s2.CharacterFormat.TextColor = Color.DarkBlue;
            document.Styles.Add(s2);
            p2.ApplyStyle(s2.Name);

            //Save the result document
            document.SaveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Change Font Color in Word

Change Font Color of a Specific Text in C# and VB.NET

The following are the steps to change the font color of a specific text in a Word document:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Find the text that you want to change font color of using Document.FindAllString() method.
  • Loop through all occurrences of the searched text and change the font color for each occurrence using TextSelection.GetAsOneRange().CharacterFormat.TextColor Property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace ChangeFontColorForText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Find the text that you want to change font color for
            TextSelection[] text = document.FindAllString("Spire.Doc for .NET", false, true);
            //Change the font color for the searched text
            foreach (TextSelection seletion in text)
            {
                seletion.GetAsOneRange().CharacterFormat.TextColor = Color.Red;
            }

            //Save the result document
            document.SaveToFile("ChangeCertainTextColor.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Change Font Color in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page