private void button1_Click(object sender, EventArgs e) { //Open a Word document Document document = new Document(@"..\..\..\..\..\..\Data\SampleB_2.docx"); string imgPath = @"..\..\..\..\..\..\Data\Spire.Doc.png"; //Add a picture in footer and set it's position DocPicture picture = document.Sections[0].HeadersFooters.Footer.AddParagraph().AppendPicture(Image.FromFile(imgPath)); picture.VerticalOrigin = VerticalOrigin.Page; picture.HorizontalOrigin = HorizontalOrigin.Page; picture.VerticalAlignment = ShapeVerticalAlignment.Bottom; picture.TextWrappingStyle = TextWrappingStyle.None; //Add a textbox in footer and set it's positiion Spire.Doc.Fields.TextBox textbox = document.Sections[0].HeadersFooters.Footer.AddParagraph().AppendTextBox(150, 20); textbox.VerticalOrigin = VerticalOrigin.Page; textbox.HorizontalOrigin = HorizontalOrigin.Page; textbox.HorizontalPosition = 300; textbox.VerticalPosition = 700; textbox.Body.AddParagraph().AppendText("Welcome to E-iceblue"); //Save to file document.SaveToFile("result.docx", FileFormat.Docx); //Launch result file WordDocViewer("result.docx"); }
private void button1_Click(object sender, EventArgs e) { //Create a new document Document doc = new Document(); //Add a section Section section = doc.AddSection(); //Add a paragraph to the section Paragraph paragraph = section.AddParagraph(); //Add a textbox to the paragraph Spire.Doc.Fields.TextBox textbox = paragraph.AppendTextBox(300, 100); //Set the position of the textbox textbox.Format.HorizontalOrigin = HorizontalOrigin.Page; textbox.Format.HorizontalPosition = 140; textbox.Format.VerticalOrigin = VerticalOrigin.Page; textbox.Format.VerticalPosition = 50; //Add text to the textbox Paragraph textboxParagraph = textbox.Body.AddParagraph(); TextRange textboxRange = textboxParagraph.AppendText("Table 1"); textboxRange.CharacterFormat.FontName = "Arial"; //Insert table to the textbox Table table = textbox.Body.AddTable(true); //Specify the number of rows and columns of the table table.ResetCells(4, 4); string[,] data = new string[, ] { { "Name", "Age", "Gender", "ID" }, { "John", "28", "Male", "0023" }, { "Steve", "30", "Male", "0024" }, { "Lucy", "26", "female", "0025" } }; //Add data to the table for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { TextRange tableRange = table[i, j].AddParagraph().AppendText(data[i, j]); tableRange.CharacterFormat.FontName = "Arial"; } } //Apply style to the table table.ApplyStyle(DefaultTableStyle.TableColorful2); //Save and launch document string output = "InsertTableIntoTextBox.docx"; doc.SaveToFile(output, FileFormat.Docx); Viewer(output); }
private void InsertTextbox(Section section) { Paragraph paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph(); paragraph = section.AddParagraph(); paragraph = section.AddParagraph(); //Insert and format the first textbox. Spire.Doc.Fields.TextBox textBox1 = paragraph.AppendTextBox(240, 35); textBox1.Format.HorizontalAlignment = ShapeHorizontalAlignment.Left; textBox1.Format.LineColor = System.Drawing.Color.Gray; textBox1.Format.LineStyle = TextBoxLineStyle.Simple; textBox1.Format.FillColor = System.Drawing.Color.DarkSeaGreen; Paragraph para = textBox1.Body.AddParagraph(); TextRange txtrg = para.AppendText("Textbox 1 in the document"); txtrg.CharacterFormat.FontName = "Lucida Sans Unicode"; txtrg.CharacterFormat.FontSize = 14; txtrg.CharacterFormat.TextColor = System.Drawing.Color.White; para.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; //Insert and format the second textbox. paragraph = section.AddParagraph(); paragraph = section.AddParagraph(); paragraph = section.AddParagraph(); paragraph = section.AddParagraph(); Spire.Doc.Fields.TextBox textBox2 = paragraph.AppendTextBox(240, 35); textBox2.Format.HorizontalAlignment = ShapeHorizontalAlignment.Left; textBox2.Format.LineColor = System.Drawing.Color.Tomato; textBox2.Format.LineStyle = TextBoxLineStyle.ThinThick; textBox2.Format.FillColor = System.Drawing.Color.Blue; textBox2.Format.LineDashing = LineDashing.Dot; para = textBox2.Body.AddParagraph(); txtrg = para.AppendText("Textbox 2 in the document"); txtrg.CharacterFormat.FontName = "Lucida Sans Unicode"; txtrg.CharacterFormat.FontSize = 14; txtrg.CharacterFormat.TextColor = System.Drawing.Color.Pink; para.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; //Insert and format the third textbox. paragraph = section.AddParagraph(); paragraph = section.AddParagraph(); paragraph = section.AddParagraph(); paragraph = section.AddParagraph(); Spire.Doc.Fields.TextBox textBox3 = paragraph.AppendTextBox(240, 35); textBox3.Format.HorizontalAlignment = ShapeHorizontalAlignment.Left; textBox3.Format.LineColor = System.Drawing.Color.Violet; textBox3.Format.LineStyle = TextBoxLineStyle.Triple; textBox3.Format.FillColor = System.Drawing.Color.Pink; textBox3.Format.LineDashing = LineDashing.DashDotDot; para = textBox3.Body.AddParagraph(); txtrg = para.AppendText("Textbox 3 in the document"); txtrg.CharacterFormat.FontName = "Lucida Sans Unicode"; txtrg.CharacterFormat.FontSize = 14; txtrg.CharacterFormat.TextColor = System.Drawing.Color.Tomato; para.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; }
private void button3_Click(object sender, EventArgs e) { #region 加载一个只含有文本的Word文档 Document document = new Document(); document.LoadFromFile(Application.StartupPath + "\\TT.docx"); #endregion #region 创建文本框 Spire.Doc.Fields.TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(150, 300); TB.Format.HorizontalOrigin = HorizontalOrigin.Page; TB.Format.HorizontalPosition = 370; TB.Format.VerticalOrigin = VerticalOrigin.Page; TB.Format.VerticalPosition = 155; TB.Format.TextWrappingStyle = TextWrappingStyle.Tight; TB.Format.TextWrappingType = TextWrappingType.Both; #endregion #region 设置文本框框的颜色,内部边距,图片填充。 TB.Format.LineStyle = TextBoxLineStyle.Simple; TB.Format.LineColor = Color.Transparent; TB.Format.LineDashing = LineDashing.Solid; TB.Format.LineWidth = 3; TB.Format.FillEfects.Type = BackgroundType.Picture; //TB.Format.FillEfects.Picture = Image.FromFile(Application.StartupPath + "\\2.jpg"); #endregion #region 在文本框内添加段落文本,图片,设置字体,字体颜色,行间距,段后距,对齐方式等。然后保存文档,打开查看效果。 Paragraph para1 = TB.Body.AddParagraph(); para1.Format.AfterSpacing = 6; para1.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; TextRange TR1 = para1.AppendText("标题"); TR1.CharacterFormat.FontName = "华文新魏"; TR1.CharacterFormat.FontSize = 16; TR1.CharacterFormat.Bold = true; Paragraph para2 = TB.Body.AddParagraph(); Image image = Image.FromFile(Application.StartupPath + "\\李白.jpg"); DocPicture picture = para2.AppendPicture(image); picture.Width = 120; picture.Height = 160; para2.Format.AfterSpacing = 8; para2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; Paragraph para3 = TB.Body.AddParagraph(); TextRange TR2 = para3.AppendText("描述--盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”"); TR2.CharacterFormat.FontName = "华文新魏"; TR2.CharacterFormat.FontSize = 11; para3.Format.LineSpacing = 15; para3.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left; para3.Format.SuppressAutoHyphens = true; document.SaveToFile(Application.StartupPath + "\\Testt.docx"); System.Diagnostics.Process.Start(Application.StartupPath + "\\Testt.docx"); #endregion }
private void button1_Click(object sender, EventArgs e) { //Create a Word document. Document document = new Document(); //Load the file from disk. document.LoadFromFile(@"..\..\..\..\..\..\Data\ExtractTextFromTextBoxes.docx"); String result = "Result-ExtractTextFromTextBoxes.txt"; //Verify whether the document contains a textbox or not. if (document.TextBoxes.Count > 0) { using (StreamWriter sw = File.CreateText(result)) { //Traverse the document. foreach (Section section in document.Sections) { foreach (Paragraph p in section.Paragraphs) { foreach (DocumentObject obj in p.ChildObjects) { if (obj.DocumentObjectType == DocumentObjectType.TextBox) { Spire.Doc.Fields.TextBox textbox = obj as Spire.Doc.Fields.TextBox; foreach (DocumentObject objt in textbox.ChildObjects) { //Extract text from paragraph in TextBox. if (objt.DocumentObjectType == DocumentObjectType.Paragraph) { sw.Write((objt as Paragraph).Text); } //Extract text from Table in TextBox. if (objt.DocumentObjectType == DocumentObjectType.Table) { Table table = objt as Table; ExtractTextFromTables(table, sw); } } } } } } } } //Launch the result file. WordDocViewer(result); }
private void button1_Click(object sender, EventArgs e) { //Create a new document Document doc = new Document(); Section sec = doc.AddSection(); //Add a text box and append sample text Spire.Doc.Fields.TextBox TB = doc.Sections[0].AddParagraph().AppendTextBox(310, 90); Paragraph para = TB.Body.AddParagraph(); TextRange TR = para.AppendText("Using Spire.Doc, developers will find " + "a simple and effective method to endow their applications with rich MS Word features. "); TR.CharacterFormat.FontName = "Cambria "; TR.CharacterFormat.FontSize = 13; //Set exact position for the text box TB.Format.HorizontalOrigin = HorizontalOrigin.Page; TB.Format.HorizontalPosition = 120; TB.Format.VerticalOrigin = VerticalOrigin.Page; TB.Format.VerticalPosition = 100; //Set line style for the text box TB.Format.LineStyle = TextBoxLineStyle.Double; TB.Format.LineColor = Color.CornflowerBlue; TB.Format.LineDashing = LineDashing.Solid; TB.Format.LineWidth = 5; //Set internal margin for the text box TB.Format.InternalMargin.Top = 15; TB.Format.InternalMargin.Bottom = 10; TB.Format.InternalMargin.Left = 12; TB.Format.InternalMargin.Right = 10; //Save and launch document string output = "TextBoxFormat.docx"; doc.SaveToFile(output, FileFormat.Docx); Viewer(output); }
private void button1_Click(object sender, EventArgs e) { //create a document Document doc = new Document(); Section sec = doc.AddSection(); //add a new paragraph Paragraph para = sec.AddParagraph(); //add a shape group with the height and width ShapeGroup shapegroup = para.AppendShapeGroup(375, 462); shapegroup.HorizontalPosition = 180; //calcuate the scale ratio float X = (float)(shapegroup.Width / 1000.0f); float Y = (float)(shapegroup.Height / 1000.0f); Spire.Doc.Fields.TextBox txtBox = new Spire.Doc.Fields.TextBox(doc); txtBox.SetShapeType(ShapeType.RoundRectangle); txtBox.Width = 125 / X; txtBox.Height = 54 / Y; Paragraph paragraph = txtBox.Body.AddParagraph(); paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; paragraph.AppendText("Start"); txtBox.HorizontalPosition = 19 / X; txtBox.VerticalPosition = 27 / Y; txtBox.Format.LineColor = Color.Green; shapegroup.ChildObjects.Add(txtBox); ShapeObject arrowLineShape = new ShapeObject(doc, ShapeType.DownArrow); arrowLineShape.Width = 16 / X; arrowLineShape.Height = 40 / Y; arrowLineShape.HorizontalPosition = 69 / X; arrowLineShape.VerticalPosition = 87 / Y; arrowLineShape.StrokeColor = Color.Purple; shapegroup.ChildObjects.Add(arrowLineShape); txtBox = new Spire.Doc.Fields.TextBox(doc); txtBox.SetShapeType(ShapeType.Rectangle); txtBox.Width = 125 / X; txtBox.Height = 54 / Y; paragraph = txtBox.Body.AddParagraph(); paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; paragraph.AppendText("Step 1"); txtBox.HorizontalPosition = 19 / X; txtBox.VerticalPosition = 131 / Y; txtBox.Format.LineColor = Color.Blue; shapegroup.ChildObjects.Add(txtBox); arrowLineShape = new ShapeObject(doc, ShapeType.DownArrow); arrowLineShape.Width = 16 / X; arrowLineShape.Height = 40 / Y; arrowLineShape.HorizontalPosition = 69 / X; arrowLineShape.VerticalPosition = 192 / Y; arrowLineShape.StrokeColor = Color.Purple; shapegroup.ChildObjects.Add(arrowLineShape); txtBox = new Spire.Doc.Fields.TextBox(doc); txtBox.SetShapeType(ShapeType.Parallelogram); txtBox.Width = 149 / X; txtBox.Height = 59 / Y; paragraph = txtBox.Body.AddParagraph(); paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; paragraph.AppendText("Step 2"); txtBox.HorizontalPosition = 7 / X; txtBox.VerticalPosition = 236 / Y; txtBox.Format.LineColor = Color.BlueViolet; shapegroup.ChildObjects.Add(txtBox); arrowLineShape = new ShapeObject(doc, ShapeType.DownArrow); arrowLineShape.Width = 16 / X; arrowLineShape.Height = 40 / Y; arrowLineShape.HorizontalPosition = 66 / X; arrowLineShape.VerticalPosition = 300 / Y; arrowLineShape.StrokeColor = Color.Purple; shapegroup.ChildObjects.Add(arrowLineShape); txtBox = new Spire.Doc.Fields.TextBox(doc); txtBox.SetShapeType(ShapeType.Rectangle); txtBox.Width = 125 / X; txtBox.Height = 54 / Y; paragraph = txtBox.Body.AddParagraph(); paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; paragraph.AppendText("Step 3"); txtBox.HorizontalPosition = 19 / X; txtBox.VerticalPosition = 345 / Y; txtBox.Format.LineColor = Color.Blue; shapegroup.ChildObjects.Add(txtBox); //save the document doc.SaveToFile("ShapeGroup.docx", FileFormat.Docx2010); FileViewer("ShapeGroup.docx"); }
private void button2_Click(object sender, EventArgs e) { try { DirectoryInfo di = new DirectoryInfo(textBox1.Text); Document document = new Document(); document.LoadFromFile(Application.StartupPath + "\\TT.docx"); FileInfo[] _allFiles = di.GetFiles("*.*", SearchOption.TopDirectoryOnly); #region 配置信息 int HorizontalPosition = 70; int VerticalPosition = 70; int count = 0; #endregion foreach (FileInfo fi in _allFiles) { if (count >= 3) { break; } count++; #region 创建文本框 Spire.Doc.Fields.TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(220, 300); TB.Format.HorizontalOrigin = HorizontalOrigin.Page; TB.Format.HorizontalPosition = HorizontalPosition; TB.Format.VerticalOrigin = VerticalOrigin.Page; TB.Format.VerticalPosition = VerticalPosition; TB.Format.TextWrappingStyle = TextWrappingStyle.Tight; TB.Format.TextWrappingType = TextWrappingType.Both; #endregion #region 设置文本框框的颜色,内部边距,图片填充。 TB.Format.LineStyle = TextBoxLineStyle.Simple; TB.Format.LineColor = Color.Transparent; TB.Format.LineDashing = LineDashing.Solid; TB.Format.LineWidth = 3; TB.Format.FillEfects.Type = BackgroundType.Picture; //TB.Format.FillEfects.Picture = Image.FromFile(Application.StartupPath + "\\2.jpg"); #endregion #region 在文本框内添加段落文本,图片,设置字体,字体颜色,行间距,段后距,对齐方式等。然后保存文档,打开查看效果。 Paragraph para1 = TB.Body.AddParagraph(); para1.Format.AfterSpacing = 6; para1.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; TextRange TR1 = para1.AppendText("图" + count); TR1.CharacterFormat.FontName = "华文新魏"; TR1.CharacterFormat.FontSize = 16; TR1.CharacterFormat.Bold = true; Paragraph para2 = TB.Body.AddParagraph(); Image image = Image.FromFile(fi.FullName); DocPicture picture = para2.AppendPicture(image); picture.Width = 200; picture.Height = 250; para2.Format.AfterSpacing = 8; para2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; //Paragraph para3 = TB.Body.AddParagraph(); //TextRange TR2 = para3.AppendText("描述--盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”"); //TR2.CharacterFormat.FontName = "华文新魏"; //TR2.CharacterFormat.FontSize = 11; //para3.Format.LineSpacing = 15; //para3.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left; //para3.Format.SuppressAutoHyphens = true; #endregion #region 更改配置变量 if (count == 1) { HorizontalPosition = 300; VerticalPosition = 70; } if (count == 2) { HorizontalPosition = 70; VerticalPosition = 400; } if (count == 3) { HorizontalPosition = 300; VerticalPosition = 400; Spire.Doc.Fields.TextBox _TB = document.Sections[0].Paragraphs[0].AppendTextBox(220, 300); _TB.Format.HorizontalOrigin = HorizontalOrigin.Page; _TB.Format.HorizontalPosition = HorizontalPosition; _TB.Format.VerticalOrigin = VerticalOrigin.Page; _TB.Format.VerticalPosition = VerticalPosition; _TB.Format.TextWrappingStyle = TextWrappingStyle.Tight; _TB.Format.TextWrappingType = TextWrappingType.Both; _TB.Format.LineStyle = TextBoxLineStyle.Simple; _TB.Format.LineColor = Color.Transparent; _TB.Format.LineDashing = LineDashing.Solid; _TB.Format.LineWidth = 3; _TB.Format.FillEfects.Type = BackgroundType.Color; _TB.Format.FillEfects.Color = Color.Green; Paragraph para3 = _TB.Body.AddParagraph(); TextRange TR2 = para3.AppendText(" 描述--盛唐最杰出的诗人,中国历史最伟大的浪漫主义诗人杜甫赞其文章“笔落惊风雨,诗成泣鬼神”"); TR2.CharacterFormat.FontName = "幼圆"; TR2.CharacterFormat.FontSize = 15; para3.Format.LineSpacing = 15; para3.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left; para3.Format.SuppressAutoHyphens = true; } #endregion } //Save and Launch document.SaveToFile(textBox1.Text + textBox2.Text, FileFormat.Docx); //System.Diagnostics.Process.Start(textBox2.Text); } catch (Exception ex) { MessageBox.Show("Error:" + ex.Message, "Tips", MessageBoxButtons.OK); } }
private void button1_Click(object sender, EventArgs e) { String filename = Application.StartupPath + "\\bin\\" + Global.templateName + ".doc"; Spire.Doc.Document document = new Spire.Doc.Document(filename, FileFormat.Docx); Spire.Doc.Fields.TextBox textBox = document.TextBoxes[10]; Spire.Doc.Documents.Paragraph paragraph = textBox.Body.AddParagraph(); TextRange textRange = paragraph.AppendText(textBox1.Text); document.SaveToFile(filename, FileFormat.Docx); object filename1 = Environment.CurrentDirectory.ToString() + "\\bin\\" + Global.templateName + ".doc"; string ImagePath = pictureBox1.ImageLocation; string strKey = "7.4.2 开门测试期间温度变化曲线图"; object MissingValue = Type.Missing; bool isFindSealLoc = false; Microsoft.Office.Interop.Word.Application wp = null; Microsoft.Office.Interop.Word.Document wd = null; try { wp = new Microsoft.Office.Interop.Word.Application(); wd = wp.Documents.Open(ref filename1, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue); wp.Selection.Find.ClearFormatting(); wp.Selection.Find.Replacement.ClearFormatting(); wp.Selection.Find.Text = strKey; object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone; if (wp.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref objReplace, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue)) { object Anchor = wp.Selection.Range; object LinkToFile = false; object SaveWithDocument = true; Microsoft.Office.Interop.Word.InlineShape Inlineshape = wp.Selection.InlineShapes.AddPicture( ImagePath, ref LinkToFile, ref SaveWithDocument, ref Anchor); Inlineshape.Select(); Microsoft.Office.Interop.Word.Shape shape = Inlineshape.ConvertToShape(); shape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind; isFindSealLoc = true; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { if (wd != null) { wd.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(wd); wd = null; } if (wp != null) { wp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(wp); wp = null; } MessageBox.Show("导入成功!"); } this.Close(); }