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

            //Add in-line image using builder
            builder.InsertText("Simple sentence 1 in line. ");
            using (Stream stream = File.OpenRead("sample.jpg"))
            {
                builder.InsertImageInline(stream, "jpg");
            }
            builder.InsertText("Simple sentence 2 in line");

            //Add in-line image using Paragraph object
            Paragraph paragraph = builder.InsertParagraph();
            //Add text before
            TextInline textStart = paragraph.Inlines.AddText();

            textStart.Text = "Text add using paragraph start.";
            //Insert image in the middle of text content
            ImageInline imageInline = paragraph.Inlines.AddImageInline();

            using (Stream stream = File.OpenRead("sample.png"))
            {
                imageInline.Image.ImageSource = new Basic.Media.ImageSource(stream, "png");
            }
            //Add text after
            TextInline textEnd = paragraph.Inlines.AddText();

            textEnd.Text = "Text add using paragraph end.";

            WordFile wordFile = new WordFile();

            File.WriteAllBytes("AddImageInline.docx", wordFile.Export(document));
        }
Example #2
0
        public static void AddBookmark()
        {
            WordDocument        document = new WordDocument();
            WordDocumentBuilder builder  = new WordDocumentBuilder(document);

            builder.InsertLine("First paragraph in this document.");

            //Insert some content before bookmark
            builder.InsertText("Second paragraph start. ");
            //Select the content you want to bookmark
            TextInline textBookmark = builder.InsertText("This is bookmark. ");

            //Insert some content after bookmark
            builder.InsertText("Second paragraph end.");

            //Add bookmark with selected content
            builder.InsertBookmark("bookmark1", textBookmark, textBookmark);

            WordFile wordFile = new WordFile();

            using (var stream = File.OpenWrite("AddBookmark.docx"))
            {
                wordFile.Export(document, stream);
            }
        }
Example #3
0
        public static void AddComment()
        {
            WordDocument        document = new WordDocument();
            WordDocumentBuilder builder  = new WordDocumentBuilder(document);

            builder.InsertLine("First paragraph in this document.");

            //Insert some content before comment
            builder.InsertText("Second paragraph start. ");
            //Select the content you want to comment
            TextInline textComment = builder.InsertText("Text has comment. ");

            //Insert some content after comment
            builder.InsertText("Second paragraph end.");

            //Add comment with selected content
            Comment comment = builder.InsertComment("Comment details here", textComment, textComment);

            comment.Author = "iDiTect";
            comment.Date   = DateTime.Now;

            WordFile wordFile = new WordFile();

            using (var stream = File.OpenWrite("AddComment.docx"))
            {
                wordFile.Export(document, stream);
            }
        }
Example #4
0
        public static void AddFloatingImage()
        {
            WordDocument        document = new WordDocument();
            WordDocumentBuilder builder  = new WordDocumentBuilder(document);

            builder.CharacterState.FontSize = 24;

            //Add floating image using builder
            using (Stream stream = File.OpenRead("sample.jpg"))
            {
                FloatingImage floatingImage1 = builder.InsertFloatingImage(stream, "jpg");
                floatingImage1.Wrapping.WrappingType = ShapeWrappingType.Square;
            }
            builder.InsertText("This text sentence content will display at the square of the floating image. ");
            builder.InsertText("This text sentence content will display at the square of the floating image.");

            WordFile wordFile = new WordFile();

            File.WriteAllBytes("AddFloatingImage.docx", wordFile.Export(document));
        }
Example #5
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);
            }
        }
Example #6
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);
        }
Example #7
0
        public static void AddText()
        {
            WordDocument        document = new WordDocument();
            WordDocumentBuilder builder  = new WordDocumentBuilder(document);

            //Set global style for text and paragraph
            builder.CharacterState.FontFamily      = new ThemableFontFamily("Arial");
            builder.CharacterState.FontSize        = 16;
            builder.ParagraphState.LineSpacing     = 1.2;
            builder.ParagraphState.FirstLineIndent = 40;

            //Insert text using builder directly
            builder.InsertText("Nomal text. ");
            //Insert one line with text, it will add line break automatically
            builder.InsertLine("Nomal line with auto line break. ");
            //So the text below will be added in a second paragraph
            builder.InsertText("Nomal text. ");

            //Insert text using TextInline object
            TextInline textInline = new TextInline(document);

            textInline.Text     = "This text content is using TextInline object. ";
            textInline.FontSize = 20;
            builder.InsertInline(textInline);

            //Insert text with customized style
            builder.InsertText("Times New Roman, ").FontFamily  = new ThemableFontFamily("Times New Roman");
            builder.InsertText("bold, ").FontWeight             = FontWeights.Bold;
            builder.InsertText("italic, ").FontStyle            = FontStyles.Italic;
            builder.InsertText("underline, ").Underline.Pattern = UnderlinePattern.Single;
            builder.InsertText("colors ").ForegroundColor       = new ThemableColor(Color.FromRgb(255, 0, 0));

            //Add several paragraphs to page
            for (int i = 0; i < 20; i++)
            {
                builder.InsertParagraph();
                for (int j = 1; j < 11; j++)
                {
                    builder.InsertText("This is sentence " + j.ToString() + ". ");
                }
            }

            WordFile wordFile = new WordFile();

            using (var stream = File.OpenWrite("AddText.docx"))
            {
                wordFile.Export(document, stream);
            }
        }
Example #8
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);
            }
        }