News Category

Spire.Doc for .NET

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);
        }
    }
}

Sometimes we only need to get the text from the word document for other use when we deal with the word documents with large amount of information. With the help of Spire.Doc, we have already demonstrated how to extract the text from the word document by traverse every paragraph on the word document and then append the text accordingly. This article will show you how to use the method of doc.GetText() to extract the text directly from the word documents with texts, images and tables. It is more convenient for developers to extract the text from the word document from code.

Firstly, view the sample word document which will be extracted the text firstly:

How to get text from word document in C#

Step 1: Create a word instance and load the source word document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 2: Invoke the doc.GetText() method to get all the texts from the word document.

string s = doc.GetText();

Step 3: Create a New TEXT File to Save Extracted Text.

File.WriteAllText("Extract.txt", s.ToString());

Effective screenshot after get all the text from the word document:

How to get text from word document in C#

Full codes:

using Spire.Doc;
using System.IO;
namespace GetText
{
   class WordText
 {
   public void GetText()
   {
     Document doc = new Document();
     doc.LoadFromFile("Sample.docx");

     string s = doc.GetText();

     File.WriteAllText("Extract.txt", s.ToString());

    }
 }
}

SVG (Scalable Vector Graphics) is an image file format and it has the advantage of be edited and indexed easily. It is widely used for interactivity and animation. Starts from Spire.Doc V5.8, Spire.Doc offers a method doc.SaveToFile() to enable developers to save the word document to SVG file format in C# easily. This article will focus on demonstrate how to convert word document to SVG with the help of Spire.Doc by using a simple line of code in C#.

Firstly, please view the original word document:

How to Convert Word to SVG (Scalable Vector Graphics) in C#

Step 1: Create a C# project in visual studio, then add a reference to Spire.Doc.dll and use the following namespace.

using Spire.Doc;

Step 2: Create a word instance and load the source word document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 3: Save the document to SVG file.

doc.SaveToFile("result.svg", FileFormat.SVG);

After running the code, we'll get the effective screenshot of the following result SVG file from word document:

How to Convert Word to SVG (Scalable Vector Graphics) in C#

Full codes:

using Spire.Doc;

namespace wordconversion.Case
{
    class WordtoSVG
    {

         public WordtoSVG()
         {
        Document doc = new Document();
        doc.LoadFromFile("Sample.docx");

        doc.SaveToFile("result.svg", FileFormat.SVG);
         }

    }
}