如何使用 OpenXML 和 C# 将图像从一个文档复制到另一个文档?

发布于 2025-01-07 09:30:17 字数 3568 浏览 0 评论 0原文

我(OpenXML 新手)需要扩展一个程序来扩展自己的模板。有一个表,其中包含人员姓名和描述。该描述应取自另一文档。这部分适用于未格式化的文本。

现在客户希望在描述部分使用格式化的文本和图片。 格式可以工作,但我无法使图像部分工作。 Word 2007 无法再打开该文档。

我认为这段关系出了问题。我在 XML 中发现了两个部分:

  • 在原始文档中,图像位于路径:/word/media/image1.bin 现在,在生成的文档中,图像位于 /media /image.bin。这会导致问题吗?在 /word/_rels/document.xml 中,路径指向正确的资源: Target="/media/image.bin"
  • Word 似乎失去了“pic”命名空间:在生成的文档中,我有标签: ,, ... 而不是 ,, ...

您能否帮助我解决此问题或为我提供教程资源?因为文档的大部分内容已经存在,所以我在实现这一点方面受到限制(例如,我必须返回一个 TableCell)。

编辑:

我刚刚手动编辑了生成的 document.xml:我用 替换了 并现在看来有效了。所以问题是如何正确生成 元素。

以下是一些有关该关系的代码片段:

...
ImagePart myIP = mainPart.AddImagePart(iP.ContentType);
// mIPRelId is a string which ist later used (blip)
mIPRelId = mainPart.CreateRelationshipToPart(myIP); 

// iP is the imagepart from the source doc...
myIP.FeedData(iP.GetStream());                  
// myDR is the local drawing
// generate Inline should generate the inline part 
myDR.Inline = generateInline(myXMLDrawing, nsManager);
...
// later when generating the blip
if (myChild.Name == "a:blip")
{
    DocumentFormat.OpenXml.Drawing.Blip myBlip = new Blip();
    myBlip.Embed = mIPRelId;
    ...
    myBlipFill.Append(myBlip);
 }

这是生成图片部分的代码片段(命名空间问题):

 ...
 DocumentFormat.OpenXml.Drawing.Picture myPic = 
    new DocumentFormat.OpenXml.Drawing.Picture();
 string myNodes = ".//descendant::pic:nvPicPr | ";
        myNodes += ".//descendant::pic:blipFill | ";
        myNodes += ".//descendant::pic:spPr | ";
        myNodes += ".//following::w:bookmarkEnd";

 XmlNodeList followingNodesList = myXMLPic.
               SelectNodes(myNodes, nsManager);

 myPic.AddNamespaceDeclaration(
                   "pic", 
                   "http://schemas.openxmlformats.org/drawingml/2006/picture");


  // stepping through the descendants and adding them
  // to the picture
  foreach (XmlElement el in followingNodesList)
  {
      if (el.Name == "pic:nvPicPr")
      {
         DocumentFormat.OpenXml.Drawing.NonVisualPictureProperties myNVPP = 
                                                  new NonVisualPictureProperties();
         ...
         myPic.Append(myNVPP);
      }
      ...          
      // Adding the other parts like blipfill....

这是生成的 XML 的代码片段(抱歉格式错误):

 ...
 <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
   <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
     <a:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
       <a:nvPicPr>
         <a:cNvPr id="0" name="MS_HUND.JPG" /> 
         <a:cNvPicPr /> 
       </a:nvPicPr>
       <a:blipFill>
         <a:blip r:embed="R65532bbe7935423e" cstate="print" /> 
         <a:stretch>
           <a:fillRect /> 
         </a:stretch>
       </a:blipFill>
       <a:spPr>
         <a:xfrm>
           <a:off x="0" y="0" /> 
           <a:ext cx="714375" cy="762000" /> 
         </a:xfrm>
         <a:prstGeom prst="rect">
           <a:avLst /> 
         </a:prstGeom>
       </a:spPr>
     </a:pic>
   </a:graphicData>
 </a:graphic>
 ...

I (OpenXML Newbie) need to extend a program which expands own templates. There is a table with person name and a description. The description should be taken from another document. This part works fine with unformatted text.

Now the customer wants to use formatted text and pictures in the description part.
The formatting works, but I couldn't get the image part to work. Word 2007 couldn't open the document any more.

I assume that something with the relationship went wrong. I found two parts in the XML which smell:

  • In the original document the image was in the path: /word/media/image1.bin now in the generated doc the image is in /media/image.bin. Can this cause a problem? In /word/_rels/document.xml the path points to correct ressource: Target="/media/image.bin"
  • Word seems to loose the "pic" namespace: In the generated doc I have the tags: <a:pic>, <a:nvPicPr>, ... instead of <pic:pic>, <pic:ncvPicPr>, ...

Can you please help me, with this issue or point me to a tutorial ressource? Because large parts of the document already exist I am constricted in the way to implement this (e.g. I have to return a TableCell).

Edit:

I just edited the generated document.xml by hand: I replaced the <a:...> with <pic:...> and now it seems to work. So the problem is how to generate the <pic:...> elements correctly.

Here are some code snippets regarding the relationship:

...
ImagePart myIP = mainPart.AddImagePart(iP.ContentType);
// mIPRelId is a string which ist later used (blip)
mIPRelId = mainPart.CreateRelationshipToPart(myIP); 

// iP is the imagepart from the source doc...
myIP.FeedData(iP.GetStream());                  
// myDR is the local drawing
// generate Inline should generate the inline part 
myDR.Inline = generateInline(myXMLDrawing, nsManager);
...
// later when generating the blip
if (myChild.Name == "a:blip")
{
    DocumentFormat.OpenXml.Drawing.Blip myBlip = new Blip();
    myBlip.Embed = mIPRelId;
    ...
    myBlipFill.Append(myBlip);
 }

Here is the snippet that generates the picture part (namespace prob):

 ...
 DocumentFormat.OpenXml.Drawing.Picture myPic = 
    new DocumentFormat.OpenXml.Drawing.Picture();
 string myNodes = ".//descendant::pic:nvPicPr | ";
        myNodes += ".//descendant::pic:blipFill | ";
        myNodes += ".//descendant::pic:spPr | ";
        myNodes += ".//following::w:bookmarkEnd";

 XmlNodeList followingNodesList = myXMLPic.
               SelectNodes(myNodes, nsManager);

 myPic.AddNamespaceDeclaration(
                   "pic", 
                   "http://schemas.openxmlformats.org/drawingml/2006/picture");


  // stepping through the descendants and adding them
  // to the picture
  foreach (XmlElement el in followingNodesList)
  {
      if (el.Name == "pic:nvPicPr")
      {
         DocumentFormat.OpenXml.Drawing.NonVisualPictureProperties myNVPP = 
                                                  new NonVisualPictureProperties();
         ...
         myPic.Append(myNVPP);
      }
      ...          
      // Adding the other parts like blipfill....

Here is a snippet of the generated XML (sorry for the formatting):

 ...
 <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
   <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
     <a:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
       <a:nvPicPr>
         <a:cNvPr id="0" name="MS_HUND.JPG" /> 
         <a:cNvPicPr /> 
       </a:nvPicPr>
       <a:blipFill>
         <a:blip r:embed="R65532bbe7935423e" cstate="print" /> 
         <a:stretch>
           <a:fillRect /> 
         </a:stretch>
       </a:blipFill>
       <a:spPr>
         <a:xfrm>
           <a:off x="0" y="0" /> 
           <a:ext cx="714375" cy="762000" /> 
         </a:xfrm>
         <a:prstGeom prst="rect">
           <a:avLst /> 
         </a:prstGeom>
       </a:spPr>
     </a:pic>
   </a:graphicData>
 </a:graphic>
 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文