I have many word documents(more than 10k) which have images(logo) in them. I want to replace the logo that i have in the word doc with another image. Some of these word files may not even have images in them and some may have multiple images in them. Images are not necessarily in the header section of the doc.
I have gone through some questions on this in Stackoverflow. Mainly this one
But being new to OpenXML
, I'm currently not even able to replace the images inside a single word doc. The problem I'm facing is on trying to replace an image in my word doc, it seems to work fine, but there is no change in the Word Doc whatsoever. Any help would be appreciated.
This is the code I've tried so far
byte[] docBytes = File.ReadAllBytes(_myFilePath);
using (MemoryStream ms = new MemoryStream())
{
ms.Write(docBytes, 0, docBytes.Length);
using (WordprocessingDocument wpdoc = WordprocessingDocument.Open(ms, true))
{
MainDocumentPart mainPart = wpdoc.MainDocumentPart;
Document doc = mainPart.Document;
IEnumerable<Drawing> drawings = mainPart.Document.Descendants<Drawing>().ToList();
foreach (Drawing drawing in drawings)
{
DocProperties dpr = drawing.Descendants<DocProperties>().FirstOrDefault();
if (dpr != null && dpr.Name == "Picture 1")
{
foreach (DocumentFormat.OpenXml.Drawing.Blip b in drawing.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().ToList())
{
OpenXmlPart imagePart = wordDoc.MainDocumentPart.GetPartById(b.Embed);
using (var writer = new BinaryWriter(imagePart.GetStream()))
{
writer.Write(File.ReadAllBytes(Path to my image with which to replace));
}
}
}
}
}
}
This produces no change in the single document i'm trying it for. Also I was wondering how this can be done for docs with multiple images in the above case I opened the xml file and saw that the filename in doc properties was "Picture 1", but for word docs with multiple images, this won't be possible. Any help will be appreciated. Thanks
答案 0 :(得分:1)
您不保存已创建的文档。您的文档当前位于内存流中,然后必须将内存流写入文件系统,然后可以从那里打开它。您正在寻找类似以下的内容:
byte[] docBytes = File.ReadAllBytes(_myFilePath);
using (MemoryStream ms = new MemoryStream())
{
ms.Write(docBytes, 0, docBytes.Length);
using (WordprocessingDocument wpdoc = WordprocessingDocument.Open(ms, true))
{
MainDocumentPart mainPart = wpdoc.MainDocumentPart;
Document doc = mainPart.Document;
IEnumerable<Drawing> drawings = mainPart.Document.Descendants<Drawing>().ToList();
foreach (Drawing drawing in drawings)
{
DocProperties dpr = drawing.Descendants<DocProperties>().FirstOrDefault();
if (dpr != null && dpr.Name == "Picture 1")
{
foreach (DocumentFormat.OpenXml.Drawing.Blip b in drawing.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().ToList())
{
OpenXmlPart imagePart = wordDoc.MainDocumentPart.GetPartById(b.Embed);
using (var writer = new BinaryWriter(imagePart.GetStream()))
{
writer.Write(File.ReadAllBytes(Path to my image with which to replace));
}
}
}
}
}
using (FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write))
{
ms.CopyTo(fs);
}
}
考虑一下,您正在阅读文档并将其存储在RAM中。然后,您对其进行操作,并且一旦处置了内存流,它就会消失。您实际上必须将操作过的字节写到某个地方。