How C # handles Word document paging-inserting, deleting, blocking pagination

Source: Internet
Author: User

This article describes how C # programming handles word pagination. Here are a few things you can do to work with pagination in Word:

    1. Insert Paging
      1.1 Inserting a page at the end of a specified paragraph
      1.2 Insert pagination in the specified word specifier
    2. Delete Paging
      3. Blocking table pagination

processing tool : Spire.doc for. NET 6.1
After you install the class library, reference the Spire.Doc.dll file in your program (for example), and the DLL file is available in the Bin folder under the installation path.

"Example 1" Inserts a page (insert pagination at the end of the specified paragraph)

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

Debug run program, generate document.
Before and after the page effect comparison add:
Before paging

After paging

"Example 2" Insert pagination (Insert page specifier in the specified word)

C#

  using spire.doc;using spire.doc.documents;using spire.doc.fields;namespace insertpagebreak1_doc{class program {static void Main (string[] args) {//Create instance, load file document doc = new Document            (); Doc.            LoadFromFile ("Test.docx"); Find the character textselection[] selections = doc that you want to insert a page after.            Findallstring ("Guests", true, true); Traverse the document, insert paging 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); }//Save and open document Doc.            SaveToFile ("Break.docx", fileformat.docx);        System.Diagnostics.Process.Start ("Break.docx"); }    }}

Test results:

"Example 3" removes paging

C#

Using spire.doc;using Spire.doc.documents;namespace removepagebreak_doc{class Program {static void Main (str                Ing[] (args) {{//Instantiate document class, load file document document = new document (); Document.                LoadFromFile ("Sample.docx", fileformat.docx); Iterates through all the paragraphs in the first section, removing paging 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.child                        Objects[i]; if (obj. Documentobjecttype = = Documentobjecttype.break) {Break b = obj as break                            ;                        P.childobjects.remove (b); }}}//Save and open file document. SaveToFile ("REsult.docx ", fileformat.docx);            System.Diagnostics.Process.Start ("Result.docx"); }        }    }}

Comparison of test results:
Original document:

After the paging is deleted:

"Example 4" Prevents Word tables from paging

The test files are as follows:

method One : Reposition the spread table on the same page
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");         }    }}

Test results:

method Two : Prevent the same row of data from being forced to page
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");        }    }}

Test results:

All of this is a way to work with page breaks in Word. If you want to reprint, please indicate the source.

How C # handles Word document paging-inserting, deleting, blocking pagination

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.