Free Support Forum - aspose.com

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();<o:p></o:p>

doc.Accept(changer);<o:p></o:p>

doc.Save(@“C:\Temp\out.html”);


///

/// Class inherited from DocumentVisitor, that chnges font.<o:p></o:p>

/// <o:p></o:p>

class FontChanger : DocumentVisitor<o:p></o:p>

{<o:p></o:p>

///

<o:p></o:p>

/// Called when a FieldEnd node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)<o:p></o:p>

{<o:p></o:p>

//Simply change font name<o:p></o:p>

ResetFont(fieldEnd.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a FieldSeparator node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)<o:p></o:p>

{<o:p></o:p>

ResetFont(fieldSeparator.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a FieldStart node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFieldStart(FieldStart fieldStart)<o:p></o:p>

{<o:p></o:p>

ResetFont(fieldStart.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a Footnote end is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFootnoteEnd(Footnote footnote)<o:p></o:p>

{<o:p></o:p>

ResetFont(footnote.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a FormField node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFormField(FormField formField)<o:p></o:p>

{<o:p></o:p>

ResetFont(formField.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a Paragraph end is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitParagraphEnd(Paragraph paragraph)<o:p></o:p>

{<o:p></o:p>

ResetFont(paragraph.ParagraphBreakFont);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a Run node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitRun(Run run)<o:p></o:p>

{<o:p></o:p>

ResetFont(run.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a SpecialChar is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitSpecialChar(SpecialChar specialChar)<o:p></o:p>

{<o:p></o:p>

ResetFont(specialChar.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

private void ResetFont(Aspose.Words.Font font)<o:p></o:p>

{<o:p></o:p>

//if (font.Name == “Arial”)<o:p></o:p>

font.Name = mNewFont;<o:p></o:p>

}<o:p></o:p>

//Font by default<o:p></o:p>

private string mNewFont = Calibri;<o:p></o:p>

}


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’:
http://www.aspose.com/docs/display/wordsnet/Specifying+Formatting

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

Best Regards,