wordpress hit counter
Re: How to use different header at end of document - Open XML SDK 2.0 - Development Tools - OpenXML Developer

Re: How to use different header at end of document

Development Tools

Discussions about working with Open XML using a wide range of development tools

How to use different header at end of document

  • rated by 0 users
  • This post has 3 Replies |
  • 1 Follower
  • Hi all,

    I am generating a docx document, that is using a header and a footer for the first page, and then a different header and footer for the rest of the document. Now, I want to add, as an appendix, a new page at the end of the generated document, without using the header and footer defined for the whole document. Ideally, it would be same header and footer as in the first page (if not possible, I'll just add the logo to the content).

    I am sure this is something that someone has had to come accross at some point ! Well, at least, I hope so !

    Here is my code :

                    using (WordprocessingDocument package = WordprocessingDocument.Create(this.saveFileDialog1.FileName, WordprocessingDocumentType.Document))
                    {
                        #region initialization of the document
                        // add a main part
                        MainDocumentPart mainDocumentPart = package.AddMainDocumentPart();
                        
                        // Delete the existing header and footer parts.
                        mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts);
                        mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts);
                        mainDocumentPart.DeleteParts(mainDocumentPart.ImageParts);
    
                        // create the document object
                        Document document = new Document();
    
                        // declare the elements of the body
                        List<OpenXmlElement> tabItems = new List<OpenXmlElement>();
                        #endregion
                                           
                        #region content
                        // add a new xml element for the section 1
                        tabItems.Add(new Paragraph(new Run(...)));
    					
    					[...]
    					
    					#endregion
    
                        #region define the section properties
                        SectionProperties sectPtr =
                        new SectionProperties(
                            new HeaderReference()
                            {
                                Type = HeaderFooterValues.First,
                                Id = "rId2"
                            },
                            new HeaderReference()
                            {
                                Type = HeaderFooterValues.Default,
                                Id = "rId3"
                            },
                            new FooterReference()
                            {
                                Type = HeaderFooterValues.First,
                                Id = "rId4"
                            },
                            new FooterReference()
                            {
                                Type = HeaderFooterValues.Default,
                                Id = "rId5"
                            },
                            new PageMargin()
                            {
                                Left = (UInt32Value)980U,
                                Right = (UInt32Value)980U,
                                Header = (UInt32Value)70U,
                                Footer = (UInt32Value)300U
                            },
                            new TitlePage()
                        );
    
                        tabItems.Add(sectPtr);
                        #endregion
    
                        #region header
                        // first page header
                        var firstPageHeaderPart = mainDocumentPart.AddNewPart<HeaderPart>("rId2");
                        firstPageHeaderPart.DeleteParts(firstPageHeaderPart.ImageParts);
                        ImagePart firstPageImagePart = firstPageHeaderPart.AddImagePart(ImagePartType.Jpeg);
                        using (Stream imgStream = firstPageImagePart.GetStream())
                        {
                            System.Drawing.Bitmap logo = DocumentResources.titlegraphic;
                            logo.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        GenerateTablesWord.GenerateHeader(true, fontSize, firstPageHeaderPart.GetIdOfPart(firstPageImagePart), engineNickname).Save(firstPageHeaderPart);
                        
                        // other pages header
                        var otherPageheaderPart = mainDocumentPart.AddNewPart<HeaderPart>("rId3");
                        otherPageheaderPart.DeleteParts(otherPageheaderPart.ImageParts);
                        ImagePart oddPageImagePart = otherPageheaderPart.AddImagePart(ImagePartType.Jpeg);
                        using (Stream imgStream = oddPageImagePart.GetStream())
                        {
                            System.Drawing.Bitmap logo = DocumentResources.titlegraphic;
                            logo.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        GenerateTablesWord.GenerateHeader(false, fontSize, otherPageheaderPart.GetIdOfPart(oddPageImagePart), engineNickname).Save(otherPageheaderPart);
                        #endregion
    
                        #region footer
                        // first page footer
                        var firstPageFooterPart = mainDocumentPart.AddNewPart<FooterPart>("rId4");
                        GenerateTablesWord.GenerateFooter("Form 343", "Rev 1", "07/09/10", fontSize).Save(firstPageFooterPart);
    
                        // other pages footer
                        var otherPageFooterPart = mainDocumentPart.AddNewPart<FooterPart>("rId5");
                        GenerateTablesWord.GenerateFooter("Form 343", "Rev 1", "07/09/10", fontSize).Save(otherPageFooterPart);
                        #endregion
                        
                        // bring the body to the document
                        Body body = new Body(tabItems);
                        document.Append(body);
    
                        // save as a new document
                        document.Save(mainDocumentPart);
                    }

    Any help / direction would be appreciated !

    Thank you !

  • You will need to insert a w:sectPr in the last paragraph in the page before the last page.  This w:sectPr will contain references to the headers/footers for the main body of the document.

    You then can modify the last w:sectPr element in the body of the document to contain a reference to whatever headers/footers you like, including references to a header or footer that is referred to by other sections in the document.  This will cause the last page in your document to refer to whatever header/footer you like.

    -Eric

  • Hi Eric,

    First of all, thank you for taking the time to help me.

    I'm not sure I understood everything. So far, what I did was to add, at the end of the list of elements to add to the Body, a new Page Break with, like you said, a new SectionProperties with differents Ids, and then finally the appendix:

    SectionProperties secprop = new SectionProperties(
    new HeaderReference()
    {
    	Type = HeaderFooterValues.Default,
    	Id = "rId6" // new ID, not in use before
    },
    new FooterReference()
    {
    	Type = HeaderFooterValues.Default,
    	Id = "rId7" // new ID, not in use before
    });
    
    tabItems.Add(new Paragraph(secprop, new Run(new Break() { Type = BreakValues.Page }));
    
    Paragraph appendix = new Paragraph(new Run(GenerateTablesWord.GenerateAppendix(fontSize)));

    I'm not sure now about what you mean by "modify the last SectionProperties element to contain a reference to whatever header/footer you like" ? Ideally, I would reuse the Header "rId2" and the Footer "rId4" but I'm not sure how to modify this SectionProperties ?

    Thanks again !

    Alex

  • Hi guys,

    I worked out what the problem was. So, for those who would be interested in the future, I explain what worked for me.

    In my instance, my mistake was that I did not insert the SectionProperties into a Paragraph/ParagraphProperties :


                        tabItems
    .Add(sectPtr);

    So after correcting it to :
    tabItems.Add(new Paragraph(new ParagraphProperties(sectPtr)));


    I have been able to define a new section and apply a new header/footer to it :

    SectionProperties secprop = new SectionProperties(
    new HeaderReference()
    {
    	Type = HeaderFooterValues.First,
    	Id = "rId6"
    });
    
    var annexHeaderFirstPart = mainDocumentPart.AddNewPart<HeaderPart>("rId6");
    (new Header(new Text("Annex Header"))).Save(annexHeaderFirstPart);
    
    Paragraph p = new Paragraph(new Run(new Text("Annex Text")));
    tabItems.Add(p);
    
    // add the new section properties
    tabItems.Add(secprop);
    It worked for me !

    Cheers,
    Alex
Page 1 of 1 (4 items)