How to change the font text of whole document and convert to PDF using Java

Hi,
We are using word to pdf conversion, using aspose, The use case what we have is to convert all the Font style to default font “Trebuchet MS”, even if the input word doc has different fonts like “Times new roman” or “Arial”.

Would it be possible to default a font style to one format, even though the input word file has different format, Can you share any example or documentation if this is a feasible solution ?

Also we have a support to convert word doc for different language like Chinese, Russian etc, If I default the font style to Trebuchet MS, would it affect the font style of the different languages as well ?

@somasundaramv

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";
}

Hi,
Is this a Java implementation for the class documentVisitor ? Can you share the java implementation of the same, Not sure if the same class/interface is available for Java also or not.

and also, instead of setting the font name as a string the reset method, would it be possible to provide the folder path, where the Trebuchet MS fonts files are located ?

@somasundaramv

In your case, we suggest you following solution.

  1. Put the font ‘Trebuchet MS’ in your desired folder.

  2. Please use FontSettings.SetFontsFolder method to set the folder where Aspose.Words looks for TrueType fonts when rendering documents or embedding fonts.

  3. The FontSubstitutionSettings class specifies font substitution mechanism settings. You can substitute the fonts of your document with specific font using FontSubstitutionSettings.TableSubstitution property as shown below.

    doc.getFontSettings().getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
    doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().addSubstitutes(“Arial”, new String[]{“Trebuchet MS”});

  4. To get the fonts used in your document, please use Document.FontInfos property.

Moreover, we suggest you please read the following article.
True Type Fonts