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

Aspose.Words convert to html (only body content)

我可以创建 word 文件并将 HTML 转换为 aspose.words API。如何使用 API 获取 HTML 中的 BODY 内容(没有 html、head、body 标签/只有正文内容)。我将使用它在 WYSIWYG 编辑器 (summernote) 应用程序中显示输出。

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

默认情况下,Aspose.Words将html保存为Xhtml格式,因此您可以安全地将其加载到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 团队工作。

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