本文介绍如何使用Spire.Doc在Word文档中添加文本框。
创建只含文字的文本框
C#
//实例化Document对象
Document doc = new Document();
//添加section和段落
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
//在段落上添加文本框
TextBox tb = paragraph.AppendTextBox(120, 50);
//设置文本框相对页边距的位置
tb.Format.HorizontalOrigin = HorizontalOrigin.Margin;
tb.Format.HorizontalPosition = 0;
tb.Format.VerticalOrigin = VerticalOrigin.Margin;
tb.Format.VerticalPosition = 50;
//设置文本框填充色、边框颜色及样式
tb.Format.LineColor = Color.DarkBlue;
tb.Format.LineStyle = TextBoxLineStyle.Simple;
tb.Format.FillColor = Color.LightGreen;
//在文本框中添加段落及文字
Paragraph para = tb.Body.AddParagraph();
TextRange tr = para.AppendText("Spire.Doc是一款用于处理Word文档的.NET组件");
//设置文字格式
tr.CharacterFormat.FontName = "黑体";
tr.CharacterFormat.FontSize = 10;
tr.CharacterFormat.TextColor = Color.Black;
//设置段落对齐方式
para.Format.HorizontalAlignment = HorizontalAlignment.Left;
//保存文档
doc.SaveToFile("添加文本框.docx", FileFormat.Docx);
VB.NET
'实例化Document对象 Dim doc As Document = New Document '添加section和段落
Dim section As Section = doc.AddSection
Dim paragraph As Paragraph = section.AddParagraph
'在段落上添加文本框 Dim tb As TextBox = paragraph.AppendTextBox(120,50) '设置文本框相对页边距的位置
tb.Format.HorizontalOrigin = HorizontalOrigin.Margin
tb.Format.HorizontalPosition = 0
tb.Format.VerticalOrigin = VerticalOrigin.Margin
tb.Format.VerticalPosition = 50
'设置文本框填充色、边框颜色及样式 tb.Format.LineColor = Color.DarkBlue tb.Format.LineStyle = TextBoxLineStyle.Simple tb.Format.FillColor = Color.LightGreen '在文本框中添加段落及文字
Dim para As Paragraph = tb.Body.AddParagraph
Dim tr As TextRange = para.AppendText("Spire.Doc是一款用于处理Word文档的.NET组件")
'设置文字格式 tr.CharacterFormat.FontName = "黑体" tr.CharacterFormat.FontSize = 10 tr.CharacterFormat.TextColor = Color.Black '设置段落对齐方式
para.Format.HorizontalAlignment = HorizontalAlignment.Left
'保存文档 doc.SaveToFile("添加文本框.docx",FileFormat.Docx)
在文本框中同时添加图片和文字
C#
//实例化Document对象 Document doc = new Document();//添加section和段落 Section section = doc.AddSection();Paragraph paragraph = section.AddParagraph();//在段落上添加文本框 TextBox tb = paragraph.AppendTextBox(140,250);//设置文本框相对页边距的位置 tb.Format.HorizontalOrigin = HorizontalOrigin.Margin;tb.Format.HorizontalPosition = 0;tb.Format.VerticalOrigin = VerticalOrigin.Margin;tb.Format.VerticalPosition = 20;//在文本框中添加段落一,并在段落一插入图片 Paragraph para1 = tb.Body.AddParagraph();Image image = Image.FromFile("hualuogeng.png");DocPicture picture = para1.AppendPicture(image);//设置段落格式 para1.Format.HorizontalAlignment = HorizontalAlignment.Center;para1.Format.AfterSpacing = 8;//在文本框中添加段落二,添加文本到段落二 Paragraph para2 = tb.Body.AddParagraph();TextRange textRange = para2.AppendText("华罗庚(1910.11.12—1985.6.12),出生于江苏常州金坛区,祖籍江苏丹阳。数学家,中国科学院院士,美国国家科学院外籍院士,第三世界科学院院士,联邦德国巴伐利亚科学院院士。");textRange.CharacterFormat.FontName = "黑体";textRange.CharacterFormat.FontSize = 9;//设置段落格式 para2.Format.HorizontalAlignment = HorizontalAlignment.Left;para2.Format.LineSpacing = 15;//保存文档 doc.SaveToFile("插入图片及文字.docx",FileFormat.Docx2013);
VB.NET
'实例化Document对象
Dim doc As Document = New Document
'添加section和段落 Dim section As Section = doc.AddSection Dim paragraph As Paragraph = section.AddParagraph '在段落上添加文本框
tb = paragraph.AppendTextBox(140, 250)
'设置文本框相对页边距的位置 tb.Format.HorizontalOrigin = HorizontalOrigin.Margin tb.Format.HorizontalPosition = 0 tb.Format.VerticalOrigin = VerticalOrigin.Margin tb.Format.VerticalPosition = 20 '在文本框中添加段落一,并在段落一插入图片
Dim para1 As Paragraph = tb.Body.AddParagraph
Dim image As Image = Image.FromFile("hualuogeng.png")
Dim picture As DocPicture = para1.AppendPicture(image)
'设置段落格式 para1.Format.HorizontalAlignment = HorizontalAlignment.Center para1.Format.AfterSpacing = 8 '在文本框中添加段落二,添加文本到段落二
Dim para2 As Paragraph = tb.Body.AddParagraph
Dim textRange As TextRange = para2.AppendText("华罗庚(1910.11.12-1985.6.12),出生于江苏常州金坛区,祖籍江苏丹阳。数学家,中国科学院院士,美国国家科学院外籍院士,第三世界科学院院士,联邦德国巴伐利亚科学院院士。")
textRange.CharacterFormat.FontName = "黑体"
textRange.CharacterFormat.FontSize = 9
'设置段落格式 para2.Format.HorizontalAlignment = HorizontalAlignment.Left para2.Format.LineSpacing = 15 '保存文档
doc.SaveToFile("插入图片及文字.docx", FileFormat.Docx2013)