@ksmani82
Thanks for sharing the additional information. Please note SetFontSubstitue method is used when Aspose.Words does not find the used font on host machine. If the document fonts are available on host machine or embedded in document then it is not called. Please check Font Availability and Substitution section for more details.
Furthermore, if you want to replace some available font with another font then you can use FontChange class. Please check following code snippet, it changes document fonts to “Arial Narrow MT Pro”. Hopefully it will help you to accomplish the task.
output_aw.zip (20.4 KB)
protected void Page_Load(object sender, EventArgs e)
{
Aspose.Words.Document doc = new Aspose.Words.Document("input.docx");
FontChanger changer = new FontChanger();
doc.Accept(changer);
doc.Save("output_aw.pdf");
}
/// <summary>
/// Class inherited from DocumentVisitor, that changes font.
/// </summary>
class FontChanger : DocumentVisitor
{
/// <summary>
/// Called when a FieldEnd node is encountered in the document.
/// </summary>
public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
{
//Simply change font name
ResetFont(fieldEnd.Font);
return VisitorAction.Continue;
}
/// <summary>
/// Called when a FieldSeparator node is encountered in the document.
/// </summary>
public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
{
ResetFont(fieldSeparator.Font);
return VisitorAction.Continue;
}
/// <summary>
/// Called when a FieldStart node is encountered in the document.
/// </summary>
public override VisitorAction VisitFieldStart(FieldStart fieldStart)
{
ResetFont(fieldStart.Font);
return VisitorAction.Continue;
}
/// <summary>
/// Called when a Footnote end is encountered in the document.
/// </summary>
public override VisitorAction VisitFootnoteEnd(Footnote footnote)
{
ResetFont(footnote.Font);
return VisitorAction.Continue;
}
/// <summary>
/// Called when a FormField node is encountered in the document.
/// </summary>
public override VisitorAction VisitFormField(FormField formField)
{
ResetFont(formField.Font);
return VisitorAction.Continue;
}
/// <summary>
/// Called when a Paragraph end is encountered in the document.
/// </summary>
public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
{
ResetFont(paragraph.ParagraphBreakFont);
return VisitorAction.Continue;
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public override VisitorAction VisitRun(Run run)
{
ResetFont(run.Font);
return VisitorAction.Continue;
}
/// <summary>
/// Called when a SpecialChar is encountered in the document.
/// </summary>
public override VisitorAction VisitSpecialChar(SpecialChar specialChar)
{
ResetFont(specialChar.Font);
return VisitorAction.Continue;
}
private void ResetFont(Aspose.Words.Font font)
{
//if (font.Name == "Arial Narrow MT Pro")
font.Name = mNewFont;
}
//Font by default
private string mNewFont = "Arial Narrow MT Pro";
}
Best Regards,