Add a new document part that receives a relationship ID to a package

This topic shows how to use the classes in the Open XML SDK for Office to add a document part (file) that receives a relationship Id parameter for a word processing document.


Packages and Document Parts

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.


Structure of a WordProcessingML Document

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 following code example shows the WordprocessingML markup for a document that contains the text "Example text."

    <w:document xmlns:w="https://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:body>
        <w:p>
          <w:r>
            <w:t>Example text.</w:t>
          </w:r>
        </w:p>
      </w:body>
    </w:document>

Using the Open XML SDK, you can create document structure and content using strongly-typed classes that correspond to WordprocessingML elements. You will 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 Class Description
document Document The root element for the main document part.
body Body The container for the block level structures such as paragraphs, tables, annotations and others specified in the ISO/IEC 29500 specification.
p Paragraph A paragraph.
r Run A run.
t Text A range of text.

For more information about the overall structure of the parts and elements of a WordprocessingML document, see Structure of a WordprocessingML document.


Sample Code

The following code, adds a new document part that contains custom XML from an external file and then populates the document part. You can call the method AddNewPart by using a call like the following code example.

    string document = @"C:\Users\Public\Documents\MyPkg1.docx";
    AddNewPart(document);

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


using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
using System.Xml;


static void AddNewPart(string document)
{
    // Create a new word processing document.
    WordprocessingDocument wordDoc =
       WordprocessingDocument.Create(document,
       WordprocessingDocumentType.Document);

    // Add the MainDocumentPart part in the new word processing document.
    var mainDocPart = wordDoc.AddNewPart<MainDocumentPart>
("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", "rId1");
    mainDocPart.Document = new Document();

    // Add the CustomFilePropertiesPart part in the new word processing document.
    var customFilePropPart = wordDoc.AddCustomFilePropertiesPart();
    customFilePropPart.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties();

    // Add the CoreFilePropertiesPart part in the new word processing document.
    var coreFilePropPart = wordDoc.AddCoreFilePropertiesPart();
    using (XmlTextWriter writer = new
XmlTextWriter(coreFilePropPart.GetStream(FileMode.Create), System.Text.Encoding.UTF8))
    {
        writer.WriteRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<cp:coreProperties xmlns:cp=\"https://schemas.openxmlformats.org/package/2006/metadata/core-properties\"></cp:coreProperties>");
        writer.Flush();
    }

    // Add the DigitalSignatureOriginPart part in the new word processing document.
    wordDoc.AddNewPart<DigitalSignatureOriginPart>("rId4");

    // Add the ExtendedFilePropertiesPart part in the new word processing document.
    var extendedFilePropPart = wordDoc.AddNewPart<ExtendedFilePropertiesPart>("rId5");
    extendedFilePropPart.Properties =
new DocumentFormat.OpenXml.ExtendedProperties.Properties();

    // Add the ThumbnailPart part in the new word processing document.
    wordDoc.AddNewPart<ThumbnailPart>("image/jpeg", "rId6");

    wordDoc.Dispose();
}

See also