Convert Word to PDF with Document Fonts

The Word to PDF Converter renders the text of a document with the fonts installed on the machine, with the fonts stored in the Word document by its author and with the font files you give to the converter. This way the text keeps its look on a server where the fonts of the document are not installed.

The font options are exposed by the WordToPdfConverterUseEmbeddedFonts, WordToPdfConverterFontFiles and WordToPdfConverterFallbackFontFiles properties of the WordToPdfConverter class.

Use the Fonts Stored in the Word Document

Word stores the fonts of a document in the DOCX file when the option to embed fonts is on when the document is saved. When UseEmbeddedFonts is true, the default value, the text is rendered with the stored fonts. When it is false the stored fonts are ignored and the text is rendered with the installed fonts.

Use the Fonts Stored in the Word Document
wordToPdfConverter.UseEmbeddedFonts = true;

Add Font Files

The TrueType or OpenType font files added to FontFiles are made available to the document in addition to the installed fonts. A font file provides the family it is named after, so it is used for the text set in that family in place of an installed font with the same name or when that font is not installed.

Add a Font File
wordToPdfConverter.FontFiles.Add(Path.Combine(fontsPath, "DejaVuSerif.ttf"));

Add Fallback Font Files

The font files added to FallbackFontFiles are used for the characters that no font of the document covers, such as text in another writing system.

Add a Fallback Font File
wordToPdfConverter.FallbackFontFiles.Add(Path.Combine(fontsPath, "NotoSansArabic-Regular.ttf"));

Code Sample - Convert Word to PDF with Document Fonts

C#
using System.IO;

// Use EVO PDF Namespace
using EvoPdf.Next;

public byte[] ConvertWordToPdfWithFonts(byte[] inputWordBytes, string fontsPath)
{
    // Set the license key received after purchase to use the library in licensed mode; leave it commented for demo mode
    // Licensing.LicenseKey = "your-license-key";

    // Create a Word to PDF converter object with default settings
    WordToPdfConverter wordToPdfConverter = new WordToPdfConverter();

    // Use the fonts stored in the Word document. Word embeds the fonts of a document when the
    // option to embed fonts is on when the document is saved, so the text keeps its look where
    // the fonts are not installed. When this option is off the stored fonts are ignored and the
    // text is rendered with the installed fonts
    wordToPdfConverter.UseEmbeddedFonts = true;

    // A font file given to the converter is used for the text set in its font family, in
    // place of an installed font with the same family name or when that font is not installed
    wordToPdfConverter.FontFiles.Add(Path.Combine(fontsPath, "DejaVuSerif.ttf"));

    // A fallback font file is used for the characters that no font of the document covers,
    // such as the Arabic text of the sample document
    wordToPdfConverter.FallbackFontFiles.Add(Path.Combine(fontsPath, "NotoSansArabic-Regular.ttf"));

    // The buffer to receive the generated PDF document
    byte[] outPdfBuffer = wordToPdfConverter.ConvertToPdf(inputWordBytes);

    return outPdfBuffer;
}

See Also