1. 程式人生 > >C# 如何處理Word文檔分頁——插入、刪除、阻止分頁

C# 如何處理Word文檔分頁——插入、刪除、阻止分頁

spi 測試 lec == 設置表格 調試 安裝路徑 text tps

本篇文章將介紹C#編程如何來處理Word分頁的方法。操作Word中的分頁這裏分為幾種情況的來介紹:

  1. 插入分頁
    1.1在指定段落末尾插入分頁
    1.2 在指定字符後插入分頁
  2. 刪除分頁
    3.阻止表格分頁

處理工具:Spire.Doc for .NET 6.1
安裝該類庫後,在程序中引用Spire.Doc.dll文件即可(如下圖),dll文件在安裝路徑下Bin文件夾中獲取。
技術分享圖片

【示例1】插入分頁(在指定段落末尾插入分頁)

【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");

        }
    }
}

調試運行程序,生成文檔。
分頁前後效果對比添:
分頁前
技術分享圖片

分頁後
技術分享圖片

【示例2】插入分頁(在指定字符後插入分頁)

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");

        }
    }
}

測試結果:
技術分享圖片

【示例3】刪除分頁

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");
            }
        }
    }
}

測試效果對比:
原文檔:
技術分享圖片
技術分享圖片

刪除分頁後:
技術分享圖片

【示例4】阻止Word表格分頁

測試文件如下:
技術分享圖片

方法一:將跨頁的表格重新定位放置在同一個頁面上
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中的分頁符的方法。如需轉載,請註明出處。

C# 如何處理Word文檔分頁——插入、刪除、阻止分頁