Пример #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);
            }
        }
Пример #2
0
        public static void AddLinkInsideDocument()
        {
            WordDocument        document = new WordDocument();
            WordDocumentBuilder builder  = new WordDocumentBuilder(document);

            //Add hyperlink navigate to bookmark inside this document by bookmark's name
            builder.InsertHyperlinkToBookmark("this is hyerlink", "bookmark1", "go to bookmark1");

            //Add a bookmark in the second page
            builder.InsertBreak(BreakType.PageBreak);
            TextInline textBookmark = builder.InsertText("This is bookmark1. ");

            builder.InsertBookmark("bookmark1", textBookmark, textBookmark);

            WordFile wordFile = new WordFile();

            using (var stream = File.OpenWrite("AddLinkInsideDocument.docx"))
            {
                wordFile.Export(document, stream);
            }
        }
Пример #3
0
        public static WordDocument CreateMailMergeTemplate()
        {
            WordDocument        document = new WordDocument();
            WordDocumentBuilder builder  = new WordDocumentBuilder(document);

            //Insert salutation
            builder.InsertText("Hello ");
            builder.InsertField("MERGEFIELD CustomerFirstName", "");
            builder.InsertText(" ");
            builder.InsertField("MERGEFIELD CustomerLastName", "");
            builder.InsertText(",");

            //Insert a blank line
            builder.InsertParagraph();

            //Insert mail body
            builder.InsertParagraph();
            builder.InsertText("Thanks for purchasing our ");
            builder.InsertField("MERGEFIELD ProductName ", "");
            builder.InsertText(", please download your Invoice at ");
            builder.InsertField("MERGEFIELD InvoiceURL", "");
            builder.InsertText(". If you have any questions please call ");
            builder.InsertField("MERGEFIELD SupportFhone", "");
            builder.InsertText(", or email us at ");
            builder.InsertField("MERGEFIELD SupportEmail", "");
            builder.InsertText(".");

            //Insert a blank line
            builder.InsertParagraph();

            //Insert mail ending
            builder.InsertParagraph();
            builder.InsertText("Best regards,");
            builder.InsertBreak(BreakType.LineBreak);
            builder.InsertField("MERGEFIELD EmployeeFullname", "");
            builder.InsertText(" ");
            builder.InsertField("MERGEFIELD EmployeeDepartment", "");

            return(document);
        }