none
Add HTML content with Image into header and footer in Open XML SDK 2.0 RRS feed

  • Question

  • I facing issue with header and footer in docx using OpenXml SDK. Here I adding html content with image in both and header and footer. Its a dynamic header footer content so can't hard code into coding part.

    Issue is that After adding content with image using lib NotesFor.HtmlToOpenXml.HtmlConverter in header and footer, the docx get generate successfully but header and footer image is broken. After googing found that header1.xml.rels is not generating.

    Please help me how can I generate this file. 

    Sample code

      public static void AddHeaderFooter(String documentPath)
            {
                // Replace header in target document with header of source document.
                using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true))
                {
                    // Get the main document part
                    MainDocumentPart mainDocumentPart = document.MainDocumentPart;

                    // Delete the existing header and footer parts
                    mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts);
                    mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts);

                    // Create a new header and footer part
                    HeaderPart headerPart = mainDocumentPart.AddNewPart<HeaderPart>();
                    FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();

                    // Get Id of the headerPart and footer parts
                    string headerPartId = mainDocumentPart.GetIdOfPart(headerPart);
                    string footerPartId = mainDocumentPart.GetIdOfPart(footerPart);

                    GenerateHeaderPartContent(headerPart, mainDocumentPart);

                    GenerateFooterPartContent(footerPart, mainDocumentPart);

                    // Get SectionProperties and Replace HeaderReference and FooterRefernce with new Id
                    IEnumerable<SectionProperties> sections = mainDocumentPart.Document.Body.Elements<SectionProperties>();

                    foreach (var section in sections)
                    {
                        // Delete existing references to headers and footers
                        section.RemoveAllChildren<HeaderReference>();
                        section.RemoveAllChildren<FooterReference>();

                        // Create the new header and footer reference node
                        section.PrependChild<HeaderReference>(new HeaderReference() { Id = headerPartId });
                        section.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId });
                        
                    }

                    wordprocessingDocument.MainDocumentPart.Document.Save();
                    // Close the document.
                    wordprocessingDocument.Close();

                }
            }


     public static void GenerateHeaderPartContent(HeaderPart part, MainDocumentPart mainDocumentPart)
            {
                Header header1 = new Header();
                var converter = new NotesFor.HtmlToOpenXml.HtmlConverter(mainDocumentPart);
                var paragraph = converter.Parse(@"<p><img src='http://localhost:62626/PPContent/40/Resources/HeaderFooter/HFk2FtngryxI_6f7ELxbjMCN_None_273182_80_DX546_DY546_CX273_CY182.jpg' style='width: 273px; height: 182px;' />header content</p>");
                header1.Append(paragraph);

                part.Header = header1;
            }

            public static void GenerateFooterPartContent(FooterPart part, MainDocumentPart mainDocumentPart)
            {
                Footer footer1 = new Footer();
                var converter = new NotesFor.HtmlToOpenXml.HtmlConverter(mainDocumentPart);
                var paragraph = converter.Parse(@"<p><img src='http://localhost:62626/PPContent/40/Resources/HeaderFooter/HFk2FtngryxI_6f7ELxbjMCN_None_273182_80_DX546_DY546_CX273_CY182.jpg' style='width: 273px; height: 182px;' />footer content</p>");
                footer1.Append(paragraph);
                part.Footer = footer1;
            }

    Tuesday, December 17, 2019 2:24 PM