Вопрос

I'm trying to put an image in the header of a Word Docx, but not with any luck. On the MS website (http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.wordprocessing.horizontalposition(v=office.14).aspx) i found something what looks like it is aligning in the center, but i have no idea where to put it. The code is:

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

My xml document:

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

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

        }
Это было полезно?

Решение

Download the OpenXML productivity tool: http://www.microsoft.com/en-us/download/details.aspx?id=5124 or http://www.microsoft.com/en-us/download/details.aspx?id=30425

Create what you need in Word.

Open the docx in the tool, replicate the code that you need/it shows.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top