翻譯|使用教程|編輯:胡濤|2022-05-09 16:33:36.470|閱讀 212 次
概述:在本文中,您將學習如何使用Spire.Doc for .NET以編程方式為 Word 文檔中的段落設置不同的文本對齊方式。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
文本對齊是一個段落格式屬性,它決定了整個段落中文本的外觀。Microsoft Word 中有四種可用的文本對齊方式:左對齊、居中對齊、右對齊和兩端對齊。在本文中,您將學習如何使用Spire.Doc for .NET以編程方式為 Word 文檔中的段落設置不同的文本對齊方式。
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.Doc
[C#]
using Spire.Doc; using Spire.Doc.Documents; namespace AlignText { class Program { static void Main(string[] args) { //Create a Document instance Document doc = new Document(); //Load a sample Word document doc.LoadFromFile(@"D:\Files\sample.docx"); //Get the first section Section section = doc.Sections[0]; //Get the first paragraph and make it center-aligned Paragraph p = section.Paragraphs[0]; p.Format.HorizontalAlignment = HorizontalAlignment.Center; //Get the second paragraph and make it left-aligned Paragraph p1 = section.Paragraphs[1]; p1.Format.HorizontalAlignment = HorizontalAlignment.Left; //Get the third paragraph and make it right-aligned Paragraph p2 = section.Paragraphs[2]; p2.Format.HorizontalAlignment = HorizontalAlignment.Right; //Get the fourth paragraph and make it justified Paragraph p3 = section.Paragraphs[3]; p3.Format.HorizontalAlignment = HorizontalAlignment.Justify; //Save the document doc.SaveToFile("WordAlignment.docx", FileFormat.Docx); } } }
[VB.NET]
Imports Spire.Doc Imports Spire.Doc.Documents Namespace AlignText Friend Class Program Shared Sub Main(ByVal args() As String) 'Create a Document instance Dim doc As New Document() 'Load a sample Word document doc.LoadFromFile("E:\Work\Documents\WordDocuments\Humor Them.docx") 'Get the first section Dim section As Section = doc.Sections(0) 'Get the first paragraph and make it center-aligned Dim p As Paragraph = section.Paragraphs(0) p.Format.HorizontalAlignment = HorizontalAlignment.Center 'Get the second paragraph and make it left-aligned Dim p1 As Paragraph = section.Paragraphs(1) p1.Format.HorizontalAlignment = HorizontalAlignment.Left 'Get the third paragraph and make it right-aligned Dim p2 As Paragraph = section.Paragraphs(2) p2.Format.HorizontalAlignment = HorizontalAlignment.Right 'Get the fourth paragraph and make it justified Dim p3 As Paragraph = section.Paragraphs(3) p3.Format.HorizontalAlignment = HorizontalAlignment.Justify 'Save the document doc.SaveToFile("WordAlignment.docx", FileFormat.Docx) End Sub End Class End Namespace
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn