文檔首頁(yè)>>E-iceblue中文文檔>>插入分頁(yè)符
插入分頁(yè)符
分頁(yè)符是控制一頁(yè)結(jié)束位置和新頁(yè)面開始位置的標(biāo)記。如果要將某個(gè)位置之后的內(nèi)容移動(dòng)到 Word 文檔的下一頁(yè),可以插入分頁(yè)符。在本文中,您將學(xué)習(xí)如何使用Spire.Doc for .NET庫(kù)在 C# 和 VB.NET 中的 Word 文檔中插入分頁(yè)符。
技術(shù)交流Q群(767755948)
為 .NET 安裝 Spire.Doc
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作為 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過(guò)NuGet安裝。
PM> Install-Package Spire.Doc
在特定段落后插入分頁(yè)符
以下是在特定段落后插入分頁(yè)符的步驟:
- 創(chuàng)建一個(gè)文檔實(shí)例。
- 使用Document.LoadFromFile()方法加載 Word 文檔。
- 使用Document.Sections[sectionIndex]屬性獲取所需的部分。
- 使用Section.Paragraphs[paragraphIndex]屬性獲取所需的段落。
- 使用Paragraph.AppendBreak(BreakType.PageBreak)方法為段落添加分頁(yè)符。
- 使用Document.SaveToFile()方法保存結(jié)果文檔。
[C#]
using Spire.Doc; using Spire.Doc.Documents; namespace InsertPageBreakAfterParagraph { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a Word document document.LoadFromFile("Sample.docx"); //Get the first section Section section = document.Sections[0]; //Get the 2nd paragraph in the section Paragraph paragraph = section.Paragraphs[1]; //Append a page break to the paragraph paragraph.AppendBreak(BreakType.PageBreak); //Save the result document document.SaveToFile("InsertPageBreak.docx", FileFormat.Docx2013); } } }
[VB.NET]
Imports Spire.Doc Imports Spire.Doc.Documents Namespace InsertPageBreakAfterParagraph Friend Class Program Private Shared Sub Main(ByVal args As String()) 'Create a Document instance Dim document As Document = New Document() 'Load a Word document document.LoadFromFile("Sample.docx") 'Get the first section Dim section As Section = document.Sections(0) 'Get the 2nd paragraph in the section Dim paragraph As Paragraph = section.Paragraphs(1) 'Append a page break to the paragraph paragraph.AppendBreak(BreakType.PageBreak) 'Save the result document document.SaveToFile("InsertPageBreak.docx", FileFormat.Docx2013) End Sub End Class End Namespace
在特定文本后插入分頁(yè)符
以下是在特定文本后插入分頁(yè)符的步驟:
- 創(chuàng)建一個(gè)文檔實(shí)例。
- 使用Document.LoadFromFile()方法加載 Word 文檔。
- 使用Document.FindString()方法查找特定文本。
- 使用TextSelection.GetAsOneRange()方法訪問(wèn)搜索文本的文本范圍。
- 使用ParagraphBase.OwnerParagraph屬性獲取文本范圍所在的段落。
- 使用Paragraph.ChildObjects.IndexOf()方法獲取段落中文本范圍的位置索引。
- 初始化Break類的實(shí)例以創(chuàng)建分頁(yè)符。
- 使用Paragraph.ChildObjects.Insert()方法在搜索文本后插入分頁(yè)符。
- 使用Document.SaveToFile()方法保存結(jié)果文檔。
[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 instance Document document = new Document(); //Load a Word document document.LoadFromFile("Sample.docx"); //Search a specific text TextSelection selection = document.FindString("celebration", true, true); //Get the text range of the seached text TextRange range = selection.GetAsOneRange(); //Get the paragraph where the text range is located Paragraph paragraph = range.OwnerParagraph; //Get the position index of the text range in the paragraph int index = paragraph.ChildObjects.IndexOf(range); //Create a page break Break pageBreak = new Break(document, BreakType.PageBreak); //Insert the page break after the searched text paragraph.ChildObjects.Insert(index + 1, pageBreak); //Save the result document document.SaveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx2013); } } }
[VB.NET]
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace InsertPageBreakAfterText Friend Class Program Private Shared Sub Main(ByVal args As String()) 'Create a Document instance Dim document As Document = New Document() 'Load a Word document document.LoadFromFile("Sample.docx") 'Search a specific text Dim selection As TextSelection = document.FindString("celebration", True, True) 'Get the text range of the seached text Dim range As TextRange = selection.GetAsOneRange() 'Get the paragraph where the text range is located Dim paragraph As Paragraph = range.OwnerParagraph 'Get the position index of the text range in the paragraph Dim index As Integer = paragraph.ChildObjects.IndexOf(range) 'Create a page break Dim pageBreak As Break = New Break(document, BreakType.PageBreak) 'Insert the page break after the searched text paragraph.ChildObjects.Insert(index + 1, pageBreak) 'Save the result document document.SaveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx2013) End Sub End Class End Namespace