【问题标题】:C# OpenXML header with image center alignment具有图像中心对齐的 C# OpenXML 标头
【发布时间】:2014-05-29 18:43:44
【问题描述】:

我正在尝试将图像放在 Word Docx 的标题中,但没有任何运气。在 MS 网站 (http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.wordprocessing.horizontalposition(v=office.14).aspx) 上,我发现了一些看起来像是在中心对齐的东西,但我不知道该放在哪里。代码是:

<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();

        }

【问题讨论】:

    标签: c# image center openxml


    【解决方案1】:

    下载 OpenXML 生产力工具:http://www.microsoft.com/en-us/download/details.aspx?id=5124http://www.microsoft.com/en-us/download/details.aspx?id=30425

    在 Word 中创建您需要的内容。

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

    【讨论】:

    • 谢谢你!效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2018-09-27
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    相关资源
    最近更新 更多