Example of deleting section from a word template?


This product is cool. I need to Delete sections based on some database conditions.
1>How do I define sections in a word template document?
2>How do I delete it?



Hi vikas,

To insert a section into the document you need to use Insert/Break command in Word and select one of the section break types which could be continious or next page and a couple of other types. Insert as many sections breaks to create sections as you want.

You will see section breaks in Word in the document as double dotted lines with word (Section break) in the middle. In Word status bar at the bottom you can see which section your cursor is currently in like Sec 1 or Sec 2.

Example how to delete a section using Aspose.Word API is available in the documentation here http://www.aspose.com/Products/Aspose.Word/Api/Aspose.Word.Range.Delete.html.


This form of notation was choosen to resemble MS Word Object Model so anyone who is familiar with programming in MS Word can easily program using Aspose.Word.

The collection is zero based, that is first section has index of 0.


Hi Roman,
Can I programmatically determine as to which section I am currently in and based on it perform the delete operation?
Following are the steps I intend to do in the code behind:
1>Open the template file.
2>Loop through all the sections and then based on the index of the section delete the section.
3>I have some merge fields too in the document that I can populate easily using your product.

Please let me know if can perform the steps above …

One more question. Is merged fields the latest way of defining in word templates?

Thanks Much (and also for the prompt reply!)


Hi Vikas,

I’m not sure what do you mean by “programmatically determine which section I am currently in”. The way this feature is supposed to be used:
1. Your program knows a bit about the structure of the template document it works with.
2. The document contains sections that represent pieces that should be deleted depending on some conditions.
3. You just delete the sections you don’t need using doc.Sections[x].Range.Delete(). You don’t normally need to enumerate through the sections because p.1.
4. Use doc.MailMerge to populate mail merge fields in the document.

For the rest you can do all what you mentioned.

To loop through sections in the document:
for (int i = 0; i < doc.Sections.Count; ++i)
Section section = doc.Sections[i];
//Do something

Note that if you want to delete sections while enumerating through them you would need to structure the loop differently either loop from last to first or use while loop.


Hi Roman,
The sample you provided helped …
Do you support bookmarks or does this assembly populates only mail merge documents?
I ask this because as far as my understanding is , To create a mail merge document I have to select a data source and in the app I am building I am going to get data via stored procs which will be joining many other tables to get the data.
I may be better off with book marks rather than mail merge I think with the ability to delete sections ofcourse …
Do you have a solution to this?



Hi Vikas,

It is simpler than that.

You just insert mail merge fields into the document in the places you need. No data source needed at that time. Simply use Insert/Field Word command, select MergeField and type field name that must match the one you later want to merge with.

You can also use Next field (also inserted in the same way) which signals to Aspose.Word to move to next record inside the data table. This is useful when creating documents such as mailing labels or invoices (invoice detail), see demo projects for this.

Then at runtime you can create and populate a DataTable in anyway you want, from stored procedures, from xml or whatever, Aspose.Word does not care.


We do not support bookmarks for marking places in the document for merge purposes just because we think it will be more complicated to users. First, bookmarks are not easily visible inside the documen. Second, you can paint yourself into a corner by creating bookmarks that overlap (how should we merge things like this?) or start or end in places where you don’t want them to. Creating and modifying bookmarks in Word is not very straightforward and takes time to get used to.


Excellent … I will give it a shot and let you know .



Hi Roman,
Everything worked like a charm until I added headers & Footers to the template document:

Headers and footers are not yet supported.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotImplementedException: Headers and footers are not yet supported.

Please let me know if this is TRUE !!



Hi Vikas,

Headers and footers and a number of other features will be included in Aspose.Word 1.2 scheduled for the end of Februrary. Beta versions will be available earlier.


Hi Roman,
Are you very positive about the release date. Since We will go ahead with this version and add the header & footers later to the template.

Following questions:
a>Will our program work without any changes after we replace it by 1.2 assembly?
b>Does word considers the header & Footer as sections? Since in our program we will be deleting known sections based on database conditions.
c>When will the beta version be available?



So far we’ve been releasing Aspose.Word on time.

1. We expect no changes to the API, only new method to accept DataSet for mail merge. But you will need either refresh a reference in your project and recompile or redirect to use new version using application configuration file (I recommend you just recompile).

2. Each section in Word can have its own headers and footers. It could also be the case that the whole document regardless of the number of sections has same headers and footers. In addition to that, headers and footers can be different for first page and even and odd pages. I expect that when each section has its own headers and footers they will be deleted when the section is deleted.

3. The closest beta that supports headers and footers and embedded images is going to be available around 10-11th Feb.


Hi Roman,
I sent a sample document I am processing at your email id : recurrence@aspose.com . Please check…



Thanks, I’ve got the file. I will reply here tomorrow.


Hi Roman,

I have attached a sample document. Please preview it. It has headers & Footers. I see what you are saying. So is there a way to not delete Header and footers ? In my case I have to include the following table in sections which can be deleted but not the header & Footers which are the same for all these blocks. Only the first page has a different header. Please advise.


Hi Vikas,

There will be no problems with this document. The headers and footers of the document will stay intact when you delete a section.

By the way, you should probably use a numbered list to mark the items in your document so they will remain properly numbered even if you delete items from the middle. I was able to do this in your document.


Hi Vikas,

Aspose.Word 1.1.2 that supports headers and footers and some other features is available here http://www.aspose.com/forums/ShowPost.aspx?PostID=3710#4175.