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 image. While performing Word to PDF/Image conversion, internally DocIO uses the fonts installed in the environment to measure and draw the content of 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 used fonts in Word document not available during Word to PDF/Image?
If font is not installed in the environment, you will face the content will not be preserved in the output PDF/Image as like input Word document.
You can also check whether all the necessary fonts used in Word document are installed or not for Word to PDF or image conversion.
How to resolve font missing problem during Word to PDF/Image
You can use any one of the 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 the Microsoft-compatible fonts by adding the Docker commands in the Dockerfile. For further information, refer here.
- If you are still facing the 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 handler to substitute alternate fonts when the necessary fonts not available in the production environment during Word to PDF/image conversion.
The following code example shows how to add event handler to set alternate font when the specified font is not installed in the machine. You can use same event handler for Word to Image conversion also.
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 missed 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, uses the "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 in machine
You can also use any other alternate fonts without installing 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 Microsoft Word application and then convert the Word document to PDF/Image using DocIO.
Follow the steps to go to the below option in Microsoft Word application. Go to File -> Options -> Save in Microsoft Word application. Refer the below screenshot to embed the fonts in Microsoft Word application.
Internally, DocIO uses those embedded fonts for conversion and it resolves the preservation problems.
Note: Using embedded fonts for Word to PDF/Image conversion supported in DOCX format document only.
What to do if still facing any preservation difference in PDF, after using above solutions?
If you are still facing any 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 the particular font information (glyphs) from the TrueType fonts used for the rendered characters in 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 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 the 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 document to PDF or image conversion.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for 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!