none
replace header and footer in a word document RRS feed

  • Question

  • Hi,

    I want to replace the header and footer of a word document by the header and footer who's contain images from another document (base document).

    I have find this code in the SDK Help document and i have try to adapt this function to replace the footer.

    This code work for the header and not for the footer, i don't understand why because is the same structure !

    The second problem is that code doesn't work with images, what can i do to manage images ?

       public void AddHeaderFooterFrom(string filepathFrom, string filepathTo)
        {
          // Replace header in target document with header of source document.
          using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(filepathTo, true))
          {
            MainDocumentPart mainPart = wdDoc.MainDocumentPart;
    
            // Delete the existing header footer part.
            mainPart.DeleteParts(mainPart.HeaderParts);
            mainPart.DeleteParts(mainPart.FooterParts);
    
            // Create a new header and footer part.
            var headerPart = mainPart.AddNewPart<HeaderPart>();
            var footerPart = mainPart.AddNewPart<FooterPart>();
    
            // Get Id of the headerPart and footerPart.
            string headerId = mainPart.GetIdOfPart(headerPart);
            string footerId = mainPart.GetIdOfPart(footerPart);
    
            // Feed target headerPart and footerPart with source headerPart and footerPart.
            using (WordprocessingDocument wdDocSource = WordprocessingDocument.Open(filepathFrom, true))
            {
              var firstHeader = wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault();
              var firstFooter = wdDocSource.MainDocumentPart.FooterParts.FirstOrDefault();
    
              if (firstHeader != null && firstFooter != null)
              {
                headerPart.FeedData(firstHeader.GetStream());
                footerPart.FeedData(firstFooter.GetStream());
              }
            }
    
            // Get SectionProperties and Replace HeaderReference with new Id.
            var sectPrs = mainPart.Document.Body.Elements<SectionProperties>();
            foreach (var sectPr in sectPrs)
            {
              // Delete existing references to headers.
              //sectPr.RemoveAllChildren<HeaderReference>();
              //sectPr.RemoveAllChildren<FooterReference>();
    
              sectPr.RemoveAllChildren<HeaderFooterReferenceType>();
    
              // Create the new header/footer reference node.
              sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = headerId });
              sectPr.PrependChild<FooterReference>(new FooterReference() { Id = footerId });
            }
          }
        }
    

    Thank you for your help.

    eGm_

    Wednesday, December 22, 2010 2:33 PM

Answers

  • Hi eGm

     

    Sorry I thought that you have done with image part. please try this code.

                        if (firstHeader != null && firstFooter != null)
                        {
                            headerPart.FeedData(firstHeader.GetStream());
                            footerPart.FeedData(firstFooter.GetStream());

                            //test image
                            string pId = string.Empty;
                            foreach (ImagePart imgPart in firstHeader.ImageParts)
                            {
                                pId = firstHeader.GetIdOfPart(imgPart);

                                // make sure to have validation here ie. is Jpeg, Bmp, etc .
                                headerPart.AddImagePart(ImagePartType.Jpeg, pId);
                               

                                // make sure to have validation here :) .
                                headerPart.ImageParts.ToList()[0].FeedData(imgPart.GetStream());
                            }

                        }

    And let us know about the result.

    Vins


    - Vins
    • Marked as answer by eGm Be Monday, December 27, 2010 12:50 PM
    Monday, December 27, 2010 12:27 PM

All replies

  • Hi eGm_

    Hopefully your issues might be in two places of your code. please try to replace my code and try again.

    1.

    Replace

              var firstHeader = wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault();
              var firstFooter = wdDocSource.MainDocumentPart.FooterParts.FirstOrDefault();


    with (of course this not handle validation, but first try it.)

           var firstHeader = wdDocSource.MainDocumentPart.HeaderParts.ToList()[2];
           var firstFooter = wdDocSource.MainDocumentPart.FooterParts.ToList()[2];

     

    2.

    Replace

              // Create the new header/footer reference node.
              sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = headerId });
              sectPr.PrependChild<FooterReference>(new FooterReference() { Id = footerId });

    with

              // Create the new header/footer reference node.
              sectPr.PrependChild<HeaderReference>(new HeaderReference { Type = HeaderFooterValues.Default, Id = headerId });
              sectPr.PrependChild<FooterReference>(new FooterReference() { Type = HeaderFooterValues.Default, Id = footerId });

     

    Resons,

    Normally word file contains 3 Header/Footer parts which represent Default, Odd and Even pages of the document. in you case you getting the Fist Header/Footer which is NOT the default one.

     

    Image problem is might be you didn't copying the respective ImagePart from source document to target document.

    Please let us know if you getting answered.

    Vins


    - Vins
    • Proposed as answer by Bruce Song Monday, December 27, 2010 7:06 AM
    Thursday, December 23, 2010 7:57 AM
  • Thank you for your Help, it was very helpful.

    I have only replace the code :

              // Create the new header/footer reference node.
              sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = headerId });
              sectPr.PrependChild<FooterReference>(new FooterReference() { Id = footerId });

    with

              // Create the new header/footer reference node.
              sectPr.PrependChild<HeaderReference>(new HeaderReference { Type = HeaderFooterValues.Default, Id = headerId });
              sectPr.PrependChild<FooterReference>(new FooterReference() { Type = HeaderFooterValues.Default, Id = footerId });

    And it work perfectly, because in my base document who's contain the header and footer, i it with "Different First Page" in my base document.

    Thank you_

    eGm_

    Thursday, December 23, 2010 8:31 AM
  • I still have my problem with images in header/footer.

    Any one have a idea how to retrieves images in my document who's contains header and footer ?

    Thank you.

    Monday, December 27, 2010 9:20 AM
  • Hi eGm

     

    Sorry I thought that you have done with image part. please try this code.

                        if (firstHeader != null && firstFooter != null)
                        {
                            headerPart.FeedData(firstHeader.GetStream());
                            footerPart.FeedData(firstFooter.GetStream());

                            //test image
                            string pId = string.Empty;
                            foreach (ImagePart imgPart in firstHeader.ImageParts)
                            {
                                pId = firstHeader.GetIdOfPart(imgPart);

                                // make sure to have validation here ie. is Jpeg, Bmp, etc .
                                headerPart.AddImagePart(ImagePartType.Jpeg, pId);
                               

                                // make sure to have validation here :) .
                                headerPart.ImageParts.ToList()[0].FeedData(imgPart.GetStream());
                            }

                        }

    And let us know about the result.

    Vins


    - Vins
    • Marked as answer by eGm Be Monday, December 27, 2010 12:50 PM
    Monday, December 27, 2010 12:27 PM
  • omg, thank you, it's working :D
    • Proposed as answer by Vins007 Monday, December 27, 2010 12:49 PM
    Monday, December 27, 2010 12:47 PM