Word控件Spire.Doc 【页面设置】教程(1):在C#/VB.NET:在 Word 文档中插入分页符

在本文中,您将学习如何使用Spire.Doc for .NET库在 C# 和 VB.NET 中的 Word 文档中插入分页符。

分页符是控制一页结束位置和新页面开始位置的标记。如果要将某个位置之后的内容移动到 Word 文档的下一页,可以插入分页符。在本文中,您将学习如何使用Spire.Doc for .NET库在 C# 和 VB.NET 中的 Word 文档中插入分页符。

为 .NET 安装 Spire.Doc

首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。

PM> Install-Package Spire.Doc
在特定段落后插入分页符

以下是在特定段落后插入分页符的步骤:

  • 创建一个文档实例。
  • 使用Document.LoadFromFile()方法加载 Word 文档。
  • 使用Document.Sections[sectionIndex]属性获取所需的部分。
  • 使用Section.Paragraphs[paragraphIndex]属性获取所需的段落。
  • 使用Paragraph.AppendBreak(BreakType.PageBreak)方法为段落添加分页符。
  • 使用Document.SaveToFile()方法保存结果文档。

[C#]

using Spire.Doc;using Spire.Doc.Documents;namespace InsertPageBreakAfterParagraph{class Program{static void Main(string[] args){//Create a Document instanceDocument document = new Document();//Load a Word documentdocument.LoadFromFile("Sample.docx");//Get the first sectionSection section = document.Sections[0];//Get the 2nd paragraph in the sectionParagraph paragraph = section.Paragraphs[1];//Append a page break to the paragraphparagraph.AppendBreak(BreakType.PageBreak);//Save the result documentdocument.SaveToFile("InsertPageBreak.docx", FileFormat.Docx2013);}}}

[VB.NET]

Imports Spire.DocImports Spire.Doc.DocumentsNamespace InsertPageBreakAfterParagraphFriend Class ProgramPrivate Shared Sub Main(ByVal args As String())'Create a Document instanceDim document As Document = New Document()'Load a Word documentdocument.LoadFromFile("Sample.docx")'Get the first sectionDim section As Section = document.Sections(0)'Get the 2nd paragraph in the sectionDim paragraph As Paragraph = section.Paragraphs(1)'Append a page break to the paragraphparagraph.AppendBreak(BreakType.PageBreak)'Save the result documentdocument.SaveToFile("InsertPageBreak.docx", FileFormat.Docx2013)End SubEnd ClassEnd Namespace

C#/VB.NET:在 Word 文档中插入分页符
在特定文本后插入分页符

以下是在特定文本后插入分页符的步骤:

  • 创建一个文档实例。
  • 使用Document.LoadFromFile()方法加载 Word 文档。
  • 使用Document.FindString()方法查找特定文本。
  • 使用TextSelection.GetAsOneRange()方法访问搜索文本的文本范围。
  • 使用ParagraphBase.OwnerParagraph属性获取文本范围所在的段落。
  • 使用Paragraph.ChildObjects.IndexOf()方法获取段落中文本范围的位置索引。
  • 初始化Break类的实例以创建分页符。
  • 使用Paragraph.ChildObjects.Insert()方法在搜索文本后插入分页符。
  • 使用Document.SaveToFile()方法保存结果文档。

[C#]

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System;namespace InsertPageBreakAfterText{class Program{static void Main(string[] args){//Create a Document instanceDocument document = new Document();//Load a Word documentdocument.LoadFromFile("Sample.docx");//Search a specific textTextSelection selection = document.FindString("celebration", true, true);//Get the text range of the seached textTextRange range = selection.GetAsOneRange();//Get the paragraph where the text range is locatedParagraph paragraph = range.OwnerParagraph;//Get the position index of the text range in the paragraphint index = paragraph.ChildObjects.IndexOf(range);//Create a page breakBreak pageBreak = new Break(document, BreakType.PageBreak);//Insert the page break after the searched textparagraph.ChildObjects.Insert(index + 1, pageBreak);//Save the result documentdocument.SaveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx2013);}}}

[VB.NET]

Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsNamespace InsertPageBreakAfterTextFriend Class ProgramPrivate Shared Sub Main(ByVal args As String())'Create a Document instanceDim document As Document = New Document()'Load a Word documentdocument.LoadFromFile("Sample.docx")'Search a specific textDim selection As TextSelection = document.FindString("celebration", True, True)'Get the text range of the seached textDim range As TextRange = selection.GetAsOneRange()'Get the paragraph where the text range is locatedDim paragraph As Paragraph = range.OwnerParagraph'Get the position index of the text range in the paragraphDim index As Integer = paragraph.ChildObjects.IndexOf(range)'Create a page breakDim pageBreak As Break = New Break(document, BreakType.PageBreak)'Insert the page break after the searched textparagraph.ChildObjects.Insert(index + 1, pageBreak)'Save the result documentdocument.SaveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx2013)End SubEnd ClassEnd Namespace

C#/VB.NET:在 Word 文档中插入分页符

欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年5月5日
下一篇 2022年5月5日

相关推荐

发表回复

登录后才能评论