在word文档中,文本框可以包含很多种元素,如文本、图片和表格等。前面我们已经讲了。本文将从两个方面详细描述如何插入图片到文本框。
新建word文档,添加文本框,并插入图片和文本到文本框
c#
//创建document实例并添加节和段落
document doc = new document();
section section = doc.addsection();
paragraph paragraph = section.addparagraph();
//添加文本框到段落,并指定文本框的宽度和高度
textbox tb = paragraph.appendtextbox(140, 120);
//设置文本框的位置
tb.format.horizontalorigin = horizontalorigin.page;
tb.format.horizontalposition = 50;
tb.format.verticalorigin = verticalorigin.page;
tb.format.verticalposition = 50;
//插入图片到文本框
paragraph para1 = tb.body.addparagraph();
image image = image.fromfile("logo.png");
docpicture picture = para1.appendpicture(image);
para1.format.afterspacing = 8;
para1.format.horizontalalignment = horizontalalignment.center;
//插入文字到文本框
paragraph para2 = tb.body.addparagraph();
textrange textrange = para2.appendtext("成都冰蓝科技,您的办公文档开发技术专家");
textrange.characterformat.fontname = "cambria";
textrange.characterformat.fontsize = 9;
para2.format.linespacing = 15;
para2.format.horizontalalignment = horizontalalignment.left;
para2.format.suppressautohyphens = true;
//保存文档
doc.savetofile("result.docx", fileformat.docx2013);
vb.net
'创建document实例并添加节和段落
dim doc as document = new document
dim section as section = doc.addsection
dim paragraph as paragraph = section.addparagraph
'添加文本框到段落,并指定文本框的宽度和高度
dim tb as textbox = paragraph.appendtextbox(140, 120)
'设置文本框的位置
tb.format.horizontalorigin = horizontalorigin.page
tb.format.horizontalposition = 50
tb.format.verticalorigin = verticalorigin.page
tb.format.verticalposition = 50
'插入图片到文本框
dim para1 as paragraph = tb.body.addparagraph
dim image as image = image.fromfile("logo.png")
dim picture as docpicture = para1.appendpicture(image)
para1.format.afterspacing = 8
para1.format.horizontalalignment = horizontalalignment.center
'插入文字到文本框
dim para2 as paragraph = tb.body.addparagraph
dim textrange as textrange = para2.appendtext("成都冰蓝科技,您的办公文档开发技术专家")
textrange.characterformat.fontname = "cambria"
textrange.characterformat.fontsize = 9
para2.format.linespacing = 15
para2.format.horizontalalignment = horizontalalignment.left
para2.format.suppressautohyphens = true
'保存文档
doc.savetofile("result.docx", fileformat.docx2013)
加载已有word文档,获取文本框,在文本框已有文字前插入图片
c#
//加载word文档
document doc = new document();
doc.loadfromfile("sample.docx");
//获取第一个文本框
textbox textbox = doc.textboxes[0];
//插入段落到文本框首行
paragraph para1 = new paragraph(doc);
textbox.body.paragraphs.insert(0,para1);
//插入图片到文本框并设置图片大小
image image = image.fromfile("logo2.png");
docpicture picture = para1.appendpicture(image);
picture.width = 80f;
picture.height = 80f;
para1.format.afterspacing = 8;
para1.format.horizontalalignment = horizontalalignment.center;
//保存文档
doc.savetofile("result2.docx", fileformat.docx2013);
vb.net
'加载word文档
dim doc as new document()
doc.loadfromfile("sample.docx")
'获取第一个文本框
dim textbox as textbox = doc.textboxes(0)
'插入段落到文本框首行
dim para1 as new paragraph(doc)
textbox.body.paragraphs.insert(0, para1)
'插入图片到文本框并设置图片大小
dim image as image = image.fromfile("logo2.png")
dim picture as docpicture = para1.appendpicture(image)
picture.width = 80f
picture.height = 80f
para1.format.afterspacing = 8
para1.format.horizontalalignment = horizontalalignment.center
'保存文档
doc.savetofile("result2.docx", fileformat.docx2013)