C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# Word改变文字方向

C#设置Word文本框中改变文字方向的方法

作者:E-iceblue

在Word中可插入文本框,默认情况下插入的文本框中的文字方向为横向排列,对于一些特殊文档的设计要求,需要改变文字方向,本文就详细的介绍一下使用,感兴趣的可以了解一下

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

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

测试程序环境如下:

关于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

到此这篇关于C#设置Word文本框中改变文字方向的方法的文章就介绍到这了,更多相关C# Word改变文字方向内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文