none
Copy Header code sample not working. RRS feed

  • Question

  • Hi,

    I am trying to add a header to a word document as per code samples on MSDN.

    https://docs.microsoft.com/en-us/office/open-xml/how-to-replace-the-header-in-a-word-processing-document

    I am getting following error after adding the header to the document.

    Word found unreadable content in documentTest1.docx.

    Also, if i select yes to recover doc, the header content is not loading correctly as per original header.

    Getting message picture cant be displayed.

    

    Is this something wrong with the code sample or a bug in the SDK?

    I am using "Microsoft Open XML SDK 2.5".

    Kindly, help.

    Thanks,

    Sachin R. Chavan.


    Sachin R. Chavan.

    Friday, March 20, 2020 11:27 AM

All replies

  • PFB, the code snippet used as per MSDN sample, only modification is "wdDoc.Save()" call.

    public static void AddHeaderFromTo(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 part.
            mainPart.DeleteParts(mainPart.HeaderParts);
    
            // Create a new header part.
            DocumentFormat.OpenXml.Packaging.HeaderPart headerPart =
        mainPart.AddNewPart<HeaderPart>();
    
            // Get Id of the headerPart.
            string rId = mainPart.GetIdOfPart(headerPart);
    
            // Feed target headerPart with source headerPart.
            using (WordprocessingDocument wdDocSource =
                WordprocessingDocument.Open(filepathFrom, true))
            {
                DocumentFormat.OpenXml.Packaging.HeaderPart firstHeader =
        wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault();
    
                wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault();
    
                if (firstHeader != null)
                {
                    headerPart.FeedData(firstHeader.GetStream());
                }
            }
    
            // Get SectionProperties and Replace HeaderReference with new Id.
            IEnumerable<DocumentFormat.OpenXml.Wordprocessing.SectionProperties> sectPrs =
        mainPart.Document.Body.Elements<SectionProperties>();
            foreach (var sectPr in sectPrs)
            {
                // Delete existing references to headers.
                sectPr.RemoveAllChildren<HeaderReference>();
    
                // Create the new header reference node.
                sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId });
            }
    
            wdDoc.Save();
        }
    }

     

    Sachin R. Chavan.

    Friday, March 20, 2020 11:37 AM
  • Also, noticed that the "header2.xml.rels" is not added, also the "header reference" element is missing type.

    <w:headerReference r:id="Rc3c6890cc60b4829"/>

    Note type is missing.

    Looks some error with code sample/SDK.

    Kindly, help.

    Thanks,

    Sachin R. Chavan.


    Sachin R. Chavan.

    Friday, March 20, 2020 12:47 PM