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

时间:2026-02-14 15:02:15

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:获取section 

Section s = doc.Sections[0];

5、步骤5:

//设置第1段文字居中对齐

Paragraph p = s.Paragraphs[1];

p.Format.HorizontalAlignment = 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("WordAlignment.docx", FileFormat.Docx);

System.Diagnostics.Process.Start("WordAlignment.docx");

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

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

© 2026 长短途
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com