简体   繁体   English

Word 2007,Open XML - 嵌入图像

[英]Word 2007, Open XML - embedding an image

Do you know what basic information MUST include a paragraph ( <w:p/> ) in document.xml inside a *.docx document, that specifies an image? 你知道什么基本信息必须在* .docx文档中的document.xml中包含一个段落( <w:p/> ),它指定一个图像吗? I do know there must be: 我知道必须有:

<a:blip r:embed="rId4" /> 

specifing the relationship id, but what else? 指定关系id,但还有什么? It's very hard to find it in Google and experimenting with cutting out tags from a ready document or reading the specification takes a lot of time. 很难在Google中找到它并尝试从现成文档中删除标签或阅读规范需要花费大量时间。

An example with all the required tags would be greatly appreciated. 将非常感谢所有必需标签的示例。

Word is rather picky concerning the input XML provided. 关于提供的输入XML,Word相当挑剔。 To embed an image, you have to provide quite some information. 要嵌入图像,您必须提供相当多的信息。 Here is a simple example: 这是一个简单的例子:

document.xml document.xml中

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" 
            xmlns:v="urn:schemas-microsoft-com:vml" 
            xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" 
            xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:body>
    <w:p>
      <w:r>
        <w:drawing>
          <wp:inline distT="0" distB="0" distL="0" distR="0">
            <wp:extent cx="5943600" cy="3717290"/>
            <wp:docPr id="1" name="Picture 0" descr="vlcsnap-325726.png"/>
            <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
              <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                  <pic:nvPicPr>
                    <pic:cNvPr id="0" name="myImage.png"/>
                    <pic:cNvPicPr/>
                  </pic:nvPicPr>
                  <pic:blipFill>
                    <a:blip r:embed="rId4"/>
                    <a:stretch>
                      <a:fillRect/>
                    </a:stretch>
                  </pic:blipFill>
                  <pic:spPr>
                    <a:xfrm>
                      <a:off x="0" y="0"/>
                      <a:ext cx="5943600" cy="3717290"/>
                    </a:xfrm>
                    <a:prstGeom prst="rect">
                      <a:avLst/>
                    </a:prstGeom>
                  </pic:spPr>
                </pic:pic>
              </a:graphicData>
            </a:graphic>
          </wp:inline>
        </w:drawing>
      </w:r>
    </w:p>
  </w:body>
</w:document>

document.xml.rels document.xml.rels

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <!-- other relationships go here -->
  <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/>
</Relationships>

And of course the image must be added to the package at the correct location (media/image1.png) 当然,必须将图像添加到正确位置的包装中(media / image1.png)

Since all this is rather complicated I would recommend you to use the OpenXML SDK 2.0 provided by Microsoft or another library, eg OpenXML4J . 由于所有这些都相当复杂,我建议您使用Microsoft提供的OpenXML SDK 2.0或其他库,例如OpenXML4J These libraries, especially the one from Microsoft can make your work a lot easier. 这些库,尤其是Microsoft的库,可以使您的工作更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM