欢迎光临
我的个人博客网站

C# 设置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—

 

赞(0) 打赏
未经允许不得转载:张拓的天空 » C# 设置Word文本框中的文字旋转方向
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏