CustomPartCollectionAdd Method

Adds an item to the collection.

Namespace:  Aspose.Words.Markup
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public void Add(
	CustomPart part
)

Parameters

part
Type: Aspose.Words.MarkupCustomPart
The item to add.
Examples
Shows how to open a document with custom parts and access them.
// Open a document that contains custom parts
// CustomParts are arbitrary content OOXML parts
// Not to be confused with Custom XML data which is represented by CustomXmlParts
// This part is internal, meaning it is contained inside the OOXML package
Document doc = new Document(MyDir + "Custom parts OOXML package.docx");
Assert.AreEqual(2, doc.PackageCustomParts.Count);

// Clone the second part
CustomPart clonedPart = doc.PackageCustomParts[1].Clone();

// Add the clone to the collection
doc.PackageCustomParts.Add(clonedPart);

Assert.AreEqual(3, doc.PackageCustomParts.Count);

// Use an enumerator to print information about the contents of each part 
using (IEnumerator<CustomPart> enumerator = doc.PackageCustomParts.GetEnumerator())
{
    int index = 0;
    while (enumerator.MoveNext())
    {
        Console.WriteLine($"Part index {index}:");
        Console.WriteLine($"\tName: {enumerator.Current.Name}");
        Console.WriteLine($"\tContentType: {enumerator.Current.ContentType}");
        Console.WriteLine($"\tRelationshipType: {enumerator.Current.RelationshipType}");
        Console.WriteLine(enumerator.Current.IsExternal
            ? "\tSourced from outside the document"
            : $"\tSourced from within the document, length: {enumerator.Current.Data.Length} bytes");
        index++;
    }
}

// Delete parts one at a time based on index
doc.PackageCustomParts.RemoveAt(2);
Assert.AreEqual(2, doc.PackageCustomParts.Count);

// Delete all parts
doc.PackageCustomParts.Clear();
Assert.AreEqual(0, doc.PackageCustomParts.Count);
See Also