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

在Word中可插入文本框,默认情况下插入的文本框中的文字方向为横向排列,对于一些特殊文档的设计要求,需要改变文字方向,如本次测试中的文档排版为考生试卷类型,考生信息栏的内容为下图中的这种,

本文将以C#程序代码为例,展示如何来实现这种排版。另附VB.NET代码供参考。

 

测试程序环境如下:

  • Visual Studio 2017
  • .net framework 4.8
  • Free spire.doc.dll 7.11
  • 测试word文档:.docx2013

关于dll安装:在程序中通过nuget搜索Free Spire.Doc安装即可。

 

设置文字方向时,可支持多种方式,如图:

本次需要实现的目标格式使用LeftToRight类型即可,如需设置竖排显示则选择LeftToRightRotated类型,其他文字旋转类型同理。

C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace SetTextDirection
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化document对象
            Document doc = new Document();

            //添加一个section
            Section section = doc.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;//旋转文字方向
            //textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRightRotated;//竖排显示       

            //添加文字并设置字体
            Paragraph textboxPara1 = textBox1.Body.AddParagraph();
            TextRange txtrg = textboxPara1.AppendText("姓名______________学号______________班级______________");
            txtrg.CharacterFormat.FontName = "等线";
            txtrg.CharacterFormat.FontSize = 10;
            txtrg.CharacterFormat.TextColor = Color.Black;
            textboxPara1.Format.HorizontalAlignment = HorizontalAlignment.Center;

            //保存文档
            doc.SaveToFile("Result.docx",FileFormat.Docx2013);
            System.Diagnostics.Process.Start("Result.docx");
        }
    }
}

VB.NET

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing

Namespace SetTextDirection
         Class Program
                   Private Shared Sub Main(args As String())
                            '实例化document对象
                            Dim doc As New Document()

                            '添加一个section
                            Dim section As Section = doc.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 = -10F
                            textBox1.Format.VerticalOrigin = VerticalOrigin.Page


                            '设置文字旋转方向
                            textBox1.Format.TextAnchor = ShapeVerticalAlignment.Center
                            textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRight'旋转文字方向
                            'textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRightRotated; '竖排显示


                            '添加文字并设置字体
                            Dim textboxPara1 As Paragraph = textBox1.Body.AddParagraph()
                            Dim txtrg As TextRange = textboxPara1.AppendText("姓名______________学号______________班级______________")
                            txtrg.CharacterFormat.FontName = "等线"
                            txtrg.CharacterFormat.FontSize = 10
                            txtrg.CharacterFormat.TextColor = Color.Black
                            textboxPara1.Format.HorizontalAlignment = HorizontalAlignment.Center


                            '保存文档
                            doc.SaveToFile("Result.docx", FileFormat.Docx2013)
                            System.Diagnostics.Process.Start("Result.docx")
                   End Sub
         End Class
End Namespace

 

—End—

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值