本主题演示如何使用 Open XML SDK for Office 中的类 (文件) 添加文档部件,该部件接收字处理文档的关系 Id 参数。
包和文档部件
Open XML 文档存储为包,其格式由 ISO/IEC 29500 定义。 包可以具有多个彼此之间存在关系的部件。 部件之间的关系控制文档的类别。 如果文档的包关系项包含与主文档部件的关系,可将文档定义为字处理文档。 如果文档的包关系项包含与演示文稿部件的关系,可将文档定义为演示文稿文档。 如果文档的包关系项包含与工作簿部件的关系,可将文档定义为电子表格文档。 在本操作方法主题中,您将使用字处理文档包。
WordProcessingML 文档的结构
文档的基本文档结构WordProcessingML由 和 body 元素组成document,后跟一个或多个块级元素(例如 p表示段落)。 段落包含一个或多个 r 元素。 
              r代表 run,它是具有一组通用属性(如格式设置)的文本区域。 运行包含一个或多个 t 元素。 元素 t 包含文本范围。 下面的代码示例演示 WordprocessingML 包含文本“示例文本”的文档的标记。
    <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>
使用 Open XML SDK,可以使用与元素对应的 WordprocessingML 强类型类创建文档结构和内容。 可以在 命名空间中找到 DocumentFormat.OpenXml.Wordprocessing 这些类。 下表列出了对应于 、、body、 rp和 t 元素的类的document类名。
| WordprocessingML 元素 | Open XML SDK 类 | 说明 | 
|---|---|---|
<document/> | 
Document | 主文档部件的根元素。 | 
<body/> | 
Body | 块级结构(如段落、表格、批注和 ISO/IEC 29500 规范中指定的其他项)的容器。 | 
<p/> | 
Paragraph | 段落。 | 
<r/> | 
Run | 一段连续文本。 | 
<t/> | 
Text | 文本范围。 | 
有关 WordprocessingML 文档的各个部分和元素的整体结构的详细信息,请参阅 WordprocessingML 文档的结构。
示例代码的工作方式
本作方法中的示例代码首先传入表示Word文档路径的参数。 然后,它会在 using 语句中创建一个新的 WordprocessingDocument 对象。
static void AddNewPart(string document)
{
    // Create a new word processing document.
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
然后,它将 MainDocumentPart 部件添加到新的字处理文档中,其关系 ID 为 rId1。 它还会在 CustomFilePropertiesPart 新的字处理文档中添加 部件和 CoreFilePropertiesPart 。
// Add the MainDocumentPart part in the new word processing document.
MainDocumentPart mainDocPart = wordDoc.AddMainDocumentPart();
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" standalone="yes"?>
        <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" />
        """);
    writer.Flush();
}
然后,该代码使用 realtionship ID rId4、ExtendedFilePropertiesPartrId5 和 ThumbnailPart rId6 在新字处理文档中添加DigitalSignatureOriginPart部件、部件和 部件。
注意
方法 AddNewPart 创建从当前文档部件到新文档部件的关系。 此方法会返回新文档部件。 此外,可以使用 <DocumentFormat.OpenXml.Packaging.DataPart.FeedData*> 方法填充文档部件。
示例代码
下面的代码添加一个包含外部文件中的自定义 XML 的新文档部件,然后填充该文档部件。 下面是 C# 和 Visual Basic 中的完整代码示例。
static void AddNewPart(string document)
{
    // Create a new word processing document.
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
    {
        // Add the MainDocumentPart part in the new word processing document.
        MainDocumentPart mainDocPart = wordDoc.AddMainDocumentPart();
        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" standalone="yes"?>
                <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" />
                """);
            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");
    }
}