我们操作一些比较大的 Word 文档时,例如论文,常常含有封面、绪论、正文等不同的章节,编排页码时,需要对不同章节内容来添加不同的页码,包括页码数字格式、起始编号等。通过使用 Spire.Doc for .NET,可实现根据 Word 文档的不同章节来设置不同的编号样式或更改起始页码。本文,将对此作详细介绍。
安装 Spire.Doc for .NET
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从 此链接 下载或通过 NuGet 安装。
PM> Install-Package Spire.Doc
为不同章节设置不同页码
添加页码时,需要获取对应的章节,通过页面设置,添加页码,指定页码格式及起始数字。以下是详细代码步骤:
- 创建 Document 类的对象,并调用 Document.LoadFromFile(string fileName) 方法加载 Word 文档。
- 通过 Documnet.Sections[int] 属性获取文档指定章节。
- 通过 HeadersFooters.Footer 属性章节中的获取页脚,并通过 HeaderFooter.AddParagraph() 方法在页脚中添加段落。
- 通过 Paragraph.AppendField(string fieldName, FieldType fieldType) 方法添加页码域到页脚,以及 ParagraphFormat.HorizontalAlignment 属性设置段落对齐方式。
- 创建 ParagraphStyle 类的对象,用于创建段落样式,指定字体名称、字体大小、是否加粗、字体颜色等;并通过 Paragraph.ApplyStyle(string styleName) 方法应用到页脚段落。
- 通过 PageNumberStyle 属性来设置页码样式。
- 通过 PageSetup.RestartPageNumbering 属性,指定是否重新编号。
- 通过 PageSetup.PageStartingNumber 属性设置页面起始编号值。
- 最后,调用Document.SaveToFile(string fileName, FileFormat fileFormat) 方法保存文档。
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace DifferentPageNumber_Doc
{
class Program
{
static void Main(string[] args)
{
//创建Document对象,并加载测试文档
Document doc = new Document();
doc.LoadFromFile("test.docx");
//获取第一节
Section section1 = doc.Sections[0];
//实例化HeaderFooter对象(指定页码添加位置:页眉或页脚)
HeaderFooter footer = section1.HeadersFooters.Footer;
//添加段落到页脚
Paragraph footerParagraph = footer.AddParagraph();
//添加页码域到页脚
footerParagraph.AppendField("page number", FieldType.FieldPage);
//设置页码右对齐
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
//创建段落样式,包括字体名称、大小、颜色
ParagraphStyle style = new ParagraphStyle(doc);
style.CharacterFormat.FontName = "黑体";
style.CharacterFormat.FontSize = 15;
style.CharacterFormat.Bold = true;
style.CharacterFormat.TextColor = Color.Black;
doc.Styles.Add(style);
//应用段落样式到页脚
footerParagraph.ApplyStyle(style.Name);
//将第一节的页码样式设置为罗马数字
section1.PageSetup.PageNumberStyle = PageNumberStyle.RomanLower;
Section section2 = doc.Sections[1];
//将第二节的页码样式设置为阿拉伯数字,并重新开始编码
section2.PageSetup.PageNumberStyle = PageNumberStyle.Arabic;
section2.PageSetup.RestartPageNumbering = true;
section2.PageSetup.PageStartingNumber = 1;
//保存文档
doc.SaveToFile("output.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("output.docx");
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Namespace DifferentPageNumber_Doc
Class Program
Private Shared Sub Main(args As String())
'创建Document对象,并加载测试文档
Dim doc As New Document()
doc.LoadFromFile("test.docx")
'获取第一节
Dim section1 As Section = doc.Sections(0)
'实例化HeaderFooter对象(指定页码添加位置:页眉或页脚)
Dim footer As HeaderFooter = section1.HeadersFooters.Footer
'添加段落到页脚
Dim footerParagraph As Paragraph = footer.AddParagraph()
'添加页码域到页脚
footerParagraph.AppendField("page number", FieldType.FieldPage)
'设置页码右对齐
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right
'创建段落样式,包括字体名称、大小、颜色
Dim style As New ParagraphStyle(doc)
style.CharacterFormat.FontName = "黑体"
style.CharacterFormat.FontSize = 15
style.CharacterFormat.Bold = True
style.CharacterFormat.TextColor = Color.Black
doc.Styles.Add(style)
'应用段落样式到页脚
footerParagraph.ApplyStyle(style.Name)
'将第一节的页码样式设置为罗马数字
section1.PageSetup.PageNumberStyle = PageNumberStyle.RomanLower
Dim section2 As Section = doc.Sections(1)
'将第二节的页码样式设置为阿拉伯数字,并重新开始编码
section2.PageSetup.PageNumberStyle = PageNumberStyle.Arabic
section2.PageSetup.RestartPageNumbering = True
section2.PageSetup.PageStartingNumber = 1
'保存文档
doc.SaveToFile("output.docx", FileFormat.Docx2013)
System.Diagnostics.Process.Start("output.docx")
End Sub
End Class
End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。