In today's post I am going to show you how to remove page and section breaks within a Word document using the Open XML SDK. Removing these two types of breaks is similar, but requires two different approaches. Let's start off by jumping into removing page breaks.
My post will talk about using version 2 of the SDK.
If you just want to jump straight into the code, feel free to download this solution here.
To remove page breaks in a document we need to take the following actions:
For the sake of this example, let's say I am starting with the following Word document:
This document has a page break (shown outlined in red) on the first page.
The code is pretty straight forward and follows the solution steps as described above in the solutions section:
Pretty easy stuff!
Running this code I should end up with a document that looks like the following:
Now let's see how to remove section breaks within a document. Before I actually jump into the solution of removing sections, I want to talk a bit about section breaks within a Word document.
WordprocessingML does not natively store the concept of pages, since it is based on paragraphs and runs. Instead it uses sections to specify groups of paragraphs that have a specific set of page properties.
Every Word document has at least one section, where each section specifies page properties (like page size, orientation, margins, etc), header/footer references, column information, etc. Given this information, there are really two high level types of sections:
In today's post I am going to show you how to remove all sections that are a paragraph property.
To remove section breaks in a document we need to take the following actions:
This document has a section break (shown outlined in red) on the first page, which separates a one column section from a two column section.
This code is also pretty straight forward and follows the solution steps as described above in the solutions section:
Notice how the document now has two columns. This solution removed the first section property, which specified a one column section.