Java 设置Word文本框中的文字旋转方向

2025-11-20 21:22:46

1、导入操作文档所需的jar包工具,如图:

Java 设置Word文本框中的文字旋转方向

2、在Java程序中嵌入如下代码内容:

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) {
       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();
   }
}

3、执行程序后,生成Word文档,打开该文档后可查看文本框中的文字旋转效果。通过设置不同旋转效果,可查看文本框中的文字效果,如图:

Java 设置Word文本框中的文字旋转方向

Java 设置Word文本框中的文字旋转方向

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢