We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Substitute font name for original font name while converting DOCX to PDF using C#

Hello,
I’m trying to replace a font with another font during the DOCX to PDF conversion regardless of whether the original font is present on the machine or not. Is it possible?
Thank you

@marco.lui

Yes, you can change the font of text during DOCX to PDF conversion using Aspose.Words. Please read the following article.
How to Specify the Default Font to use when Rendering

You can use TableSubstitutionRule.AddSubstitutes method to add substitute font names for given original font name.

Thanks @tahir.manzoor for the reply.

I tried with the methods indicated, but they only work when the font is not found.
My need is that fonts are always replaced, regardless of whether they are present or not.
For example in my case I need that all the fonts in the document to be replaced with “arial”.

@marco.lui

Please use the following code example to change the font name of text in whole document and save it to PDF. Hope this helps you.

Document doc = new Document(MyDir + @"input.docx");

FontChanger changer = new FontChanger();
doc.Accept(changer);

doc.Save(MyDir + @"out.pdf");

---
class FontChanger : DocumentVisitor
{
    ///
    /// Called when a FieldEnd node is encountered in the document.
    ///
    public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
    {
        //Simply change font name
        ResetFont(fieldEnd.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a FieldSeparator node is encountered in the document.
    ///
    public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
    {
        ResetFont(fieldSeparator.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a FieldStart node is encountered in the document.
    ///
    public override VisitorAction VisitFieldStart(FieldStart fieldStart)
    {
        ResetFont(fieldStart.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a Footnote end is encountered in the document.
    ///
    public override VisitorAction VisitFootnoteEnd(Footnote footnote)
    {
        ResetFont(footnote.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a FormField node is encountered in the document.
    ///
    public override VisitorAction VisitFormField(FormField formField)
    {
        ResetFont(formField.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a Paragraph end is encountered in the document.
    ///
    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        ResetFont(paragraph.ParagraphBreakFont);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a Run node is encountered in the document.
    ///
    public override VisitorAction VisitRun(Run run)
    {
        ResetFont(run.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a SpecialChar is encountered in the document.
    ///
    public override VisitorAction VisitSpecialChar(SpecialChar specialChar)
    {
        ResetFont(specialChar.Font);
        return VisitorAction.Continue;
    }

    private void ResetFont(Aspose.Words.Font font)
    {
        font.Name = mNewFontName;
    }

    private string mNewFontName = "Arial";
}