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

时间:2012-02-18 11:39:56

标签: c# image openxml

我(OpenXML Newbie)需要扩展一个扩展自己模板的程序。有一个人名和描述的表。描述应该从另一个文件中获得。这部分适用于未格式化的文本。

现在,客户希望在说明部分中使用格式化文本和图片。 格式化工作,但我无法使图像部分工作。 Word 2007无法再打开文档。

我认为关系出错了。我在XML中发现了两个部分:

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

你可以帮我解决这个问题,还是指点一下教程资源?因为文档的大部分已经存在,所以我在实现它的方式上受到限制(例如,我必须返回一个TableCell)。

编辑:

我刚刚手动编辑了生成的document.xml:我将<a:...>替换为<pic:...>,现在它似乎正常工作。所以问题是如何正确生成<pic:...>元素。

以下是关于这种关系的一些代码片段:

...
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>
 ...

0 个答案:

没有答案
相关问题