wordpress hit counter
adding header using OpenXML SDK2.0 - WordprocessingML - Formats - OpenXML Developer

adding header using OpenXML SDK2.0

Formats

Discussions about working with different Open XML Formats

adding header using OpenXML SDK2.0

  • rated by 0 users
  • This post has 7 Replies |
  • 1 Follower
  • can anyone tell how to add header and footer to a wordML document?with an example
  • code:

    using (WordprocessingDocument package = WordprocessingDocument.Create(docName, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
               {

                    package.AddMainDocumentPart();

                    package.MainDocumentPart.Document =
                        new Document(
                            new Body(new Header(new Paragraph(
                                    new Run(
                                        new Text("hello"))))));

                    package.MainDocumentPart.Document.Save();
                   }
    i tried with the above code....but the header is not visible in the document.what be the solution for this?


  • Try adding a new PackagePart. The code sample here may help although you need to abstract it from the SharePoint usage:

    http://blogs.msdn.com/pranab/archive/2008/12/18/sharepoint-2007-moss-wss-adding-header-and-footer-in-word-document-docx-with-itemadded-event-receiver-using-openxml.aspx



    HTH - Philip
  • you may take a look at this and refer to the "create" part of the code.

    note: this code is on top of SDK 1.0.

    hope it helps:)

  • I managed to get a header added to my document using this method (with SDK 2.0). Adding a footer should be a similar process.

    public void CreateDocument()
    {
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Create("test.docx", WordprocessingDocumentType.Document))
    {
          MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
          Document d = new Document();
          Body b = new Body();
          Table tbl = new Table();
          TableProperties tblPr = new TableProperties();
          TableCellMarginDefault tblCellMar = new TableCellMarginDefault();
          tblCellMar.TopMargin = new TopMargin { Width = 0, Type = TableWidthUnitValues.Dxa };
          tblCellMar.RightMargin = new RightMargin { Width = 120, Type = TableWidthUnitValues.Dxa };
          tblCellMar.BottomMargin = new BottomMargin { Width = 0, Type = TableWidthUnitValues.Dxa };
          tblCellMar.LeftMargin = new LeftMargin { Width = 120, Type = TableWidthUnitValues.Dxa };
          TableRow tr = new TableRow();
          TableCell tc = new TableCell();
          Paragraph p = new Paragraph();
          Run r = new Run();
          Text t = new Text();
          t.Text = "This is text in a table cell.";
          r.Append(t);
          p.Append(r);
          tc.Append(p);
          tr.Append(tc);
          tblPr.Append(tblCellMar);
          tbl.Append(tblPr);
          tbl.Append(tr);
          b.Append(tbl);
          d.Append(b);

          HeaderPart hp = mainPart.AddNewPart<HeaderPart>();
          string relId = mainPart.GetIdOfPart(hp);
          SectionProperties sectPr = new SectionProperties();
          HeaderReference headerReference = new HeaderReference();
          headerReference.Id = relId;
          sectPr.Append(headerReference);

          d.Append(sectPr);
          mainPart.Document = d;
          mainPart.Document.Save();
          hp.Header = GetHeader();
          hp.Header.Save();

          wordDoc.Close();
       }
    }

    public Header GetHeader()
    {
       Header h = new Header();
       Paragraph p = new Paragraph();
       Run r = new Run();
       Text t = new Text();
       t.Text = "This is the header.";
       r.Append(t);
       p.Append(r);
       h.Append(p);
       return h;
    }

    Stuart.

  • thank you stuart....it works

  • Noob question but does this work for modifying an existing document?

    all I'm doing that is different is instead of this

          d.Append(sectPr);
          mainPart.Document = d;
          mainPart.Document.Save();

    I simply do this

          mainPart.Document.Append(hdrSecProps);
          mainPart.Document.Save();

    where mainPart is part from an open existing document that I'm doing some simple content control changes to. When I run the code no header shows. I've tried a few things including removing header parts before I add this header part but no luck.

    Thanks

    ~>Z
  • Hello Z - I know this is an old post but I am working through the exact same issue. I made the same change you did to update the header of an existing document. Were ever able to find a solution?

    Thanks.

    J
Page 1 of 1 (8 items)