Compartir a través de


How to: Add a New Document Part that Receives a Relationship Id to an Office Open XML Package by Using the Open XML API

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The Office Open XML Package specification defines a set of XML files that contain the content and define the relationships for all of the document parts stored in a single package. These packages combine the parts that comprise the document files for Microsoft® Office Excel® 2007, Microsoft Office PowerPoint® 2007, and Microsoft Office Word 2007. The Open XML Application Programming Interface (API) allows you to create packages and manipulate the files that comprise the packages. This topic walks through the code and steps to add a document part (file) that receives a relationship Id parameter for an Office Open XML package in Word 2007, although the steps are the same for each of the three 2007 Microsoft Office system programs that support the Office Open XML Format.

NoteNote

The code samples in this topic are in Microsoft Visual Basic® .NET and Microsoft Visual C#®. You can use them in an add-in created in Microsoft Visual Studio® 2008. For more information about how to create an add-in in Visual Studio 2008, see Getting Started with the Open XML Format SDK 1.0.

Adding a New Document Part to the Package

In the following code, you add a new document part containing custom XML from an external file and then populate the document part.

NoteNote

These code examples are modified to facilitate online viewing. Change the indentation and remove extra lines to work with this code.

' How to add a new document part that receives a relationship ID to a package
Public Sub AddNewPart(ByVal newFilePath As String)
   ' Create a new Word document.
   Dim wordDoc As WordprocessingDocument = 
      WordprocessingDocument.Create(newFilePath, 
      WordprocessingDocumentType.Document)

   ' Add the MainDocumentPart part in new Word document.
   wordDoc.AddNewPart(Of MainDocumentPart)
      ("application/vnd.openxmlformats-
      officedocument.wordprocessingml.document.main+xml", "rId1")

   ' Add the CustomFilePropertiesPart part in new Word document.
   wordDoc.AddNewPart(Of CustomFilePropertiesPart)("rId2")

   ' Add the CoreFilePropertiesPart part in the new Word document.
   wordDoc.AddNewPart(Of CoreFilePropertiesPart)("rId3")

   ' Add the DigitalSignatureOriginPart part in the new Word document.
   wordDoc.AddNewPart(Of DigitalSignatureOriginPart)("rId4")

   ' Add the ExtendedFilePropertiesPart part in the new Word document.
   wordDoc.AddNewPart(Of ExtendedFilePropertiesPart)("rId5")

   ' Add the ThumbnailPart part in the new Word document.
   wordDoc.AddNewPart(Of ThumbnailPart)("image/jpeg", "rId6")

   wordDoc.Close()

End Sub

// How to add a new document part that receives a relationship ID to a package.
public void AddNewPart(string newFilePath)
{
   // Create a new Word document.
   WordprocessingDocument wordDoc =
      WordprocessingDocument.Create(newFilePath,
      WordprocessingDocumentType.Document);

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

   // Add the CustomFilePropertiesPart part in the new Word document.
   wordDoc.AddNewPart<CustomFilePropertiesPart>("rId2");

   // Add the CoreFilePropertiesPart part in the new Word document.
   wordDoc.AddNewPart<CoreFilePropertiesPart>("rId3");

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

   // Add the ExtendedFilePropertiesPart part in the new Word document.
   wordDoc.AddNewPart<ExtendedFilePropertiesPart>("rId5");

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

   wordDoc.Close();
}

To add a new document part containing custom XML

  1. First, pass in a parameter representing the path of the source Word 2007 document.

  2. Then, you open the document as a WordprocessingDocument object.

  3. Next, you add a MainDocumentPart part, a CustomFilePropertiesPart part, a CoreFilePropertiesPart part, a DigitalSignatureOriginPart part, a ExtendedFilePropertiesPart part, and a ThumbailPart part. Note how you pass a relationship Id to each package part.

  4. Finally, you close the document.

    NoteNote

    The AddNewPart<T> method creates a relationship sourcing from the current document part and targets the newly created document part. This method returns the newly created document part. Additionally, you could use the FeedData() method to fill the document part.