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


    Word文档中可添加文本框,并设置文本框为横向文本排列或是纵向文本排列,或者设置文本框中的文字旋转方向等。通过Java程序代码,也可以实现以上文本框的操作。下面以Java代码示例展示具体的实现步骤。另外,可参考C#及VB.NET代码的实现方法。

    本次程序测试环境如下:

    Word测试文档版本:.docx 2013

    Word Jar包工具:free spire.doc.jar 3.9.0

    代码编译工具:IDEA

    Jdk版本:1.8.0

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

    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) {
    //创建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竖排显示效果:

    延伸阅读:

    —End—

  • 相关阅读:
    bzoj5157: [Tjoi2014]上升子序列(树状数组LIS)
    2435: [Noi2011]道路修建(树上操作)
    bzoj1019: [SHOI2008]汉诺塔(动态规划)
    bzoj1103: [POI2007]大都市meg(树链剖分)
    bzoj2190: [SDOI2008]仪仗队(欧拉)
    bzoj4519: [Cqoi2016]不同的最小割(分治最小割)
    bzoj2229: [Zjoi2011]最小割(分治最小割+最小割树思想)
    bzoj1816: [Cqoi2010]扑克牌(二分答案判断)
    [HEOI2015]兔子与樱花
    [POI2009]KAM-Pebbles
  • 原文地址:https://www.cnblogs.com/Yesi/p/14943717.html
Copyright © 2020-2023  润新知