2014-04-15 58 views
0

我試圖把圖像放在Word Docx的標題中,但沒有任何運氣。在MS網站(http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.wordprocessing.horizontalposition(v=office.14).aspx),我發現了一些看起來像是在中心對齊的東西,但我不知道該把它放在哪裏。該代碼是:C#OpenXML標題與圖像中心對齊

<wp:anchor … > 
<wp:positionH relativeFrom="margin"> 
<wp:align>center</wp:align> 
</wp:positionH> 
<wp:positionV relativeFrom="margin"> 
<wp:align>center</wp:align> 
</wp:positionV> 
</wp:anchor> 

我的xml文檔:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<w:hdr xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"  xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"> 
<w:p w:rsidR="00A65029" w:rsidRDefault="00240387"> 
<w:pPr> 
    <w:pStyle w:val="Header"/> 
</w:pPr> 
<w:r> 
    <w:rPr> 
    <w:noProof/> 
    </w:rPr> 
    <w:drawing> 
    <wp:inline distT="0" distB="0" distL="0" distR="0"> 
     <wp:extent cx="1708000" cy="700000"/> 
     <wp:effectExtent l="19050" t="0" r="0" b="0"/> 
     <wp:docPr id="1" name="Picture 0" descr="lms.gif"/> 
     <wp:cNvGraphicFramePr> 
     <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/> 
     </wp:cNvGraphicFramePr> 
     <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="lms.gif"/> 
       <pic:cNvPicPr/> 
      </pic:nvPicPr> 
      <pic:blipFill> 
       <a:blip r:embed="rId1"/> 
       <a:stretch> 
       <a:fillRect/> 
       </a:stretch> 
      </pic:blipFill> 
      <pic:spPr> 
       <a:xfrm> 
       <a:off x="0" y="0"/> 
       <a:ext cx="1281000" cy="525000"/> 
       </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:r w:rsidR="00A65029"> 
    <w:t></w:t> 
</w:r> 
</w:p> 
<w:p w:rsidR="00A65029" w:rsidRDefault="00A65029"> 
<w:pPr> 
    <w:pStyle w:val="Header"/> 
</w:pPr> 

和C#:

const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; 
     const string relationshipNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; 
     string reference = "w:headerReference"; 
     using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, true)) 
     { 
      MainDocumentPart mainPart = wordDoc.MainDocumentPart; 
      XmlDocument xDoc = new XmlDocument(); 
      xDoc.Load(mainPart.GetStream()); 

      HeaderPart headPart = mainPart.AddNewPart<HeaderPart>(); 



      XmlDocument header = new XmlDocument(); 
      header.Load(@"..\..\HeaderWithImage.xml"); 

      header.Save(headPart.GetStream()); 

      ImagePart imgpart = headPart.AddImagePart(ImagePartType.Gif); 

      //add image file to the image part 
      using (Stream targetStream = imgpart.GetStream()) 
      { 
       using (FileStream sourceStream = new FileStream(@"C:\Users\PDI\Documents\fotos numafa\producten\Logo.png", 
         FileMode.Open, FileAccess.Read)) 
       { 
        byte[] buffer = new byte[1024]; 
        int nrBytesWritten = sourceStream.Read(buffer, 0, 1024); 
        while (nrBytesWritten > 0) 
        { 
         targetStream.Write(buffer, 0, nrBytesWritten); 
         nrBytesWritten = sourceStream.Read(buffer, 0, 1024); 
        } 
       } 
      } 

      string imgId = headPart.GetIdOfPart(imgpart); 

      string relId = mainPart.GetIdOfPart(headPart); 

      NameTable nt = new NameTable(); 
      XmlNamespaceManager nsManager1 = new XmlNamespaceManager(nt); 

      nsManager1.AddNamespace("a", "http://schemas.openxmlformats.org/drawingml/2006/main"); 



      XmlNode blip = header.SelectSingleNode("//a:blip", nsManager1); 
      XmlAttribute embed = blip.Attributes["embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"]; 
      embed.Value = imgId; 

      header.Save(headPart.GetStream()); 


      XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); 

      nsManager.AddNamespace("w", wordmlNamespace); 


      XmlNode targetNode = xDoc.SelectSingleNode("//w:sectPr", nsManager); 


      XmlElement node = xDoc.CreateElement(reference, wordmlNamespace); 

      XmlAttribute attr = node.Attributes.Append(xDoc.CreateAttribute("r:id", relationshipNamespace)); 

      attr.Value = relId; 

      // node.Attributes.Append(attr); 

      targetNode.InsertBefore(node, targetNode.FirstChild); 

      xDoc.Save(mainPart.GetStream()); 

      wordDoc.Close(); 

     } 

回答