700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Word控件Spire.Doc 【段落处理】教程(一):C#/VB.NET:在 Word 中对齐文本

Word控件Spire.Doc 【段落处理】教程(一):C#/VB.NET:在 Word 中对齐文本

时间:2021-02-25 08:13:00

相关推荐

Word控件Spire.Doc 【段落处理】教程(一):C#/VB.NET:在 Word 中对齐文本

文本对齐是一个段落格式属性,它决定了整个段落中文本的外观。Microsoft Word 中有四种可用的文本对齐方式:左对齐、居中对齐、右对齐和两端对齐。在本文中,您将学习如何使用Spire.Doc for .NET以编程方式为 Word 文档中的段落设置不同的文本对齐方式。

Spire.Doc 最新下载/product/3368/download

为 .NET 安装 Spire.Doc

首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。

PM> Install-Package Spire.Doc

在 Word 中对齐文本

创建一个文档实例。使用Document.LoadFromFile()方法加载示例 Word 文档。使用Document.Sections[]属性获取指定部分。使用Section.Paragraphs[]属性获取指定段落。使用Paragraph.Format属性获取段落格式使用ParagraphFormat.HorizontalAlignment属性设置指定段落的文本对齐方式。使用Document.SaveToFile()方法将文档保存到另一个文件。

[C#]

using Spire.Doc;using Spire.Doc.Documents;namespace AlignText{class Program{static void Main(string[] args){//Create a Document instanceDocument doc = new Document();//Load a sample Word documentdoc.LoadFromFile(@"D:\Files\sample.docx");//Get the first sectionSection section = doc.Sections[0];//Get the first paragraph and make it center-alignedParagraph p = section.Paragraphs[0];p.Format.HorizontalAlignment = HorizontalAlignment.Center;//Get the second paragraph and make it left-alignedParagraph p1 = section.Paragraphs[1];p1.Format.HorizontalAlignment = HorizontalAlignment.Left;//Get the third paragraph and make it right-alignedParagraph p2 = section.Paragraphs[2];p2.Format.HorizontalAlignment = HorizontalAlignment.Right;//Get the fourth paragraph and make it justifiedParagraph p3 = section.Paragraphs[3];p3.Format.HorizontalAlignment = HorizontalAlignment.Justify;//Save the documentdoc.SaveToFile("WordAlignment.docx", FileFormat.Docx);}}}

[]

Imports Spire.DocImports Spire.Doc.DocumentsNamespace AlignTextFriend Class ProgramShared Sub Main(ByVal args() As String)'Create a Document instanceDim doc As New Document()'Load a sample Word documentdoc.LoadFromFile("E:\Work\Documents\WordDocuments\Humor Them.docx")'Get the first sectionDim section As Section = doc.Sections(0)'Get the first paragraph and make it center-alignedDim p As Paragraph = section.Paragraphs(0)p.Format.HorizontalAlignment = HorizontalAlignment.Center'Get the second paragraph and make it left-alignedDim p1 As Paragraph = section.Paragraphs(1)p1.Format.HorizontalAlignment = HorizontalAlignment.Left'Get the third paragraph and make it right-alignedDim p2 As Paragraph = section.Paragraphs(2)p2.Format.HorizontalAlignment = HorizontalAlignment.Right'Get the fourth paragraph and make it justifiedDim p3 As Paragraph = section.Paragraphs(3)p3.Format.HorizontalAlignment = HorizontalAlignment.Justify'Save the documentdoc.SaveToFile("WordAlignment.docx", FileFormat.Docx)End SubEnd ClassEnd Namespace

欢迎下载|体验更多E-iceblue产品技术交流Q群(767755948)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。