C# OpenXml从Word文档转换为带标题的HTML
我想阅读一个.docx文件,并将其内容作为电子邮件正文而不是附件发送到电子邮件中 为此,我使用openXML和OpenXmlPowerTools将docx文件转换为html。这几乎可以正常工作,直到我得到一个带有图像的页眉和页脚的文档 这是我将.docx转换为Html的代码C# OpenXml从Word文档转换为带标题的HTML,c#,asp.net-mvc,openxml,C#,Asp.net Mvc,Openxml,我想阅读一个.docx文件,并将其内容作为电子邮件正文而不是附件发送到电子邮件中 为此,我使用openXML和OpenXmlPowerTools将docx文件转换为html。这几乎可以正常工作,直到我得到一个带有图像的页眉和页脚的文档 这是我将.docx转换为Html的代码 using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true)) {
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文档转换为HTML时忽略页眉和页脚,因此它们不会显示在生成的HTML中(您可以在github上查看)
可能是因为“页面”的概念不适用于HTML,因此没有明显的等同于文档页眉的概念。OpenXmlPowerTools在将docx文档转换为HTML时忽略页眉和页脚,因此它们不会显示在生成的HTML中(可以在github上)
可能是因为“页面”的概念不适用于HTML,所以没有明显的等同于文档标题。(我知道这有点不相关)您使用OpenXML SDK而不是MS Word Interop Assembly有什么特别的原因吗?@AzazulHaq我认为MS Word Interop Assembly需要在您的机器上安装MS Office,所以我避免这样做。(我知道这有点不相关)您使用OpenXML SDK而不是MS Word Interop Assembly有什么特别的原因吗?@AzazulHaq我认为MS Word Interop Assembly需要在您的机器上安装MS Office,所以我避免这样做。