我需要将DOCX文件中的文本和图像提取到文本文件中(当然将图像保存为图形文件)。 使用下面的代码如何获取图像并使用文本文件中的引用保存它?
如果我使用:
List<ImagePart> imgPart = wordProcessingDoc.MainDocumentPart.ImageParts.ToList();
我可以获得所有图像,但有时一个图像在几个地方使用。我找不到从列表中获取该特定图像的引用。 以下是取自(Extract table from DOCX)的示例代码:
public static string ReadAllTextFromDocx(FileInfo fileInfo)
{
StringBuilder stringBuilder;
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fileInfo.FullName, false))
{
NameTable nameTable = new NameTable();
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(nameTable);
xmlNamespaceManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
string wordprocessingDocumentText;
using (StreamReader streamReader = new StreamReader(wordprocessingDocument.MainDocumentPart.GetStream()))
{
wordprocessingDocumentText = streamReader.ReadToEnd();
}
stringBuilder = new StringBuilder(wordprocessingDocumentText.Length);
XmlDocument xmlDocument = new XmlDocument(nameTable);
xmlDocument.LoadXml(wordprocessingDocumentText);
XmlNodeList paragraphNodes = xmlDocument.SelectNodes("//w:p", xmlNamespaceManager);
foreach (XmlNode paragraphNode in paragraphNodes)
{
XmlNodeList textNodes = paragraphNode.SelectNodes(".//w:t | .//w:tab | .//w:br | .//w:drawing", xmlNamespaceManager);
foreach (XmlNode textNode in textNodes)
{
switch (textNode.Name)
{
case "w:t":
stringBuilder.Append(textNode.InnerText);
break;
case "w:tab":
stringBuilder.Append("\t");
break;
case "w:br":
stringBuilder.Append("\v");
break;
case "w:drawing":
stringBuilder.Append("----------------IMAGE HERE-------------");
break;
}
}
stringBuilder.Append(Environment.NewLine);
}
}
return stringBuilder.ToString();
}
答案 0 :(得分:1)
我在这篇文章中找到了答案: Replace image in word doc using OpenXML
var imageParts =from graphic in par.Descendants<DocumentFormat.OpenXml.Drawing.Graphic>()
let graphicData = graphic.Descendants<DocumentFormat.OpenXml.Drawing.GraphicData>().FirstOrDefault()
let pic = graphicData.ElementAt(0)
let nvPicPrt = pic.ElementAt(0).FirstOrDefault()
let blip = pic.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault()
select new
{
Id = blip.GetAttribute("embed", xr).Value,
Filename = nvPicPrt.GetAttribute("name", xpic).Value
};
答案 1 :(得分:1)
您可以使用从ImagePart获取图像流。
JSON_UNESCAPED_SLASHES