Back to Devexpress

How to: Embed Fonts Used in a Document in the Word Processing Document API

officefileapi-404347-word-processing-document-api-examples-text-how-to-embed-fonts-used-in-a-document.md

latest2.1 KB
Original Source

How to: Embed Fonts Used in a Document in the Word Processing Document API

  • Mar 20, 2023

The Word Processing Document API allows you to specify whether to embed fonts used in a document. You can embed TrueType and OpenType fonts that have the specified embedding permissions.

Use the Document.EmbedFonts property to embed fonts.

The code sample below loads a document to the RichEditDocumentServer instance, formats the document header and applies the Chiller font, enables font embedding, and saves the result.

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
//...

using (var wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument(@"C:\Docs\Word (RTF) Document API for NET.docx");
    Paragraph titleParagraph = wordProcessor.Document.Paragraphs[0];
    CharacterProperties characterProperties = wordProcessor.Document.BeginUpdateCharacters(titleParagraph.Range);
    characterProperties.FontName = "Chiller";
    characterProperties.FontSize = 16;
    wordProcessor.Document.EndUpdateCharacters(characterProperties);
    wordProcessor.Document.EmbedFonts = true;
    string fileName = /*the path to the OneDrive folder*/ 
    wordProcessor.SaveDocument(fileName, DocumentFormat.Docx);
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
'...

Using wordProcessor As New RichEditDocumentServer()
  wordProcessor.LoadDocument("C:\Docs\Word (RTF) Document API for NET.docx")
  Dim titleParagraph As Paragraph = wordProcessor.Document.Paragraphs(0)
  Dim characterProperties As CharacterProperties = wordProcessor.Document.BeginUpdateCharacters(titleParagraph.Range)
  characterProperties.FontName = "Chiller"
  characterProperties.FontSize = 16
  wordProcessor.Document.EndUpdateCharacters(characterProperties)
  wordProcessor.Document.EmbedFonts = True
  Dim fileName As String = wordProcessor.SaveDocument(fileName, DocumentFormat.Docx)
End Using