Import Word document Paragraph with formatting in HTML Textbox in C# MVC

Hi All,

How to import word document data in WYSIWYG editor in MVC application with word style format.

@pravinghadge,

You can either save Word document to HTML or HtmlFixed format and then pass it to your editor.

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

HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.Html);
opts.PrettyFormat = true;
opts.ExportImagesAsBase64 = true;
// etc

doc.Save(MyDir + @"18.1.html", opts); 

Or the code for HtmlFixed format:

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

HtmlFixedSaveOptions opts = new HtmlFixedSaveOptions();
opts.PrettyFormat = true;
// etc

doc.Save(MyDir + @"18.1.html", opts);

You can also get HTML representation of a Paragraph in Word document by using the following code:

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

HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.Html);
opts.PrettyFormat = true;
opts.ExportImagesAsBase64 = true;

Console.WriteLine(doc.FirstSection.Body.FirstParagraph.ToString(opts));

Hope, this helps.

Hi Awais,

Thanx for your reply.

I have multiple textbox in my Web Application.

Based on Paragraph Start String i will put the paragraph in different Textboxe’s

For Eg: My Word Document Text are:

Description : It’s easier to follow discussions and find interesting people in conversations when everyone has a unique profile picture!

About: You’ve posted a few topics and replies, but your profile picture isn’t as unique as you are – it’s just a letter.

After Importing above Word document in HTML , it will add text in respective Textboxes.

@pravinghadge,

Sure, you can get the text representation of Paragraph as follows:

if (doc.FirstSection.Body.FirstParagraph.ToString(SaveFormat.Text).StartsWith("string to match"))
{
    // add to first textbox
}
else
{
    // add to second textbox
}

Hi Hafeez,

Thanx for your reply.

It works great !!!

But this code is working only for first paragraph,

I have more than 10 paragraph.

How to get data of other paragraph??

Please suggest

@pravinghadge,

You can iterate through the Paragraph collection. The following code example can be used to get HTML representations of all Paragraphs in document:

foreach(Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.Html);
    opts.PrettyFormat = true;
    opts.ExportImagesAsBase64 = true;

    string html = para.ToString(opts));
}

Thanx for your support