C#設(shè)置Word文本框中改變文字方向的方法
在Word中可插入文本框,默認(rèn)情況下插入的文本框中的文字方向?yàn)闄M向排列,對(duì)于一些特殊文檔的設(shè)計(jì)要求,需要改變文字方向,如本次測(cè)試中的文檔排版為考生試卷類型,考生信息欄的內(nèi)容為下圖中的這種,
本文將以C#程序代碼為例,展示如何來(lái)實(shí)現(xiàn)這種排版。另附VB.NET代碼供參考。
測(cè)試程序環(huán)境如下:
- Visual Studio 2017
- .net framework 4.8
- Free spire.doc.dll 7.11
- 測(cè)試word文檔:.docx2013
關(guān)于dll安裝:在程序中通過(guò)nuget搜索Free Spire.Doc安裝即可。
設(shè)置文字方向時(shí),可支持多種方式,如圖:
本次需要實(shí)現(xiàn)的目標(biāo)格式使用LeftToRight類型即可,如需設(shè)置豎排顯示則選擇LeftToRightRotated類型,其他文字旋轉(zhuǎn)類型同理。
C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace SetTextDirection { class Program { static void Main(string[] args) { //實(shí)例化document對(duì)象 Document doc = new Document(); //添加一個(gè)section Section section = doc.AddSection(); //設(shè)置頁(yè)面邊距 section.PageSetup.Margins.Left = 90; section.PageSetup.Margins.Right = 90; Paragraph paragraph = section.AddParagraph(); //添加第一個(gè)文本框 TextBox textBox1 = paragraph.AppendTextBox(section.PageSetup.Margins.Left - 20, section.PageSetup.PageSize.Height + 20); //設(shè)置文本框?yàn)楣潭ǘㄎ? textBox1.Format.HorizontalOrigin = HorizontalOrigin.Page; textBox1.Format.HorizontalPosition = 0; textBox1.Format.VerticalPosition = -10f; textBox1.Format.VerticalOrigin = VerticalOrigin.Page; //設(shè)置文字旋轉(zhuǎn)方向 textBox1.Format.TextAnchor = ShapeVerticalAlignment.Center; textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRight;//旋轉(zhuǎn)文字方向 //textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRightRotated;//豎排顯示 //添加文字并設(shè)置字體 Paragraph textboxPara1 = textBox1.Body.AddParagraph(); TextRange txtrg = textboxPara1.AppendText("姓名______________學(xué)號(hào)______________班級(jí)______________"); 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()) '實(shí)例化document對(duì)象 Dim doc As New Document() '添加一個(gè)section Dim section As Section = doc.AddSection() '設(shè)置頁(yè)面邊距 section.PageSetup.Margins.Left = 90 section.PageSetup.Margins.Right = 90 Dim paragraph As Paragraph = section.AddParagraph() '添加第一個(gè)文本框 Dim textBox1 As TextBox = paragraph.AppendTextBox(section.PageSetup.Margins.Left - 20, section.PageSetup.PageSize.Height + 20) '設(shè)置文本框?yàn)楣潭ǘㄎ? textBox1.Format.HorizontalOrigin = HorizontalOrigin.Page textBox1.Format.HorizontalPosition = 0 textBox1.Format.VerticalPosition = -10F textBox1.Format.VerticalOrigin = VerticalOrigin.Page '設(shè)置文字旋轉(zhuǎn)方向 textBox1.Format.TextAnchor = ShapeVerticalAlignment.Center textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRight'旋轉(zhuǎn)文字方向 'textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRightRotated; '豎排顯示 '添加文字并設(shè)置字體 Dim textboxPara1 As Paragraph = textBox1.Body.AddParagraph() Dim txtrg As TextRange = textboxPara1.AppendText("姓名______________學(xué)號(hào)______________班級(jí)______________") 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
到此這篇關(guān)于C#設(shè)置Word文本框中改變文字方向的方法的文章就介紹到這了,更多相關(guān)C# Word改變文字方向內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中System.IO.Pipelines庫(kù)的使用詳解
System.IO.Pipelines 是一個(gè)庫(kù),旨在使在 .NET 中執(zhí)行高性能 I/O 更加容易,本文主要為大家詳細(xì)介紹了System.IO.Pipelines具體使用方法,感興趣的可以了解下2023-12-12WinForm中DataGridView添加,刪除,修改操作具體方法
這篇文章介紹了WinForm中DataGridView添加,刪除,修改操作具體方法,有需要的朋友可以參考一下2013-10-10WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法,涉及WinForm計(jì)時(shí)器及進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09