本文介绍使用Spire.Doc for Java来设置Word文本框中的文字旋转方向。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextBox;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class SetTextDirection {
public static void main(String[] args) {
//创建Word文档
Document doc = new Document();
Section section = doc.addSection();
//设置页面边距
section.getPageSetup().getMargins().setLeft(90f);
section.getPageSetup().getMargins().setRight(90f);
Paragraph paragraph = section.addParagraph();
//添加第一个文本框
TextBox textBox1 = paragraph.appendTextBox(280, 250);
//设置文本框为固定定位
textBox1.getFormat().setHorizontalOrigin(HorizontalOrigin.Page);
textBox1.getFormat().setHorizontalPosition(150);
textBox1.getFormat().setVerticalOrigin(VerticalOrigin.Page);
textBox1.getFormat().setVerticalPosition(80);
//设置文字旋转方向
textBox1.getFormat().setTextAnchor(ShapeVerticalAlignment.Center);
textBox1.getFormat().setLayoutFlowAlt(TextDirection.Left_To_Right);//旋转文字方向
//textBox1.getFormat().setLayoutFlowAlt(TextDirection.Left_To_Right_Rotated);//文字竖排显示
//添加文字并设置字体
Paragraph textboxPara1 = textBox1.getBody().addParagraph();
TextRange txtrg = textboxPara1.appendText("姓名_______学号_________班级__________");
txtrg.getCharacterFormat().setFontName("等线");
txtrg.getCharacterFormat().setFontSize(10);
txtrg.getCharacterFormat().setTextColor(Color.black);
textboxPara1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//保存文档
doc.saveToFile("Result.docx");
doc.dispose();
}
}
在生成的Word文档中可查看文本框中的文字旋转效果,如图:
Left_To_Right旋转效果:
Left_To_Right_Rotated竖排显示效果: