How to resolve font problems during Word to PDF or image conversion?
Syncfusion® .NET Word library (DocIO) allows you to convert Word documents to PDF and images. While performing Word to PDF/Image conversion, DocIO internally uses the fonts installed in the environment to measure and draw the content of the Word document during conversion. If a font is not available in the environment, an alternate font will be used. This can lead to differences in the output PDF or image, such as missing text or different font sizes.
What happens if the fonts used in the Word document are not available during Word to PDF/Image conversion?
If a font is not installed in the environment, the content will not be preserved in the output PDF/Image as it appears in the input Word document.
You can also check whether all the necessary fonts used in the Word document are installed or not for Word to PDF or image conversion.
How to resolve font missing problems during Word to PDF/Image conversion
You can use any one of the following suggestions to resolve the problem.
Suggestion 1: Install all the necessary fonts
Install all the necessary fonts in the production environment, which are used in your Word document.
For Linux containers:
- Install Microsoft-compatible fonts by adding the Docker commands in the Dockerfile. For further information, refer here.
- If you are still facing font problems after implementing the above suggestion, you can maintain the necessary fonts in a separate folder and copy them to the Linux containers. For further information, refer here.
Suggestion 2: Substitute alternate fonts
DocIO supports event handlers to substitute alternate fonts when the necessary fonts are not available in the production environment during Word to PDF/Image conversion.
The following code example shows how to add an event handler to set an alternate font when the specified font is not installed in the machine. You can use the same event handler for Word to Image conversion as well.
C#:
using (FileStream docStream = new FileStream("Template.docx", FileMode.Open, FileAccess.Read))
{
// Loads file stream into Word document
using (WordDocument wordDocument = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Docx))
{
// Hooks the font substitution event
wordDocument.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
// Instantiation of DocIORenderer for Word to PDF conversion
using (DocIORenderer render = new DocIORenderer())
{
// Converts Word document into PDF document
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
// Unhooks the font substitution event after converting to PDF
wordDocument.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;
// Saves the PDF file
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
// Closes the instance of PDF document object
pdfDocument.Close();
}
}
}
}
Event to set alternate installed font
You can use any other alternate installed font names instead of missing fonts during Word to PDF/Image conversion.
The following code example shows how to set the alternate installed font name when converting a Word document to PDF/Image.
C#:
private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
// Sets the alternate font when a specified font is not installed in the production environment
// If "Arial Unicode MS" font is not installed, then it uses the "Arial" font
// For other missing fonts, it uses "Times New Roman"
if (args.OriginalFontName == "Arial Unicode MS")
args.AlternateFontName = "Arial";
else
args.AlternateFontName = "Times New Roman";
}
You can download a complete working sample from GitHub.
Event to set alternate font without installing it on the machine
You can also use any other alternate fonts without installing them during Word to PDF/Image conversion.
Note: If the font is installed but the style type of the font is missing, the font will be thrown in the event handler. For example, if Cambria Italic is installed on your machine but Cambria Bold is used in the Word document, then Cambria Bold will be thrown in the event.
C#:
private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
// Sets the alternate font when a specified font is not installed in the production environment
if (args.OrignalFontName == "Arial Unicode MS" && args.FontStyle == FontStyle.Regular)
args.AlternateFontStream = new FileStream("Arial.TTF", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
else
args.AlternateFontStream = new FileStream("Calibri.TTF", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
You can download a complete working sample from GitHub.
Suggestion 3: Embed fonts in DOCX
You can also resolve this font-related problem by embedding the necessary fonts in the input Word document using the Microsoft Word application and then convert the Word document to PDF/Image using DocIO.
Follow the steps below to go to the required option in the Microsoft Word application. Go to File -> Options -> Save in the Microsoft Word application. Refer to the below screenshot to embed the fonts in the Microsoft Word application.
Internally, DocIO uses those embedded fonts for conversion, which resolves preservation problems.
- Using embedded fonts for Word to PDF/Image conversion is supported only in DOCX format documents.
- Microsoft Office doesn’t support embedding *.otf (OpenType) fonts. It only supports *.ttf (TrueType) fonts. To embed fonts in Office documents, use *.ttf fonts instead. Replace *.otf fonts with *.ttf alternatives to ensure consistent formatting when sharing your document. For more details, see the official Microsoft documentation.
What to do if you still face preservation differences in PDFs after using the above solutions?
If you are still facing preservation issues after using the above solutions, it is possible that your machine does not contain the respective font and the PDF viewer is unable to load the content properly. To resolve this, you can embed the fonts in the PDF document itself. This will allow you to transfer the PDF documents to any device without experiencing any content preservation differences.
Embed Subset Fonts
This setting allows you to embed particular font information (glyphs) from the TrueType fonts used for the rendered characters in the converted PDF document.
The following code sample shows how to embed the TrueType fonts into the converted PDF document.
C#:
WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic)
// Instantiation of DocIORenderer for Word to PDF conversion
DocIORenderer render = new DocIORenderer();
// Sets true to embed TrueType fonts
render.Settings.EmbedFonts = true;
// Converts Word document into PDF document
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
You can download a complete working sample from GitHub.
Note: If you are using DocToPDFConverter for conversion, you can achieve the same using DocToPDFConverterSettings.EmbedFonts API.
Embed Complete Fonts
This setting allows you to embed the complete font information (glyphs) from the TrueType fonts used in the converted PDF document.
The following code sample shows how to embed the complete TrueType fonts into the converted PDF document.
C#:
WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic)
// Instantiation of DocIORenderer for Word to PDF conversion
DocIORenderer render = new DocIORenderer();
// Sets true to embed complete TrueType fonts
render.Settings.EmbedCompleteFonts = true;
// Converts Word document into PDF document
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
You can download a complete working sample from GitHub.
Note: If you are using DocToPDFConverter for conversion, you can achieve the same using DocToPDFConverterSettings.EmbedCompleteFonts API.
Complex script preservation
Sometimes, you may encounter issues with preserving complex scripts such as Arabic, Chinese, and others in PDFs that have been converted from Word documents. To solve this problem, make sure that the AutoDetectComplexScript setting is enabled during Word to PDF conversion.
The following code sample shows how to preserve the complex script text in the converted PDF document.
C#:
WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic)
// Instantiation of DocIORenderer for Word to PDF conversion
DocIORenderer render = new DocIORenderer();
// Sets true to embed complete TrueType fonts
render.Settings.AutoDetectComplexScript = true;
// Converts Word document into PDF document
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
You can download a complete working sample from GitHub.
Note: If you are using DocToPDFConverter for conversion, you can achieve the same using DocToPDFConverterSettings.AutoDetectComplexScript API.
Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to resolve font problems when converting Word documents to PDF or image conversions.
You can refer to our ASP.NET Core DocIO feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!