Exemplo n.º 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));
        }
Exemplo n.º 2
0
        public static void AddTableFrame()
        {
            WordDocument        document = new WordDocument();
            WordDocumentBuilder builder  = new WordDocumentBuilder(document);

            builder.TableState.Indent         = 100;
            builder.TableState.PreferredWidth = new TableWidthUnit(300);
            builder.TableState.LayoutType     = TableLayoutType.FixedWidth;

            Paragraph tableTitle = builder.InsertParagraph();

            tableTitle.TextAlignment = Alignment.Center;
            builder.InsertLine("Table Frame");

            Table table = builder.InsertTable();

            table = CreateTableFrame(table);

            WordFile wordFile = new WordFile();

            using (var stream = File.OpenWrite("AddTableFrame.docx"))
            {
                wordFile.Export(document, stream);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
0
        public static void AddSimpleTable()
        {
            WordDocument        document = new WordDocument();
            WordDocumentBuilder builder  = new WordDocumentBuilder(document);

            builder.TableState.Indent = 100;

            Paragraph tableTitle = builder.InsertParagraph();

            tableTitle.TextAlignment = Alignment.Center;
            builder.InsertLine("Simple Table Title");

            Table table = builder.InsertTable();

            table = CreateSimpleTable(table);

            WordFile wordFile = new WordFile();

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