该文将介绍如何使用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
效果图: