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

How to: Add a new document part that receives a relationship ID to a package (Open XML SDK)

Last modified: July 27, 2012

Applies to: Office 2013 | Open XML

In this article
Packages and Document Parts
The Structure of a WordProcessingML Document
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 add a document part (file) that receives a relationship Id parameter for a word processing document.

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

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

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 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="http://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 2.5, 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 2.5 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.

The sample code, in this how-to, starts by passing in a parameter that represents the path to the Word document. It then creates a document as a WordprocessingDocument object.

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

It then adds the MainDocumentPart part in the new word processing document, with the relationship ID, rId1. It also adds the CustomFilePropertiesPart part and a CoreFilePropertiesPart in the new word processing document.

// Add the MainDocumentPart part in the new word processing document.
MainDocumentPart 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.
CustomFilePropertiesPart customFilePropPart = wordDoc.AddCustomFilePropertiesPart();
customFilePropPart.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties();

// Add the CoreFilePropertiesPart part in the new word processing document.
CoreFilePropertiesPart 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=\
"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\"></cp:coreProperties>");
    writer.Flush();
}

The code then adds the DigitalSignatureOriginPart part, the ExtendedFilePropertiesPart part, and the ThumbnailPart part in the new word processing document with realtionship IDs rId4, rId5, and rId6. And then it closes the wordDoc object.

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

// Add the ExtendedFilePropertiesPart part in the new word processing document.**
ExtendedFilePropertiesPart 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.Close();
NoteNote

The AddNewPart<T> method creates a relationship from the current document part to the new document part. This method returns the new document part. Also, you can use the DataPart.FeedData method to fill the document part.

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.

public 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=\
"http://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.Close();
}

Contribute to this article

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

Show:
© 2016 Microsoft