Ekrem Kangal
Ekrem Kangal

Reputation: 125

Aspose.Words convert to html (only body content)

I can create word file and convert HTML with aspose.words API. How do I get the BODY content in HTML with the API (withou html,head,body tag/ only body content). I will use this to show the output in the WYSIWYG editors (summernote) application.

Note: I am developing the application with .net Framework (C#)

Upvotes: 0

Views: 3359

Answers (2)

Hüseyin Karatay
Hüseyin Karatay

Reputation: 26

Document doc = new Document(MyDir + "inputdocx.docx");
var options = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Html)
{
ImageSavingCallback = new HandleImageSaving(),
}; 
String html = doc.FirstSection.Body.ToString(options);

Upvotes: 1

Alexey Noskov
Alexey Noskov

Reputation: 1960

By default, Aspose.Words saves html in Xhtml format, so you can safely load it into XmlDocument and get bydy tag’s content. For example see the following code.

// Create a simple document for testing.
DocumentBuilder builder = new DocumentBuilder();
builder.Writeln("Hello world!!!");
// For testing purposes insert an image.
builder.InsertImage(@"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png");

// Additional options can be specified in the corresponding save options.
HtmlSaveOptions opt = new HtmlSaveOptions(SaveFormat.Html);
// For example, output images in the HTML as base64 string (summernote supports base64)
opt.ExportImagesAsBase64 = true;

// Save the document to MemoryStream.
using (MemoryStream ms = new MemoryStream())
{
    builder.Document.Save(ms, opt);

    // Move the stream position ot the beginning and load the resulting HTML into Xml document.
    ms.Position = 0;
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(ms);

    // Find body tag.
    XmlNode body = xmlDoc.SelectSingleNode("//body");

    // Get inner xml of the body.
    Console.WriteLine(body.InnerXml);
}

Hope this helps.

Disclosure: I work at Aspose.Words team.

Upvotes: 0

Related Questions