首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

Aspose.Words 是一个商业库,用于处理 Microsoft Word 文档

代码语言:javascript
复制
using Aspose.Words;
using System;

namespace AsposeWordsToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            // 加载 Word 文档
            Document doc = new Document("input.docx");

            // 创建一个 HTML 写入器
            HtmlSaveOptions saveOptions = new HtmlSaveOptions();
            saveOptions.HtmlFormatter = new CustomHtmlFormatter();

            // 将文档保存为 HTML
            doc.Save("output.html", saveOptions);
        }
    }

    public class CustomHtmlFormatter : IHtmlFormatter
    {
        public string FormatHtml(string html)
        {
            // 删除所有非正文内容的 HTML 标签
            string plainText = System.Text.RegularExpressions.Regex.Replace(html, @"<[^>]*>", string.Empty);
            return plainText;
        }
    }
}

在这个示例中,我们首先加载一个 Word 文档,然后创建一个自定义的 HTML 写入器,该写入器使用 CustomHtmlFormatter 类来格式化 HTML。CustomHtmlFormatter 类实现了 IHtmlFormatter 接口,并重写了 FormatHtml 方法。在这个方法中,我们使用正则表达式删除所有非正文内容的 HTML 标签。

最后,我们将文档保存为 HTML 文件。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券