使用openxml提取word中的文本和圖片 使用 openXml 提取 word 中的 Text 和 Drawing 使用 openXml 將 word 中的文本和圖片轉為Html 使用 openXml 將 word 中的 文本 和 圖片 轉為 Html 註:只支持內嵌,不支持公式 文章最後為效果 ...
使用openxml提取word中的文本和圖片
使用 openXml 提取 word 中的 Text 和 Drawing
使用 openXml 將 word 中的文本和圖片轉為Html
使用 openXml 將 word 中的 文本 和 圖片 轉為 Html
註:只支持內嵌,不支持公式
------------------------------------------------
---------------文章最後為效果------------
------------------------------------------------
加入包:OpenXml
創建文件:Read.cs
複製下麵全部代碼到文件 Read.cs
using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; using System.Xml.Xsl; namespace YCBX.Office.WordXml { public class Read { public List<string> ParagraphHtmlAll(string wordPathStr) { return ParagraphHtmlAll(new FileStream(wordPathStr, FileMode.Open)); } public List<string> ParagraphHtmlAll(Stream wordStream) { using (WordprocessingDocument doc = WordprocessingDocument.Open(wordStream, false)) { //XmlWriterSettings settings = new XmlWriterSettings() { OmitXmlDeclaration = true, ConformanceLevel = ConformanceLevel.Auto,DoNotEscapeUriAttributes=true}; List<string> paragraphHtmls = new List<string>(); MainDocumentPart mainPart = doc.MainDocumentPart; Body body = doc.MainDocumentPart.Document.Body; //段落 foreach (var paragraph in body.Elements<Paragraph>()) { StringBuilder paragraphHtml = new StringBuilder(); //塊 foreach (var run in paragraph.ChildElements) { if (run is Run) { foreach (OpenXmlElement openXmlElement in run.Elements()) { //文字塊 if (openXmlElement is Text) { Text text = openXmlElement as Text; paragraphHtml.Append(text.Text); } //圖像塊 else if (openXmlElement is Drawing) { Drawing drawing = openXmlElement as Drawing; //得到圖像的內嵌ID(外嵌沒做處理) var inline = drawing.Inline; var extent = inline.Extent; var pic = inline.Graphic.GraphicData.GetFirstChild<DocumentFormat.OpenXml.Drawing.Pictures.Picture>(); var embed = pic.BlipFill.Blip.Embed.Value; //得到圖像流 var part = mainPart.GetPartById(embed); var stream = part.GetStream(); //流轉2進位 byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); //2進位轉base64 string imgHtml = $"<img style='width:{ImageExtent.EMU_TO_PX((decimal)extent.Cx.Value).ToString("0.")}px;height:{ImageExtent.EMU_TO_PX((decimal)extent.Cy.Value).ToString("0.")}px' src='data:{part.ContentType};base64," + Convert.ToBase64String(bytes) + "' />"; paragraphHtml.Append(imgHtml); } } } //else if(run is DocumentFormat.OpenXml.Math.OfficeMath math) //{ // var x = new XmlDocument(); // x.LoadXml(math.OuterXml); // using var ms = ConvertToMatchMl(x, settings); // paragraphHtml.Append(ConvertToLatex(settings, ms)); //} } paragraphHtmls.Add(paragraphHtml.ToString()); } return paragraphHtmls; } } private string ConvertToLatex(XmlWriterSettings settings, Stream ms) { var latexTransform = new XslCompiledTransform(); latexTransform.Load(Path.Combine(AppContext.BaseDirectory, "xsltml", "mmltex.xsl"), new XsltSettings(true,true),new XmlUrlResolver() ); using var la = new MemoryStream(); latexTransform.Transform(new XmlTextReader(ms), XmlWriter.Create(la, settings)); la.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(la, Encoding.UTF8); return sr.ReadToEnd(); } private Stream ConvertToMatchMl(XmlDocument xmlDocument, XmlWriterSettings settings) { var ms = new MemoryStream(); var xslTransform = new XslCompiledTransform(); xslTransform.Load(Path.Combine(AppContext.BaseDirectory, "xsltml", "OMML2MML.XSL")); xslTransform.Transform(xmlDocument, XmlWriter.Create(ms, settings)); ms.Seek(0, SeekOrigin.Begin); return ms; } } }
創建文件:ImageExtent.cs
複製下麵全部代碼到文件 ImageExtent.cs
using System; using System.Collections.Generic; using System.Text; namespace YCBX.Office.WordXml { /// <summary> /// 圖像長度單位轉換 /// </summary> public class ImageExtent { private const decimal CM_TO_PX = 96M; private const decimal INCH_TO_CM = 2.54M; /// <summary> /// 釐米到EMU(English Metric Unit) /// </summary> private const decimal CM_TO_EMU = 360000M; /// <summary> /// EMU(English Metric Unit) 到像素(px) /// </summary> /// <param name="EMU"></param> public static decimal EMU_TO_PX(decimal EMU) { return EMU / CM_TO_EMU / INCH_TO_CM * CM_TO_PX; } } }
調用方法:
var sss = new Read().ParagraphHtmlAll("1.docx");
word文件中為:
創建一個test.html,將代碼放在<body></body>中,查看效果為:
完成