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)); }
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); } }
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); } }
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); } }
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); } }
public static Table CreateSimpleTable(Table table) { ThemableColor bordersColor = new ThemableColor(Color.FromRgb(73, 90, 128)); ThemableColor headerColor = new ThemableColor(Color.FromRgb(34, 143, 189)); ThemableColor defaultRowColor = new ThemableColor(Color.FromRgb(176, 224, 230)); Border border = new Border(1, BorderStyle.Single, bordersColor); table.Borders = new TableBorders(border); table.TableCellPadding = new Basic.Primitives.Padding(5); //Add table header TableRow headerRow = table.Rows.AddTableRow(); headerRow.RepeatOnEveryPage = true; //Add first column TableCell column1 = headerRow.Cells.AddTableCell(); column1.State.BackgroundColor.LocalValue = headerColor; column1.Borders = new TableCellBorders(border, border, border, border, null, null, border, null); column1.PreferredWidth = new TableWidthUnit(50); //Add second column TableCell column2 = headerRow.Cells.AddTableCell(); column2.State.BackgroundColor.LocalValue = headerColor; column2.PreferredWidth = new TableWidthUnit(150); column2.VerticalAlignment = VerticalAlignment.Center; Paragraph column2Para = column2.Blocks.AddParagraph(); column2Para.TextAlignment = Alignment.Center; column2Para.State.LineSpacing = 1; TextInline column2Text = column2Para.Inlines.AddText("Product"); column2Text.State.ForegroundColor = new ThemableColor(Colors.White); column2Text.FontSize = 20; //Add third column TableCell column3 = headerRow.Cells.AddTableCell(); column3.State.BackgroundColor.LocalValue = headerColor; column3.PreferredWidth = new TableWidthUnit(250); column3.Padding = new Basic.Primitives.Padding(20, 0, 0, 0); Paragraph column3Para = column3.Blocks.AddParagraph(); column3Para.State.LineSpacing = 1; TextInline column3Text = column3Para.Inlines.AddText("Price"); column3Text.State.ForegroundColor = new ThemableColor(Colors.White); column3Text.FontSize = 20; //Add table rows Random r = new Random(); for (int i = 0; i < 50; i++) { ThemableColor rowColor = i % 2 == 0 ? defaultRowColor : new ThemableColor(Colors.White); TableRow row = table.Rows.AddTableRow(); row.Height = new TableRowHeight(HeightType.Exact, 20); TableCell idCell = row.Cells.AddTableCell(); idCell.State.BackgroundColor.LocalValue = rowColor; idCell.Blocks.AddParagraph().Inlines.AddText(i.ToString()); TableCell productCell = row.Cells.AddTableCell(); productCell.State.BackgroundColor.LocalValue = rowColor; Paragraph productPara = productCell.Blocks.AddParagraph(); productPara.TextAlignment = Alignment.Center; productPara.Inlines.AddText(String.Format("Product{0}", i)); TableCell priceCell = row.Cells.AddTableCell(); priceCell.Padding = new Basic.Primitives.Padding(20, 0, 0, 0); priceCell.State.BackgroundColor.LocalValue = rowColor; priceCell.Blocks.AddParagraph().Inlines.AddText(r.Next(10, 1000).ToString()); } return(table); }