C# 如何处理Word文档分页——插入、删除、阻止分页
本篇文章将介绍C#编程如何来处理Word分页的方法。操作Word中的分页这里分为几种情况的来介绍:
插入分页1.1在指定段落末尾插入分页
1.2 在指定字符后插入分页删除分页
3.阻止表格分页
处理工具:Spire.Doc for .NET 6.1
安装该类库后,在程序中引用Spire.Doc.dll文件即可(如下图),dll文件在安装路径下Bin文件夹中获取。
【C#】
using Spire.Doc;using Spire.Doc.Documents;namespace InsertPageBreak_Doc{ class Program { static void Main(string[] args) { //创建实例,加载文件 Document document = new Document(); document.LoadFromFile("test.docx"); //在指定段落末尾,插入分页 document.Sections[0].Paragraphs[1].AppendBreak(BreakType.PageBreak); //保存文件并打开 document.SaveToFile("PageBreak.docx", FileFormat.Docx2010); System.Diagnostics.Process.Start("PageBreak.docx"); } }}
调试运行程序,生成文档。
分页前后效果对比添:
分页前
分页后
C#
using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;namespace InsertPagebreak1_Doc{ class Program { static void Main(string[] args) { //创建实例,加载文件 Document doc = new Document(); doc.LoadFromFile("test.docx"); //查找需要在其后插入分页的字符 TextSelection[] selections = doc.FindAllString("guests", true, true); //遍历文档,插入分页 foreach (TextSelection ts in selections) { TextRange range = ts.GetAsOneRange(); Paragraph paragraph = range.OwnerParagraph; int index = paragraph.ChildObjects.IndexOf(range); Break pageBreak = new Break(doc, BreakType.PageBreak); paragraph.ChildObjects.Insert(index + 1, pageBreak); } //保存并打开文档 doc.SaveToFile("Break.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Break.docx"); } }}
测试结果:
C#
using Spire.Doc;using Spire.Doc.Documents;namespace RemovePagebreak_Doc{ class Program { static void Main(string[] args) { { //实例化Document类,加载文件 Document document = new Document(); document.LoadFromFile("sample.docx", FileFormat.Docx); //遍历第一节中的所有段落,移除分页 for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++) { Paragraph p = document.Sections[0].Paragraphs[j]; for (int i = 0; i < p.ChildObjects.Count; i++) { DocumentObject obj = p.ChildObjects[i]; if (obj.DocumentObjectType == DocumentObjectType.Break) { Break b = obj as Break; p.ChildObjects.Remove(b); } } } //保存并打开文件 document.SaveToFile("result.docx", FileFormat.Docx); System.Diagnostics.Process.Start("result.docx"); } } }}
测试效果对比:
原文档:
删除分页后:
测试文件如下:
方法一:将跨页的表格重新定位放置在同一个页面上
C#
using Spire.Doc;using Spire.Doc.Documents;namespace PreventPagebreak_Table__Doc{ class Program { static void Main(string[] args) { //创建Document类实例,加载文档 Document doc = new Document("test.docx"); //获取表格 Table table = doc.Sections[0].Tables[0] as Table; //设置表格的段落位置,保持表格在同一页 foreach (TableRow row in table.Rows) { foreach (TableCell cell in row.Cells) { foreach (Paragraph p in cell.Paragraphs) { p.Format.KeepFollow = true; } } } //保存文件并打开 doc.SaveToFile("result.docx", FileFormat.Docx2010); System.Diagnostics.Process.Start("result.docx"); } }}
测试效果:
方法二:阻止同一行数据被强制分页
C#
using Spire.Doc;using Spire.Doc.Documents;namespace PreventPagebreak_Table__Doc{ class Program { static void Main(string[] args) { //创建实例,加载文件 Document doc = new Document("test.docx"); //获取指定表格 Table table = doc.Sections[0].Tables[0] as Table; //设置表格分页属性 table.TableFormat.IsBreakAcrossPages = false; //保存并打开文件 doc.SaveToFile("output.docx", FileFormat.Docx2010); System.Diagnostics.Process.Start("output.docx"); } }}
测试效果:
以上全部是本次关于如何操作Word中的分页符的方法。如需转载,请注明出处。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。