Beispiel #1
0
        public static void AddHeaderFooterForSections()
        {
            WordDocument        document = new WordDocument();
            WordDocumentBuilder builder  = new WordDocumentBuilder(document);

            //One section can contains a range of pages

            //Insert one section with single page
            Section sectionSinglePage = builder.InsertSection();

            builder.InsertText("First page in section 1");

            //Add header for single page section
            Header headerSinglePage = sectionSinglePage.Headers.Add();

            headerSinglePage.Blocks.AddParagraph().Inlines.AddText("header for single page section");

            //Insert one section with multiple pages
            Section sectionMultipage = builder.InsertSection();

            //Create first page in section
            builder.InsertText("First page in section 2");
            builder.InsertBreak(BreakType.PageBreak);
            //Create second page in section
            builder.InsertText("Second page in section 2");

            //Defaults, all the secions's header and footer will inherit the rules in the first section
            //If you want to use blank header in the second section, you need initialize a new Header object with nothing to do
            Header headerMultipage = sectionMultipage.Headers.Add();
            //Add footer for multiple page section
            Footer footerMultipage = sectionMultipage.Footers.Add();

            footerMultipage.Blocks.AddParagraph().Inlines.AddText("footer for multiple page section");

            WordFile wordFile = new WordFile();

            using (var stream = File.OpenWrite("AddHeaderFooterForSections.docx"))
            {
                wordFile.Export(document, stream);
            }
        }