![](/img/trans.png)
[英]How to convert Word document created from template by OpenXML into MemoryStream?
[英]OpenXml Convert from Word document to HTML with Header
我想閱讀.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文件中包含頁眉和頁腳。
請建議我。 謝謝!
將docx文檔轉換為HTML時,OpenXmlPowerTools會忽略頁眉和頁腳,因此它們不會顯示在生成的HTML中(您可以在github上瀏覽源代碼 )。
也許是因為“頁面”的概念不適用於HTML,因此沒有明顯的等同於文檔標題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.