OpenXML将Word文档转换为带头的HTML

技术标签: C#  ASP.NET-MVC  OpenXML.

我想读取.docx文件,并以电子邮件发送其内容作为电子邮件正文,不是作为附件。

因此,我使用OpenXML和OpenXMLPowertools将DocX文件转换为HTML。直到我收到一份文件,这几乎工作得很好 标题页脚 用图像。

以下是要将.docx转换为html的代码

 using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
                {
                    HtmlConverterSettings convSettings = new HtmlConverterSettings()
                    {
                        FabricateCssClasses = true,
                        CssClassPrefix = "cls-",
                        RestrictToSupportedLanguages = false,
                        RestrictToSupportedNumberingFormats = false,
                        ImageHandler = imageInfo =>
                        {
                            DirectoryInfo localDirInfo = new DirectoryInfo(imageDirectoryName);
                            if (!localDirInfo.Exists)
                            {
                                localDirInfo.Create();
                            }

                            ++imageCounter;
                            string extension = imageInfo.ContentType.Split('/')[1].ToLower();
                            ImageFormat imageFormat = null;
                            if (extension == "png")
                            {
                                extension = "jpeg";
                                imageFormat = ImageFormat.Jpeg;
                            }
                            else if (extension == "bmp")
                            {
                                imageFormat = ImageFormat.Bmp;
                            }
                            else if (extension == "jpeg")
                            {
                                imageFormat = ImageFormat.Jpeg;
                            }
                            else if (extension == "tiff")
                            {
                                imageFormat = ImageFormat.Tiff;
                            }

                            // If the image format is not one that you expect, ignore it,
                            // and do not return markup for the link.
                            if (imageFormat == null)
                            {
                                return null;
                            }

                            string imageFileName = imageDirectoryName + "/image" + imageCounter.ToString() + "." + extension;

                            try
                            {
                                imageInfo.Bitmap.Save(imageFileName, imageFormat);
                            }
                            catch (System.Runtime.InteropServices.ExternalException)
                            {
                                return null;
                            }

                            XElement img = new XElement(Xhtml.img, new XAttribute(NoNamespace.src, imageFileName), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
                            return img;
                        }
                    };

                    XElement html = OpenXmlPowerTools.HtmlConverter.ConvertToHtml(doc1, convSettings);

上面的代码工作正常,还可以转换图像,但如果文档具有页眉和页脚,则不会转换。

因此,他们的任何解决方法都包含HTML文件中的标题和页脚。

请建议我。谢谢!

看答案

OpenXMLPowertools在将DocX-Document转换为HTML时忽略页眉和页脚,因此它们不会在生成的HTML中显示(您可以 浏览源代码 在github上)。

也许是因为“页面”的概念不适用于HTML,因此没有明显等同于文档标题。


智能推荐

java对象创建、内存布局和访问定位

出入:深入理解Java虚拟机:JVM高级特性与最佳实践(第3版) 1.1对象创建 当Java虚拟机遇到一条字节码new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已被加载、解析和初始化过。如果没有,那必须先执行相应的类加载过程。 这里常量池应该是方法区-》运行时常量池。 新生对象内存分配方式: 指针碰撞(Bump The Pointer...

Securing the Deep Learning Stack

This is the first post of Nervana’s “Security Meets Deep Learning” series. Security is one of the biggest concerns for any enterprise, but it’s especially critical for companie...

计算智能(CI)之粒子群优化算法(PSO)(一)

欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习、深度学习的知识! 计算智能(Computational Intelligence , CI)是以生物进化的观点认识和模拟智能。按照这一观点,智能是在生物的遗传、变异、生长以及外部环境的自然选择中产生的。在用进废退、优胜劣汰的过程中,适应度高的结构被保存下来,智能水平也随之提高。因此计...

@解决[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up. [

@解决[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up. [28000][1045] Access denied for user ‘root’@‘localhost’ (using password: YES)...

ngrx注入到应用类构造函数里的store变量

这个类型为Store<fromExample.State>的变量: 本质是个Observable: store的source字段的_value里,包含了两个state的初始值: 更多Jerry的原创文章,尽在:“汪子熙”:...

猜你喜欢

Windows XP SP3中远程桌面实现多用户登陆

现在电脑我想都可以在远程控制和远程协助了吧(xp,sp2,sp3),现在一个问题,你想远程操控你的电脑,但是你电脑有人用,你好意思让他退出~然后你在远程控制?呵呵,下面Star我为他家说说sp3多用户控制的弄法~~sp2这些都类似~具体没有试。。 案例:电脑建2个用户,一个用户你专用(假设为:xp1),一个用户给别人用(假设为:xp2)~自己看着办~如果你远程控制你的电脑通过电脑进入你的用户名xp...

java运行环境-jdk1.7如何切换成1.8

适用于电脑已经安装了jdk1.7版本,然后想要重新安装1.8版本并且使用1.8版本的情况。   理论上来说,如果之前存在1.7版本,现在安装了1.8版本 ,只需要重新配置java环境变量就行。 首先修改java_home 然后修改path变量   然后修改classpath变量,如果是win10系统,classpath变量如下 .;%Java_Home%\bin;%J...

JAVA入门介绍1(JDK和Eclipse下载安装)

JAVA入门介绍(JDK和Eclipse下载安装) 1.JDK下载安装 官网下载 :https://www.oracle.com/java/technologies/javase-jdk11-downloads.html 点击JavaSEDownloads: 进入后不建议直接下载最新版本,建议下载长期支持版本(LTS): 选择自己平台下的版本(这里以window x64为例): 这里需要注册账号并...

CSRF——跨站请求伪造

CSRF全拼为Cross Site Request Forgery,译为跨站请求伪造。 CSRF指攻击者盗用了你的身份,以你的名义发送恶意请求。 造成的问题:个人隐私泄露以及财产安全。 图解: CSRF请求伪造过程: 用户C登录正常网站A,网站A校验登录成功之后,在session中记住了用户C的登录状态。 网站A返回应答给用户C时,让用户C的浏览器保存一个cookie信息,比如名为sessioni...

LINUX Redhat www服务器

什么是www? www是world wide web的缩写,也就是全球信息广播的意思。通常说的上网就是使用www来查询用户所需要的信息。www可以结合文字、图形、影像以及声音等多媒体,并通过可以让鼠标单击超链接的方式将信息以Internet传递到世界各处去。 与其他服务器类似,当你连接上www网站,该网站肯定会提供一些数据,而你的客户端则必须要使用可以解析这些数据的软件来处理,那就是浏览器。www...

问答精选

knitr: keep cache when I make small change in chunk

I understandably broke cache when updating a chunk (however the result should be the same, it was cosmetic changes). However, I do not want to run the chunk again because it takes 1 week to run. How c...

Parsing reddit json into Python array and print items from array

This is my first couple of weeks coding; apologies for a basic question. I've managed to parse the 'WorldNews' subreddit json, identify the individual children (24 of them as I write) and grab the tit...

How would I make it so that a border only spans a certain length, on one side?

How would I make it so that a border only spans a certain length, on one side? I tried and the css: You can try the line-height hack: The pseudo-element hack:...

Modalize does not override react navigation

Each time the user taps on a specific element I want to appear a modalize that describes everything of what the user tapped, I am using Modalize and tried to use portalize but I can't make the modaliz...

Duplicate code from identical objects in separate namespaces

I am writing a console app to make managing a local SSRS instance easier using the ReportExecution2005 and ReportService2010 libraries each providing different functions. To work with existing reports...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答