AltChunk Class

Anchor for Imported External Content

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.OpenXmlElement
    DocumentFormat.OpenXml.OpenXmlCompositeElement
      DocumentFormat.OpenXml.Wordprocessing.AltChunk

Namespace:  DocumentFormat.OpenXml.Wordprocessing
Assembly:  DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)

Syntax

声明
<ChildElementInfoAttribute(GetType(AltChunkProperties))> _
Public Class AltChunk _
    Inherits OpenXmlCompositeElement
用法
Dim instance As AltChunk
[ChildElementInfoAttribute(typeof(AltChunkProperties))]
public class AltChunk : OpenXmlCompositeElement

Remarks

The following table lists the possible child types:

  • AltChunkProperties <w:altChunkPr>

[ISO/IEC 29500-1 第 1 个版本]

17.17.2.1 altChunk (Anchor for Imported External Content)

This element specifies a location within a document for the insertion of the contents of a specified file containing external content to be imported into the main WordprocessingML document. The specified file's contents should appear at the specified location within the document, and can henceforth be emitted as regular WordprocessingML without distinction to its origin. The location of the external content to be imported shall be specified by the relationship whose Id attribute matches the id attribute on this element.

If the relationship type of the relationship specified by this element is not https://schemas.openxmlformats.org/officeDocument/2006/afChunk, is not present, or does not have a TargetMode attribute value of Internal, then the document shall be considered non-conformant. If an application cannot process external content of the content type specified by the targeted part, then it should ignore the specified alternate content but continue to process the file. If possible, it should also provide some indication that unknown content was not imported.

[Example: Consider a WordprocessingML document consisting of contents which must be imported from the following HTML document:

<html … >
<body style="margin-left:200px;margin-top:50px">
<p>Paragraph one.</p>
<blockquote style="border:5px solid #00FFFF">Paragraph in a blockquote.</blockquote>
<p>Paragraph two.</p>
</body>
</html>

The resulting WordprocessingML host document would consist of its own WordprocessingML content as well as an external content import anchor in the appropriate location:

<w:body>
<w:altChunk r:id="altChunk1" />
<w:p/>
<w:sectPr>
…
</w:sectPr>
</w:body>

The altChunk element specifies that the external content targeted by the relationship with an ID of altChunk1 must be imported at the beginning of the document. Examining the contents of the corresponding relationship part item, we can see the targets for that relationship:

<Relationships … >
…
<Relationship Id="altChunk1" TargetMode="Internal" Type="https://schemas.openxmlformats.org/officeDocument/2006/relationships/afChunk" Target="import.htm" />
…
</Relationships>

The corresponding relationship part item shows that the file to be imported is located next to the main document and is named import.htm. end example]

Parent Elements

body (§17.2.2); comment (§17.13.4.2); docPartBody (§17.12.6); endnote (§17.11.2); footnote (§17.11.10); ftr (§17.10.3); hdr (§17.10.4); tc (§17.4.66)

Child Elements

Subclause

altChunkPr (External Content Import Properties)

§17.17.2.2

Attributes

Description

id (Relationship to Part)

Namespace: .../officeDocument/2006/relationships

Specifies the relationship ID to a specified part containing alternate content for import.

If the specified relationship does not match the relationship type required by the parent element, then this document shall be considered to be non-conformant.

[Example: Consider an XML element which has the following id attribute:

<… r:id="rId10" />

The markup specifies the associated relationship part with relationship ID rId1 contains the corresponding relationship information for the parent XML element. end example]

The possible values for this attribute are defined by the ST_RelationshipId simple type (§22.8.2.1).

[Note: The W3C XML Schema definition of this element's content model (CT_AltChunk) is located in §A.1. end note]

© ISO/IEC29500: 2008. 以上摘要已经由 Microsoft(和/或其代理公司)从英语翻译为简体中文,ISO 对此翻译不承担任何责任。

Examples

The following example shows how to merge two word-processing documents using altChunk. The example inserts the content of the file (source.docx) into another file (Destination.docx). After running the example, examine the contents the file "Destination.docx." You will notice that the contents of the source file are inserted right after the text that was originally in the file. Notice also that the code example starts by deleting the file "Destination.docx" and creates a fresh one. This is to avoid the ID conflict when you run the code more than once.

using System.Linq;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace altChunk
{
    class Program
    {
        static void Main(string[] args)
        {          
            string fileName1 = @"c:\Users\Public\Documents\Destination.docx";
            string fileName2 = @"c:\Users\Public\Documents\Source.docx";
            string testFile = @"c:\Users\Public\Documents\Test.docx";
            File.Delete(fileName1);
            File.Copy(testFile, fileName1);
            using (WordprocessingDocument myDoc =
                WordprocessingDocument.Open(fileName1, true))
            {
                string altChunkId = "AltChunkId1";
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                AlternativeFormatImportPart chunk = 
                    mainPart.AddAlternativeFormatImportPart(
                    AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                using (FileStream fileStream = File.Open(fileName2, FileMode.Open))
                    chunk.FeedData(fileStream);
                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;
                mainPart.Document
                    .Body
                    .InsertAfter(altChunk, mainPart.Document.Body
                    .Elements<Paragraph>().Last());
                mainPart.Document.Save();
            }           
        }
    }
}
Imports System.Linq
Imports System.IO
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing

Module Module1
    Sub Main(ByVal args As String())
        Dim fileName1 As String = "c:\Users\Public\Documents\Destination.docx"
        Dim fileName2 As String = "c:\Users\Public\Documents\Source.docx"
        Dim testFile As String = "c:\Users\Public\Documents\Test.docx"
        File.Delete(fileName1)
        File.Copy(testFile, fileName1)
        Using myDoc As WordprocessingDocument = WordprocessingDocument.Open(fileName1, True)
            Dim altChunkId As String = "AltChunkId1"
            Dim mainPart As MainDocumentPart = myDoc.MainDocumentPart
            Dim chunk As AlternativeFormatImportPart = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId)
            Using fileStream As FileStream = File.Open(fileName2, FileMode.Open)
                chunk.FeedData(fileStream)
            End Using
            Dim altChunk As New AltChunk()
            altChunk.Id = altChunkId
            mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements(Of Paragraph)().Last())
            mainPart.Document.Save()
        End Using
    End Sub
End Module

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

AltChunk Members

DocumentFormat.OpenXml.Wordprocessing Namespace