none
Copy Word Header with Multiple Paragraphs RRS feed

  • Question

  • I have a Word header with multiple paragraphs in it.

    Please see sample file here:

    https://skydrive.live.com/redir?resid=CE19B387D4472209!402&authkey=!AEjhjZ-QrVCPiKo

    I use code below to copy header just fine with a single paragraph and multiple images, but not multiple paragraphs. Any help or tips appreciated.

            /// <summary>
            /// Merges Header and Body Template given two files MemoryStream
            /// </summary>
            /// <param name="memStreamHeaderTemplate">Header Template (.dotx) MemoryStream</param>
            /// <param name="memStreamBodyTemplate">Body Template (.dotx) MemoryStream</param>
            /// <returns></returns>
            public static MemoryStream MergeHeadersFooters(MemoryStream memStreamHeaderTemplate, MemoryStream memStreamBodyTemplate)
            {
    
                // Replace header in target document with header of source document.
                using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(memStreamBodyTemplate, true))
                {
                    // Add an ExternalRelationShip of type AttachedTemplate.
                    // Specify the path of template and the relationship ID
                    wdDoc.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
                    MainDocumentPart mainPart = wdDoc.MainDocumentPart;
                    mainPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",
                                                     new Uri("SOMEFILE", UriKind.RelativeOrAbsolute));
                    mainPart.Document.Save();
    
                    // Delete the existing header part.
                    mainPart.DeleteParts(mainPart.HeaderParts);
                    mainPart.DeleteParts(mainPart.FooterParts);
    
                    List<string> headerIds = new List<string>();
                    List<string> footerIds = new List<string>();
    
                    // Feed target headerPart with source headerPart.
                    using (WordprocessingDocument wdDocSource = WordprocessingDocument.Open(memStreamHeaderTemplate, true))
                    {
                        foreach (HeaderPart header in wdDocSource.MainDocumentPart.HeaderParts)
                        {
    
                            // Create a new header part.
                            DocumentFormat.OpenXml.Packaging.HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>();
    
                            // Get Id of the headerPart.
                            headerIds.Add(mainPart.GetIdOfPart(headerPart));
                            headerPart.FeedData(header.GetStream());
    
                            if (header.ImageParts.Count() > 0)
                            {
                                foreach (ImagePart imgPart in header.ImageParts)
                                {
                                    string pId = header.GetIdOfPart(imgPart);
                                    ImagePart imgPrtNew = headerPart.AddImagePart(imgPart.ContentType, pId);
                                    imgPrtNew.FeedData(imgPart.GetStream());
                                }
                            }
                        }
    
                        foreach (FooterPart footer in wdDocSource.MainDocumentPart.FooterParts)
                        {
                            if (footer != null)
                            {
                                // Create a new header part.
                                DocumentFormat.OpenXml.Packaging.FooterPart footerPart = mainPart.AddNewPart<FooterPart>();
    
                                // Get Id of the headerPart.
                                footerIds.Add(mainPart.GetIdOfPart(footerPart));
                                footerPart.FeedData(footer.GetStream());
    
                                if (footer.ImageParts.Count() > 0)
                                {
                                    foreach (ImagePart imgPrt in footer.ImageParts)
                                    {
                                        string pId = footer.GetIdOfPart(imgPrt);
                                        ImagePart imgPrtNew = footerPart.AddImagePart(imgPrt.ContentType, pId);
                                        imgPrtNew.FeedData(imgPrt.GetStream());
                                    }
                                }
                            }
                        }
    
    
                        //Copy Margin Properties
                        Body bodyTo = mainPart.Document.GetFirstChild<Body>();
                        SectionProperties sectionPropertiesTo = bodyTo.GetFirstChild<SectionProperties>();
                        PageMargin pageMarginTo = sectionPropertiesTo.GetFirstChild<PageMargin>();
    
                        Body bodySource = wdDocSource.MainDocumentPart.Document.GetFirstChild<Body>();
                        SectionProperties sectionPropertiesSource = bodySource.GetFirstChild<SectionProperties>();
                        PageMargin pageMarginSource = sectionPropertiesSource.GetFirstChild<PageMargin>();
    
                        //Get Margin Attributes from source doc
                        IList<OpenXmlAttribute> attributesSource = pageMarginSource.GetAttributes();
                        pageMarginTo.SetAttributes(attributesSource);
                    }
    
                    // 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<HeaderFooterReferenceType>();
    
                        foreach (string id in headerIds)
                        {
                            // Create the new header reference node.
                            sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = id });
                        }
    
                        foreach (var id in footerIds)
                        {
                            sectPr.PrependChild<FooterReference>(new FooterReference() { Type = HeaderFooterValues.Default, Id = id });
                        }
                    }
    
                    return memStreamBodyTemplate;
                }
            }


    • Edited by PetkaRUS Wednesday, March 20, 2013 11:05 PM text change
    Wednesday, March 20, 2013 11:04 PM

All replies