How to: Remove a document part from a package (Open XML SDK)

Last modified: July 27, 2012

Applies to: Office 2013 | Open XML

In this article
Packages and Document Parts
Getting a WordprocessingDocument Object
Basic Structure of a WordProcessingML Document
Settings Element
How the Sample Code Works
Sample Code

This topic shows how to use the classes in the Open XML SDK 2.5 for Office to remove a document part (file) from a Wordprocessing document programmatically.

The following assembly directives are required to compile the code in this topic.

using System;
using DocumentFormat.OpenXml.Packaging;

An Open XML document is stored as a package, whose format is defined by ISO/IEC 29500-2. The package can have multiple parts with relationships between them. The relationship between parts controls the category of the document. A document can be defined as a word-processing document if its package-relationship item contains a relationship to a main document part. If its package-relationship item contains a relationship to a presentation part it can be defined as a presentation document. If its package-relationship item contains a relationship to a workbook part, it is defined as a spreadsheet document. In this how-to topic, you will use a word-processing document package.

The code example starts with opening a package file by passing a file name as an argument to one of the overloaded Open() methods of the DocumentFormat.OpenXml.Packaging.WordprocessingDocument that takes a string and a Boolean value that specifies whether the file should be opened in read/write mode or not. In this case, the Boolean value is true specifying that the file should be opened in read/write mode.

// Open a Wordprocessing document for editing.
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
      // Insert other code here.

The using statement provides a recommended alternative to the typical .Create, .Save, .Close sequence. It ensures that the Dispose method (internal method used by the Open XML SDK to clean up resources) is automatically called when the closing brace is reached. The block that follows the using statement establishes a scope for the object that is created or named in the using statement, in this case wordDoc. Because the WordprocessingDocument class in the Open XML SDK automatically saves and closes the object as part of its System.IDisposable implementation, and because the Dispose method is automatically called when you exit the block; you do not have to explicitly call Save and Close─as long as you use using.

The basic document structure of a WordProcessingML document consists of the document and body elements, followed by one or more block level elements such as p, which represents a paragraph. A paragraph contains one or more r elements. The r stands for run, which is a region of text with a common set of properties, such as formatting. A run contains one or more t elements. The t element contains a range of text. The WordprocessingML markup for the document that the sample code creates is shown in the following code example.

<w:document xmlns:w="">
        <w:t>Create text in body - CreateWordprocessingDocument</w:t>

Using the Open XML SDK 2.5, you can create document structure and content using strongly-typed classes that correspond to WordprocessingML elements. You can find these classes in the DocumentFormat.OpenXml.Wordprocessing namespace. The following table lists the class names of the classes that correspond to the document, body, p, r, and t elements.

WordprocessingML Element

Open XML SDK 2.5 Class




The root element for the main document part.



The container for the block level structures such as paragraphs, tables, annotations, and others specified in the ISO/IEC 29500 specification.



A paragraph.



A run.



A range of text.

The following text from the ISO/IEC 29500 specification introduces the settings element in a PresentationML package.

This element specifies the settings that are applied to a WordprocessingML document. This element is the root element of the Document Settings part in a WordprocessingML document.[Example: Consider the following WordprocessingML fragment for the settings part of a document:
  <w:defaultTabStop w:val="720" />
  <w:characterSpacingControl w:val="dontCompress" />
The settings element contains all of the settings for this document. In this case, the two settings applied are automatic tab stop increments of 0.5" using the defaultTabStop element, and no character level white space compression using the characterSpacingControl element. end example]© ISO/IEC29500: 2008.

After you have opened the document, in the using statement, as a WordprocessingDocument object, you create a reference to the DocumentSettingsPart part. You can then check if that part exists, if so, delete that part from the package. In this instance, the settings.xml part is removed from the package.

MainDocumentPart mainPart = wordDoc.MainDocumentPart;
if (mainPart.DocumentSettingsPart != null)

The following code removes a document part from a package. To run the program, call the method RemovePart like this example.

string document = @"C:\Users\Public\Documents\MyPkg6.docx";

Before running the program on the test file, "," for example, open the file by using the Open XML SDK 2.5 Productivity Tool for Microsoft Office and examine its structure. After running the program, examine the file again, and you will notice that the DocumentSettingsPart part was removed.

Following is the complete code example in both C# and Visual Basic.

// To remove a document part from a package.
public static void RemovePart(string document)
  using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
     MainDocumentPart mainPart = wordDoc.MainDocumentPart;
     if (mainPart.DocumentSettingsPart != null)

Contribute to this article

Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub.