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

Spire.Doc for.NET 最新下载(qun:767755948)https://www.evget.com/product/3368/download

为 .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 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.DocumentsNamespace 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

在特定文本后插入分页符

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

  • 创建一个文档实例。
  • 使用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 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.FieldsNamespace 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

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

  1. Word控件Spire.Doc 转换教程(六):如何将 XML 转换为 Word

    为什么使用 C#/VB.NET 将 Office OpenXML 转换为 Word? 作为一种出色的编程语言,Office OpenXML 在其自然形式下很难导航和访问.将文件从 Office Ope ...

  2. Word控件Spire.Doc 转换教程(二十三):保留或禁用从 Word 到 PDF 转换的超链接

    超链接为读者提供了更多的附加信息,它被广泛用于我们的 word 文档.Spire.Doc 具有强大的超链接文字元素操作功能.开发者可以在word文档中添加.修改和删除超链接.Spire.Doc for ...

  3. Word控件Spire.Doc 转换教程(十一):如何将 HTML 转换为图像

    Spire.Doc 可以帮助用户使用 C#/VB.NET 将HTML 转换为 Image.该解决方案使用户可以随时随地通过手机.MP4播放器.PSP.iPad.iTouch等便携式设备阅读HTML.按 ...

  4. Word控件Spire.Doc 转换教程(九):在 Doc 转PDF 转换期间设置图像质量

    您可能会好奇为什么我们在将Doc 转换为 PDF时设置图像质量.在我们转换Word文档后,尤其是文档中有很多图像,输出的PDF文档的大小明显大于原始的Doc文件.现在,Spire.Doc 新版本的新功 ...

  5. Word控件Spire.Doc 转换教程(十五):在 C# 中将 HTML 文件转换为 PDF 和 XPS

    HTML 文件广泛用于收集数据,出于安全,我们需要将其转换为 PDF 和 XPS 格式的可打印文档.您可以使用Spire.PDF轻松地将 URL 中的 HTML 页面转换为高质量的 PDF.本文将重点 ...

  6. Word控件Spire.Doc 转换教程(十二):如何将 Word 转换为 EPub、XPS、Emf

    本文将介绍一种通过名为 Spire.Doc 的强大且独立的Word .NET 组件将 Word 转换为 EPub.XPS.Emf的简单方法,无需在机器上安装 Microsoft Word.它还支持将 ...

  7. Word控件Spire.Doc 转换教程(十八): 将 RTF 转换为图像并重置图像分辨率

    Spire.Doc具有在 C# 和 VB.NET 中操作 RTF 文件格式的强大能力.通过使用 Spire.Doc,开发人员可以将 RTF 转换为 PDF.HTML和 .doc..docx 格式的 w ...

  8. Word控件Spire.Doc 转换教程(七):如何将 Word 转换为 RTF

    为什么要将 Word 转换为 RTF? 自 1990 年代以来,Microsoft 开始为其一系列专有的文字处理格式使用 .doc 扩展名.Doc 格式通常与 Microsoft Office 应用程 ...

  9. Word控件Spire.Doc 转换教程(二十八):将 Word 转换为 PCL

    PCL 文件是以打印机命令语言(通常称为 PCL)页面描述语言创建的数字打印文档.从v7.1.19 开始,Spire.Doc 支持将 word 文档转换为 PCL.PCL文件的标准有很多种:这里的 P ...

  10. Word控件Spire.Doc 转换教程(二十五):在 C#、VB.NET 中将 ODT 转换为 DOC

    具有 .ODT 文件扩展名的文件是 OpenDocument 文本文档文件.这些文件通常由免费的 OpenOffice Writer 文字处理器程序创建.ODT 文件类似于与 Microsoft Wo ...

最新文章

  1. http://hi.baidu.com/grayworm/blog/item/c76c4046307d380a6a63e56a.html
  2. UOJ#449. 【集训队作业2018】喂鸽子
  3. latex 小于_一份菜鸡的Latex课堂作业works--(ii)
  4. matlab resample上采样,resample matlab实现
  5. Python QT5
  6. Altium Designer20 PCB板子绘制
  7. 2011 MVP大奖礼品包,那是相当的给力啊!!
  8. InVEST model中生境质量
  9. Layui的轮播事件和echarts插件事件
  10. 光学计算机的工作原理,使用光学计算机的人工智能超分辨率
  11. 抓取scrapy中文文档(我的第一个爬虫)
  12. 关于DevExpress Winform GridControl GridView 主从表(Master-Detail)导出Excel问题
  13. man手册中文版设置
  14. 2009年最新虚拟主机提供商排名榜
  15. CSS3彩色渐变的电脑键盘js特效
  16. 自家小程序 如何实现定点跳转其他(比如:京东,天猫,淘宝)的小程序的具体店铺
  17. 计算机开机自动进入bios,电脑开机自动进入bios界面的原因及解决办法
  18. ARM简单程序设计【嵌入式系统】
  19. 蓝队工具:使用VirusTotal API校验样本
  20. cisco 3548x 低延时交换机erspan获取ns时间戳

热门文章

  1. 编程人员的不二之选 LEGION Y9000X正式发布
  2. Diablo3英雄榜-使用Volley和Gson来处理暴雪API的Json数据
  3. 计算机网络冗余码计算
  4. 原子操作:CAS、TAS、TTAS、FAA浅析
  5. VMware Tanzu Application Service——Config Client (TAS)
  6. 局域网有几台电脑频繁断网_关于电脑经常掉线的那些事,解决方法
  7. word审阅 去除word回车换行时出现的竖线的方法
  8. 数据结构(六)——间接寻址
  9. 私活,永远解救不了自己屌丝的人生!
  10. 立即收藏!2019前端工程师如何应对裁员潮?