Asp.net Word to HTML conversion using Aspose Word Product with formatting

Hi there!

We are using Aspose word product for the file conversion from word to html. Currently we are successfully able to convert the word documents to HTML format.
In addition to this , we need the following features in order to meet our business requirement:

  1. We should able to convert document with any font type and any font size.
  2. We should able to set the output format i…e Output font type, font size and other required HTML parameters. (Predefined HTML formatting)

For Example:
If I am converting a word document with Arial font text and size 12, then after conversion, i would like to see the HTML output with the original text with Calibri font and 15 font size.

Please let us know whether this is possible and if possible then the steps to implement this requirement.

Hi Divesh,

Thanks for your inquiry. Sure, you can achieve this using Aspose.Words. If you would like to change font of the entire document before saving to Html format, you should loop through all nodes and change font. You can use DocumentVisitor to achieve this. Here is code example:

Document doc = new Document(@"C:\Temp\in.doc");
FontChanger changer = new FontChanger();
doc.Accept(changer);
doc.Save(@"C:\Temp\out.html");
/// 
/// Class inherited from DocumentVisitor, that chnges font.
/// 
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)
    {
        // if (font.Name == "Arial")
        font.Name = mNewFont;
    }
    // Font by default
    private string mNewFont = "Calibri";
}

I hope, this helps.

Best Regards,

Thank you Awais for your response.

The Response seems very useful. We will definitely try this solution and will let you know if any issue.

Apart from font type and size changes, what other formatting is possible with the word document?
like text /object alignment, color changes etc.

Do we have any reference documents which has these formatting notes?

Hi Divesh,

Thanks for your inquiry. Sure, please read the following article that outlines ‘formatting different Document elements’:
https://docs.aspose.com/words/net/applying-formatting/

Please let me know if I can be of any further assistance.

Best Regards,