Articles in this section
Category / Section

How to perform font substitution in WinForms Word to PDF conversion?

3 mins read

When the necessary fonts used in the Word document has not been installed in the production machine, then Essential DocIO uses the ”Microsoft Sans Serif” as default font for rendering the text. Due to this font substitution, you could see the difference with the rendered text in the generated PDF/Image files during Word to PDF/Image conversion. To know more about the default font substitution, please refer here.

 

From 2018 Volume 1 release (v16.1.0.24) onwards, Syncfusion provides support to  perform font substitution using event approach while converting a particular Word document as PDF. So, that you can choose any installed font instead of ”Microsoft Sans Serif”.

 

The following code example illustrates how to trigger `SubstituteFont` event during font substitution in Word to PDF conversion. A runnable sample application can be downloaded from here

C#:

// Loads an existing Word document.
WordDocument wordDocument = new WordDocument("template.docx", FormatType.Docx);
// Initializes the SubstituteFont event to perform font substitution during Word to PDF conversion.
wordDocument.FontSettings.SubstituteFont += new SubstituteFontEventHandler(SubstituteFont);
// Creates an instance of DocToPDFConverter.
DocToPDFConverter converter = new DocToPDFConverter();
// Converts the Word document into PDF document.
PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);
// Closes the instance of Word document object.
wordDocument.Close();
// Saves the PDF file.
pdfDocument.Save("WordtoPDF.pdf");
// Closes the instance of PDF document object.
pdfDocument.Close(true);

 

VB:

' Loads an existing Word document.
Dim wordDocument As New WordDocument("template.docx", FormatType.Docx)
' Initializes the SubstituteFont event to perform font substitution during Word to PDF conversion.
AddHandler wordDocument.FontSettings.SubstituteFont, AddressOf SubstituteFont
' Creates an instance of DocToPDFConverter.
Dim converter As New DocToPDFConverter()
' Converts the Word document into PDF document.
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(wordDocument)
' Closes the instance of document object.
wordDocument.Close()
' Saves the PDF file .
pdfDocument.Save("WordtoPDF.pdf")
' Closes the instance of document object.
pdfDocument.Close(True)

 

The following code example illustrates how to set the alternate font when a specified font is not installed in the production environment by using `SubstituteFontEventHandler`.

C#:

private void 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.AlternateFontName = "Arial";
    else
        args.AlternateFontName = "Times New Roman";
}

 

VB:

Private Sub SubstituteFont(ByVal sender As Object, ByVal args As SubstituteFontEventArgs)
    ' Sets the alternate font when a specified font is not installed in the production environment.
    If args.OrignalFontName = "Arial Unicode MS" Then
        args.AlternateFontName = "Arial"
    Else
        args.AlternateFontName = "Times New Roman"
    End If
End Sub

 

If you have any questions or require clarification about this support, please let us know in the comments below. You can also contact us through our support forum or Direct Trac. We are happy to assist you as always!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied