此经验将分享C# 如何来设置Word文字对齐方式。
工具/原料
Spire.Doc for .NET 版本6.1
Visual Stuido
C#代码示例
1、步骤1:编辑代码前,安装该类库,并在你的项目程序中添加引用Spire.Doc.dll(dll文件可在安装路径下的Bin文件夹中获取),如下图:
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:调试运行程序,生成文件: