该文将介绍如何使用spire.doc for .net设置文本框内的文字旋转方向并固定文本框位置。
c#
using spire.doc;
using spire.doc.documents;
using spire.doc.fields;
using system;
namespace wordtextbox
{
class program
{
static void main(string[] args)
{
//实例化document对象
document document = new document();
//添加一个section
section section = document.addsection();
//设置页面边距
section.pagesetup.margins.left = 90;
section.pagesetup.margins.right = 90;
paragraph paragraph = section.addparagraph();
//添加第一个文本框
textbox textbox1 = paragraph.appendtextbox(section.pagesetup.margins.left - 20, section.pagesetup.pagesize.height 20);
//设置文本框为固定定位
textbox1.format.horizontalorigin = horizontalorigin.page;
textbox1.format.horizontalposition = 0;
textbox1.format.verticalposition = -10f;
textbox1.format.verticalorigin = verticalorigin.page;
//设置文字旋转方向
textbox1.format.textanchor = shapeverticalalignment.center;
textbox1.format.layoutflowalt = textdirection.lefttoright;
//添加文字并设置字体
paragraph textboxpara1 = textbox1.body.addparagraph();
textrange txtrg = textboxpara1.appendtext("姓名_______学号_________班级__________");
txtrg.characterformat.fontname = "等线";
txtrg.characterformat.fontsize = 10;
txtrg.characterformat.textcolor = system.drawing.color.black;
textboxpara1.format.horizontalalignment = horizontalalignment.center;
//保存文档
document.savetofile("result.docx");
}
}
}
vb.net
namespace wordtextbox
class program
private shared sub main(byval args() as string)
dim document as document = new document
dim section as section = document.addsection
section.pagesetup.margins.left = 90
section.pagesetup.margins.right = 90
dim paragraph as paragraph = section.addparagraph
dim textbox1 as textbox = paragraph.appendtextbox((section.pagesetup.margins.left - 20), (section.pagesetup.pagesize.height 20))
textbox1.format.horizontalorigin = horizontalorigin.page
textbox1.format.horizontalposition = 0
textbox1.format.verticalposition = -10!
textbox1.format.verticalorigin = verticalorigin.page
textbox1.format.textanchor = shapeverticalalignment.center
textbox1.format.layoutflowalt = textdirection.lefttoright
dim textboxpara1 as paragraph = textbox1.body.addparagraph
dim txtrg as textrange = textboxpara1.appendtext("姓名_______学号_________班级__________")
txtrg.characterformat.fontname= "等线"
txtrg.characterformat.fontsize = 10
txtrg.characterformat.textcolor = system.drawing.color.black
textboxpara1.format.horizontalalignment = horizontalalignment.center
document.savetofile("result.docx")
end sub
end class
end namespace
效果图: