C# 如何设置word文字对齐方式

2025-04-15 10:39:43

此经验将分享C# 如何来设置Word文字对齐方式。

工具/原料

Spire.Doc for .NET 版本6.1

Visual Stuido

C#代码示例

1、步骤1:编辑代码前,安装该类库,并在你的项目程序中添加引用Spire.Doc.dll(dll文件可在安装路径下的Bin文件夹中获取),如下图:

C# 如何设置word文字对齐方式

2、步骤2:添加using指令using Spire.Doc;using Spire.Doc.Documents;

3、步骤3:实例化Document类,加载文档Document doc = new Document();doc.LoadFromFile(@"test.docx");

4、步骤4:获取sectionSection s = doc.Sections[0];

5、步骤5://设置第1段文字居中对齐Paragraph p = s.Paragraphs[1];p.Format.HorizontalA造婷用痃lignment = HorizontalAlignment.Center;//设置第2段文字左对齐Paragraph p1 = s.Paragraphs[2];p1.Format.HorizontalAlignment = HorizontalAlignment.Left;//设置第5段文字右对齐Paragraph p2 = s.Paragraphs[3];p2.Format.HorizontalAlignment = HorizontalAlignment.Right;//设置第6段文字两端对齐Paragraph p3 = s.Paragraphs[4];p3.Format.HorizontalAlignment = HorizontalAlignment.Justify;

6、步骤6:保存并打开文档doc.SaveToFile("WordAl坡纠课柩ignment.docx", 僻棍募暖FileFormat.Docx);System.Diagnostics.Process.Start("WordAlignment.docx");

7、步骤7:调试运行程序,生成文件:

C# 如何设置word文字对齐方式
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢