Aspose.Words转换为html(仅正文内容)



我可以创建单词文件,并使用aspose.words API转换HTML。如何使用API获取HTML中的BODY内容(无HTML、头、正文标记/仅正文内容(。我将使用它来显示所见即所得编辑器(summernote(应用程序中的输出。

注意:我正在使用.net Framework(C#(开发应用程序

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);

默认情况下,Aspose.Words以Xhtml格式保存html,因此您可以安全地将其加载到XmlDocument中并获取bydy标记的内容。例如,请参阅以下代码。

// 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);
}

希望这能有所帮助。

披露:我在Aspose.Words团队工作。

最新更新