在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—