带有图像中心对齐的C#OpenXML标头

带有图像中心对齐的C#OpenXML标头,c#,image,center,openxml,C#,Image,Center,Openxml,我试图在Word Docx的标题中添加一个图像,但运气不好。在微软网站()上,我发现了一个看起来像是在中央对齐的东西,但我不知道该放在哪里。代码是: <wp:anchor … > <wp:positionH relativeFrom="margin"> <wp:align>center</wp:align> </wp:positionH> <wp:positionV relativeFrom="margin"> <wp

我试图在Word Docx的标题中添加一个图像,但运气不好。在微软网站()上,我发现了一个看起来像是在中央对齐的东西,但我不知道该放在哪里。代码是:

<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";
常量字符串relationshipNamespace=”http://schemas.openxmlformats.org/officeDocument/2006/relationships";
string reference=“w:headerReference”;
使用(WordprocessingDocument wordDoc=WordprocessingDocument.Open(文件名,true))
{
MainDocumentPart mainPart=wordDoc.MainDocumentPart;
XmlDocument xDoc=新的XmlDocument();
Load(mainPart.GetStream());
HeaderPart headPart=mainPart.AddNewPart();
XmlDocument header=新的XmlDocument();
header.Load(@.\..\HeaderWithImage.xml”);
header.Save(headPart.GetStream());
ImagePart imgpart=headPart.AddImagePart(ImagePartType.Gif);
//将图像文件添加到图像部件
使用(Stream targetStream=imgpart.GetStream())
{
使用(FileStream sourceStream=new FileStream(@“C:\Users\PDI\Documents\fotos numafa\producten\Logo.png”,
FileMode.Open,FileAccess.Read)
{
字节[]缓冲区=新字节[1024];
int nrbyteswrited=sourceStream.Read(缓冲区,0,1024);
而(nrbyteswrited>0)
{
写入(缓冲区,0,nrbyteswrite);
nrbyteswrite=sourceStream.Read(缓冲区,0,1024);
}
}
}
字符串imgId=headPart.GetIdOfPart(imgpart);
string relId=mainPart.GetIdOfPart(头部);
NameTable nt=新的NameTable();
XmlNamespaceManager nsManager1=新的XmlNamespaceManager(nt);
nsManager1.AddNamespace(“a”http://schemas.openxmlformats.org/drawingml/2006/main");
XmlNode blip=header.SelectSingleNode(//a:blip),nsManager1);
XmlAttribute嵌入=blip.Attributes[“嵌入”http://schemas.openxmlformats.org/officeDocument/2006/relationships"];
嵌入.Value=imgId;
header.Save(headPart.GetStream());
XmlNamespaceManager nsManager=新的XmlNamespaceManager(nt);
nsManager.AddNamespace(“w”,wordmlNamespace);
XmlNode targetNode=xDoc.SelectSingleNode(//w:sectPr),nsManager);
XmlElement节点=xDoc.CreateElement(引用,wordmlNamespace);
XmlAttribute attr=node.Attributes.Append(xDoc.CreateAttribute(“r:id”,relationshipNamespace));
属性值=relId;
//node.Attributes.Append(attr);
InsertBefore(节点,targetNode.FirstChild);
Save(mainPart.GetStream());
wordDoc.Close();
}

下载OpenXML生产力工具:或

在Word中创建您需要的内容


在工具中打开docx,复制您需要的代码/它显示的代码。

谢谢!很好!
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();

        }