How to use custom font in DocIO when export to PDF?

Hello everyone

I want to create PDF document with complex flow layout, for this I use word document with conversion to PDF.


Also, I want custom font "Roboto" in my word document.


How can I do this?


4 Replies 1 reply marked as answer

MJ Mohanaselvam Jothi Syncfusion Team January 15, 2021 01:26 PM UTC

Hi Yauheni,

Thank you for contacting Syncfusion support.

Using Essential DocIO you can create Word document with custom font and also convert Word document to PDF. In the Word document, setting font name alone is enough to display in Microsoft Word application. But, for Word to PDF conversion it is need to access the font to perform internal measuring and layouting techniques.  If the font is installed in the production machine, DocIO uses that font for Word to PDF conversion process.

If you wish to perform Word to PDF conversion without installing the fonts in production machine means, we suggest to use SubstituteFont event to use alternate font stream for missed fonts during Word to PDF conversion.

We have prepare the sample for the same and it can be download from the below link:
https://www.syncfusion.com/downloads/support/forum/161514/ze/WordToPDF362920898
Note: In this sample, kindly replace your .ttf custom font file to use your custom font in Word to PDF conversion process.

Please refer the below link to know more about font substitution in Word to PDF conversion:
https://help.syncfusion.com/file-formats/docio/word-to-pdf#font-substitution

Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 


Marked as answer

YP Yauheni Pakala January 16, 2021 01:36 PM UTC

Awesome! Thank you!

Quick answer:

            //Creates an instance of WordDocument Instance (Empty Word Document)
            WordDocument wordDocument = new WordDocument();
         
            //Sets the font name to text
            text.CharacterFormat.FontName = "Roboto";

            //Hooks the font substitution event
            wordDocument.FontSettings.SubstituteFont += FontSettings_SubstituteFont;

            // ...

        /// <summary>
        /// Event Handler to use alternate font without installing the fonts
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        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 == "Roboto")
                //Here you can use your custom font file
                args.AlternateFontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("WordToPDF.Assets.Roboto.TTF");
        }


RA Ronald Abellano February 5, 2021 05:42 AM UTC


Why this is not working ?  args.AlternateFontStream = File.OpenRead(filePath); ?

When the conversion starts the error said that the fontStream is null. I've checked the path is right testing locally. C:.....\\font.woff2


MJ Mohanaselvam Jothi Syncfusion Team February 8, 2021 11:03 AM UTC

Hi Ronald,

Thank you for contacting Syncfusion support.

On further checking the given details, we have found that your are using *. woff2 format font file for font substitution. DocIO supports *.ttf and *.otf format font files only. So, it rises exception while using the given improper font file for Word to PDF conversion process.

To resolve this problem, we recommend you to use *.ttf or *.otf font files for substitution.

Please let us know if you have any other questions.

Regards,
Mohanaselvam J 


Loader.
Up arrow icon