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

  • A+
所属分类:.NET技术
摘要

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

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

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

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

 

测试程序环境如下:

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

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

 

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

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

本次需要实现的目标格式使用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—