本文介绍如何使用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)