Articles in this section
Category / Section

How to perform font substitution in .NET WebForms Word to PDF conversion?

2 mins read

When the necessary fonts used in the Word document have not been installed on the production machine, Essential® DocIO uses “Microsoft Sans Serif” as the default font for rendering the text. Due to this font substitution, you may notice differences in the rendered text in the generated PDF/Image files during Word to PDF/Image conversion. To learn more about default font substitution, please refer here.

 

From the 2018 Volume 1 release (v16.1.0.24) onwards, Syncfusion® provides support for performing font substitution using an event approach while converting a particular Word document to PDF. This allows you to choose any installed font instead of “Microsoft Sans Serif.”

 

The following code example illustrates how to trigger the `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 a PDF document.
PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);
// Closes the instance of the Word document object.
wordDocument.Close();
// Saves the PDF file.
pdfDocument.Save("WordtoPDF.pdf");
// Closes the instance of the 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 a PDF document.
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(wordDocument)
' Closes the instance of the document object.
wordDocument.Close()
' Saves the PDF file.
pdfDocument.Save("WordtoPDF.pdf")
' Closes the instance of the 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.OriginalFontName == "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.OriginalFontName = "Arial Unicode MS" Then
        args.AlternateFontName = "Arial"
    Else
        args.AlternateFontName = "Times New Roman"
    End If
End Sub

 

Note:

A new version of Essential® Studio® for ASP.NET is available. Versions released prior to Essential® Studio® 2014, Volume 2 are now referred to as classic versions. The new ASP.NET suite is powered by Essential® Studio® for JavaScript, providing client-side rendering of HTML5-JavaScript controls, offering better performance, and improved support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can still be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential® Studio® for ASP.NET. Although Syncfusion® will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

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 explore 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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