使用的OpenXML在Word文档替换图片 [英] Replace image in word doc using OpenXML

查看:1645
本文介绍了使用的OpenXML在Word文档替换图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的最后一个问题继<一href="http://stackoverflow.com/questions/2804531/create-word-document-and-add-image-from-net-app">here

Following on from my last question here

的OpenXML看起来像它可能不正是我想要的,但文档是可怕的。谷歌搜索小时还没让我更接近搞清楚什么我需要做的。

OpenXML looks like it probably does exactly what I want, but the documentation is terrible. An hour of googling hasn't got me any closer to figuring out what I need to do.

我有一个word文档。我想以这样的方式,我可以再打开的OpenXML文档并替换图像的图像添加到Word文档(使用文字)。应该足够简单,是吗?

I have a word document. I want to add an image to that word document (using word) in such a way that I can then open the document in OpenXML and replace that image. Should be simple enough, yes?

我假设我应该能够给我的形象'占位'某种形式的ID,然后使用 GetPartById 定位的形象和更换。这将是正确的方法?这是什么标识?如何使用Word添加呢?

I'm assuming I should be able to give my image 'placeholder' an id of some sort and then use GetPartById to locate the image and replace it. Would this be the correct method? What is this Id? How do you add it using Word?

每一个例子,我可以找到它通过从头ML,构建整个word文档做任何远程类似的开始这确实不是大量的使用。

Every example I can find which does anything remotely similar starts by building the whole word document from scratch in ML, which really isn't a lot of use.

编辑:它发生,我认为这将是更容易只需更换图像与新图像的媒体文件夹,但同样找不到任何指示如何做到这一点

it occured to me that it would be easier to just replace the image in the media folder with the new image, but again can't find any indication of how to do this.

推荐答案

虽然处理OpenXML文档不是很大,还有就是你可以用它来看看如何利用现有的Word文档是建立一个很好的工具。如果你安装它自带的 DocumentReflector.exe 下的 Open XML格式SDK \ V2.0 \工具的目录。

Although the documentation for OpenXML isn't great, there is an excellent tool that you can use to see how existing Word documents are built. If you install the OpenXml SDK it comes with the DocumentReflector.exe tool under the Open XML Format SDK\V2.0\tools directory.

在Word文档图像包括一个在所述文档的正文中引用被分配给它的图像数据和ID的。这似乎是你的问题可以分解为两个部分:查找图片的文件中,然后的ID 重写它的图像数据

Images in Word documents consist of the image data and an ID that is assigned to it that is referenced in the body of the document. It seems like your problem can be broken down into two parts: finding the ID of the image in the document, and then re-writing the image data for it.

要查找图片的ID,你需要解析MainDocumentPart。图像存储在奔跑为图形元素

To find the ID of the image, you'll need to parse the MainDocumentPart. Images are stored in Runs as a Drawing element

<w:p>
  <w:r>
    <w:drawing>
      <wp:inline>
        <wp:extent cx="3200400" cy="704850" /> <!-- describes the size of the image -->
        <wp:docPr id="2" name="Picture 1" descr="filename.JPG" />
        <a:graphic>
          <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
            <pic:pic>
              <pic:nvPicPr>
                <pic:cNvPr id="0" name="filename.JPG" />
                <pic:cNvPicPr />
              </pic:nvPicPr>
              <pic:blipFill>
                <a:blip r:embed="rId5" /> <!-- this is the ID you need to find -->
                <a:stretch>
                  <a:fillRect />
                </a:stretch>
              </pic:blipFill>
              <pic:spPr>
                <a:xfrm>
                  <a:ext cx="3200400" cy="704850" />
                </a:xfrm>
                <a:prstGeom prst="rect" />
              </pic:spPr>
            </pic:pic>
          </a:graphicData>
        </a:graphic>
      </wp:inline>
    </w:drawing>
  </w:r>
</w:p>

在上面的例子中,需要找到存储在光点元件的图像的ID。你如何去发现,依赖于你的问题,但如果你知道原始图像的文件名,你可以看看docPr元素:

In the above example, you need to find the ID of the image stored in the blip element. How you go about finding that is dependent on your problem, but if you know the filename of the original image you can look at the docPr element:

using (WordprocessingDocument document = WordprocessingDocument.Open("docfilename.docx", true)) {

  // go through the document and pull out the inline image elements
  IEnumerable<Inline> imageElements = from run in Document.MainDocumentPart.Document.Descendants<Run>()
      where run.Descendants<Inline>().First() != null
      select run.Descendants<Inline>().First();

  // select the image that has the correct filename (chooses the first if there are many)
  Inline selectedImage = (from image in imageElements
      where (image.DocProperties != null &&
          image.DocProperties.Equals("image filename"))
      select image).First();

  // get the ID from the inline element
  string imageId = "default value";
  Blip blipElement = selectedImage.Descendants<Blip>().First();
  if (blipElement != null) {
      imageId = blipElement.Embed.Value;
  }
}

然后当你有图像的ID,你可以用它来重写图像数据。我觉得这是你将如何做到这一点:

Then when you have the image ID, you can use that to rewrite the image data. I think this is how you would do it:

ImagePart imagePart = (ImagePart)document.MainDocumentPart.GetPartById(imageId);
byte[] imageBytes = File.ReadAllBytes("new_image.jpg");
BinaryWriter writer = new BinaryWriter(imagePart.GetStream());
writer.Write(imageBytes);
writer.Close();

这篇关于使用的OpenXML在Word文档替换图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆