在 Word 中對齊文本
Spire.Doc for .NET是一款專門對 Word 文檔進(jìn)行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時(shí)適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.Doc for.NET 最新下載
文本對齊是一個(gè)段落格式屬性,它決定了整個(gè)段落中文本的外觀。Microsoft Word 中有四種可用的文本對齊方式:左對齊、居中對齊、右對齊和兩端對齊。在本文中,您將學(xué)習(xí)如何使用Spire.Doc for .NET以編程方式為 Word 文檔中的段落設(shè)置不同的文本對齊方式。
為 .NET 安裝 Spire.Doc
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作為 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.Doc
在 Word 中對齊文本
- 創(chuàng)建一個(gè)文檔實(shí)例。
- 使用Document.LoadFromFile()方法加載示例 Word 文檔。
- 使用Document.Section[]屬性獲取指定部分。
- 使用Section.Paragraphs[]屬性獲取指定段落。
- 使用Paragraph.Format屬性獲取段落格式
- 使用ParagraphFormat.HorizontalAlignment屬性設(shè)置指定段落的文本對齊方式。
- 使用Document.SaveToFile()方法將文檔保存到另一個(gè)文件。
[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
	 
 
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)


 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號(hào)
渝公網(wǎng)安備
            50010702500608號(hào)
             
            
 客服熱線
客服熱線