sprie.doc 操作word文档
作者:互联网
一、设置段落对齐方式
-
步骤1:编辑代码前,安装该类库,并在你的项目程序中添加引用Spire.Doc.dll(dll文件可在安装路径下的Bin文件夹中获取),如下图:
-
步骤2:添加using指令
using Spire.Doc;
using Spire.Doc.Documents;
-
步骤3:实例化Document类,加载文档
Document doc = new Document();
doc.LoadFromFile(@"test.docx");
-
步骤4:获取section
Section s = doc.Sections[0];
-
步骤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:保存并打开文档
doc.SaveToFile("WordAlignment.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("WordAlignment.docx");
-
步骤7:调试运行程序,生成文件:
END
二、设置页边距
//实例化一个Document对象
Document doc = new Document();
//载入测试的Word文档
doc.LoadFromFile("test.docx");
//获取第一个Section对象
Section sec = doc.Sections[0];
//设置纸张大小为信纸
sec.PageSetup.PageSize = PageSize.Letter;
//分别设置四个方向的页边距
sec.PageSetup.Margins.Top = 20f;
sec.PageSetup.Margins.Left = 30f;
sec.PageSetup.Margins.Bottom = 20f;
sec.PageSetup.Margins.Right = 30f;
//把纸张方向设置为横向
sec.PageSetup.Orientation = PageOrientation.Landscape;
//保存并打开文档
doc.SaveToFile("result.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("result.docx");
标签:docx,word,doc,sec,sprie,HorizontalAlignment,Document,PageSetup 来源: https://www.cnblogs.com/sunzzhankui/p/15361695.html