com.aspose.words

Class CustomPart

  • java.lang.Object
    • com.aspose.words.CustomPart
  • All Implemented Interfaces:
    java.lang.Cloneable
    public class CustomPart 
    extends java.lang.Object

Represents a custom (arbitrary content) part, that is not defined by the ISO/IEC 29500 standard.

This class represents an OOXML part that is a target of an "unknown relationship". All relationships not defined within ISO/IEC 29500 are considered "unknown relationships". Unknown relationships are permitted within an Office Open XML document provided that they conform to relationship markup guidelines.

Microsoft Word preserves custom parts during open/save cycles. Some additional info can be found here http://blogs.msdn.com/dmahugh/archive/2006/11/25/arbitrary-content-in-an-opc-package.aspx

Aspose.Words also roundtrips custom parts and in addition, allows to programmatically access such parts via the CustomPart and CustomPartCollection objects.

Do not confuse custom parts with Custom XML Data. Use CustomXmlPart if you need to access Custom XML Data.

Example:

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(getMyDir() + "Document.PackageCustomParts.docx");
Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);

// Clone the second part
CustomPart clonedPart = doc.getPackageCustomParts().get(1).deepClone();

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

Assert.assertEquals(doc.getPackageCustomParts().getCount(), 3);

// Use an enumerator to print information about the contents of each part
Iterator<CustomPart> enumerator = doc.getPackageCustomParts().iterator();

int index = 0;
while (enumerator.hasNext()) {
    CustomPart customPart = (CustomPart) enumerator.next();
    System.out.println(MessageFormat.format("Part index {0}:", index));
    System.out.println(MessageFormat.format("\tName: {0}", customPart.getName()));
    System.out.println(MessageFormat.format("\tContentType: {0}", customPart.getContentType()));
    System.out.println(MessageFormat.format("\tRelationshipType: {0}", customPart.getRelationshipType()));
    if (customPart.isExternal()) {
        System.out.println("\tSourced from outside the document");
    } else {
        System.out.println(MessageFormat.format("\tSourced from within the document, length: {0} bytes", customPart.getData().length));
    }
    index++;
}


// Delete parts one at a time based on index
doc.getPackageCustomParts().removeAt(2);
Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);

// Delete all parts
doc.getPackageCustomParts().clear();
Assert.assertEquals(doc.getPackageCustomParts().getCount(), 0);
See Also:
CustomPartCollection, Document.PackageCustomParts

Constructor Summary
 
Property Getters/Setters Summary
java.lang.StringgetContentType()
void
setContentType(java.lang.Stringvalue)
           Specifies the content type of this custom part.
byte[]getData()
void
setData(byte[]value)
           Contains the data of this custom part.
booleanisExternal()
void
isExternal(booleanvalue)
          False if this custom part is stored inside the OOXML package. True if this custom part is an external target.
java.lang.StringgetName()
void
setName(java.lang.Stringvalue)
           Gets or sets this part's absolute name within the OOXML package or the target URL.
java.lang.StringgetRelationshipType()
void
setRelationshipType(java.lang.Stringvalue)
           Gets or sets the relationship type from the parent part to this custom part.
 
Method Summary
CustomPartdeepClone()
Makes a "deep enough" copy of the object. Does not duplicate the bytes of the Data value.
 

    • Constructor Detail

      • CustomPart

        public CustomPart()
    • Property Getters/Setters Detail

      • getContentType/setContentType

        public java.lang.String getContentType() / public void setContentType(java.lang.String value)
        
        Specifies the content type of this custom part.

        This property is applicable only when IsExternal is false.

        The default value is an empty string. A valid value must be a non-empty string.

        Example:

        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(getMyDir() + "Document.PackageCustomParts.docx");
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Clone the second part
        CustomPart clonedPart = doc.getPackageCustomParts().get(1).deepClone();
        
        // Add the clone to the collection
        doc.getPackageCustomParts().add(clonedPart);
        
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 3);
        
        // Use an enumerator to print information about the contents of each part
        Iterator<CustomPart> enumerator = doc.getPackageCustomParts().iterator();
        
        int index = 0;
        while (enumerator.hasNext()) {
            CustomPart customPart = (CustomPart) enumerator.next();
            System.out.println(MessageFormat.format("Part index {0}:", index));
            System.out.println(MessageFormat.format("\tName: {0}", customPart.getName()));
            System.out.println(MessageFormat.format("\tContentType: {0}", customPart.getContentType()));
            System.out.println(MessageFormat.format("\tRelationshipType: {0}", customPart.getRelationshipType()));
            if (customPart.isExternal()) {
                System.out.println("\tSourced from outside the document");
            } else {
                System.out.println(MessageFormat.format("\tSourced from within the document, length: {0} bytes", customPart.getData().length));
            }
            index++;
        }
        
        
        // Delete parts one at a time based on index
        doc.getPackageCustomParts().removeAt(2);
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Delete all parts
        doc.getPackageCustomParts().clear();
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 0);
      • getData/setData

        public byte[] getData() / public void setData(byte[] value)
        
        Contains the data of this custom part.

        This property is applicable only when IsExternal is false.

        The default value is an empty byte array. The value cannot be null.

        Example:

        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(getMyDir() + "Document.PackageCustomParts.docx");
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Clone the second part
        CustomPart clonedPart = doc.getPackageCustomParts().get(1).deepClone();
        
        // Add the clone to the collection
        doc.getPackageCustomParts().add(clonedPart);
        
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 3);
        
        // Use an enumerator to print information about the contents of each part
        Iterator<CustomPart> enumerator = doc.getPackageCustomParts().iterator();
        
        int index = 0;
        while (enumerator.hasNext()) {
            CustomPart customPart = (CustomPart) enumerator.next();
            System.out.println(MessageFormat.format("Part index {0}:", index));
            System.out.println(MessageFormat.format("\tName: {0}", customPart.getName()));
            System.out.println(MessageFormat.format("\tContentType: {0}", customPart.getContentType()));
            System.out.println(MessageFormat.format("\tRelationshipType: {0}", customPart.getRelationshipType()));
            if (customPart.isExternal()) {
                System.out.println("\tSourced from outside the document");
            } else {
                System.out.println(MessageFormat.format("\tSourced from within the document, length: {0} bytes", customPart.getData().length));
            }
            index++;
        }
        
        
        // Delete parts one at a time based on index
        doc.getPackageCustomParts().removeAt(2);
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Delete all parts
        doc.getPackageCustomParts().clear();
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 0);
      • isExternal/isExternal

        public boolean isExternal() / public void isExternal(boolean value)
        
        False if this custom part is stored inside the OOXML package. True if this custom part is an external target.

        The default value is false.

        Example:

        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(getMyDir() + "Document.PackageCustomParts.docx");
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Clone the second part
        CustomPart clonedPart = doc.getPackageCustomParts().get(1).deepClone();
        
        // Add the clone to the collection
        doc.getPackageCustomParts().add(clonedPart);
        
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 3);
        
        // Use an enumerator to print information about the contents of each part
        Iterator<CustomPart> enumerator = doc.getPackageCustomParts().iterator();
        
        int index = 0;
        while (enumerator.hasNext()) {
            CustomPart customPart = (CustomPart) enumerator.next();
            System.out.println(MessageFormat.format("Part index {0}:", index));
            System.out.println(MessageFormat.format("\tName: {0}", customPart.getName()));
            System.out.println(MessageFormat.format("\tContentType: {0}", customPart.getContentType()));
            System.out.println(MessageFormat.format("\tRelationshipType: {0}", customPart.getRelationshipType()));
            if (customPart.isExternal()) {
                System.out.println("\tSourced from outside the document");
            } else {
                System.out.println(MessageFormat.format("\tSourced from within the document, length: {0} bytes", customPart.getData().length));
            }
            index++;
        }
        
        
        // Delete parts one at a time based on index
        doc.getPackageCustomParts().removeAt(2);
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Delete all parts
        doc.getPackageCustomParts().clear();
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 0);
        See Also:
        Name
      • getName/setName

        public java.lang.String getName() / public void setName(java.lang.String value)
        
        Gets or sets this part's absolute name within the OOXML package or the target URL.

        If the relationship target is internal, then this property is the absolute part name within the package. If the relationship target is external, then this property is the target URL.

        The default value is an empty string. A valid value must be a non-empty string.

        Example:

        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(getMyDir() + "Document.PackageCustomParts.docx");
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Clone the second part
        CustomPart clonedPart = doc.getPackageCustomParts().get(1).deepClone();
        
        // Add the clone to the collection
        doc.getPackageCustomParts().add(clonedPart);
        
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 3);
        
        // Use an enumerator to print information about the contents of each part
        Iterator<CustomPart> enumerator = doc.getPackageCustomParts().iterator();
        
        int index = 0;
        while (enumerator.hasNext()) {
            CustomPart customPart = (CustomPart) enumerator.next();
            System.out.println(MessageFormat.format("Part index {0}:", index));
            System.out.println(MessageFormat.format("\tName: {0}", customPart.getName()));
            System.out.println(MessageFormat.format("\tContentType: {0}", customPart.getContentType()));
            System.out.println(MessageFormat.format("\tRelationshipType: {0}", customPart.getRelationshipType()));
            if (customPart.isExternal()) {
                System.out.println("\tSourced from outside the document");
            } else {
                System.out.println(MessageFormat.format("\tSourced from within the document, length: {0} bytes", customPart.getData().length));
            }
            index++;
        }
        
        
        // Delete parts one at a time based on index
        doc.getPackageCustomParts().removeAt(2);
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Delete all parts
        doc.getPackageCustomParts().clear();
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 0);
        See Also:
        IsExternal
      • getRelationshipType/setRelationshipType

        public java.lang.String getRelationshipType() / public void setRelationshipType(java.lang.String value)
        
        Gets or sets the relationship type from the parent part to this custom part.

        The relationship type for a custom part must be "unknown" e.g. a custom relationship type, not one of the relationship types defined within ISO/IEC 29500.

        The default value is an empty string. A valid value must be a non-empty string.

        Example:

        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(getMyDir() + "Document.PackageCustomParts.docx");
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Clone the second part
        CustomPart clonedPart = doc.getPackageCustomParts().get(1).deepClone();
        
        // Add the clone to the collection
        doc.getPackageCustomParts().add(clonedPart);
        
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 3);
        
        // Use an enumerator to print information about the contents of each part
        Iterator<CustomPart> enumerator = doc.getPackageCustomParts().iterator();
        
        int index = 0;
        while (enumerator.hasNext()) {
            CustomPart customPart = (CustomPart) enumerator.next();
            System.out.println(MessageFormat.format("Part index {0}:", index));
            System.out.println(MessageFormat.format("\tName: {0}", customPart.getName()));
            System.out.println(MessageFormat.format("\tContentType: {0}", customPart.getContentType()));
            System.out.println(MessageFormat.format("\tRelationshipType: {0}", customPart.getRelationshipType()));
            if (customPart.isExternal()) {
                System.out.println("\tSourced from outside the document");
            } else {
                System.out.println(MessageFormat.format("\tSourced from within the document, length: {0} bytes", customPart.getData().length));
            }
            index++;
        }
        
        
        // Delete parts one at a time based on index
        doc.getPackageCustomParts().removeAt(2);
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Delete all parts
        doc.getPackageCustomParts().clear();
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 0);
    • Method Detail

      • deepClone

        public CustomPart deepClone()
        Makes a "deep enough" copy of the object. Does not duplicate the bytes of the Data value.

        Example:

        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(getMyDir() + "Document.PackageCustomParts.docx");
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Clone the second part
        CustomPart clonedPart = doc.getPackageCustomParts().get(1).deepClone();
        
        // Add the clone to the collection
        doc.getPackageCustomParts().add(clonedPart);
        
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 3);
        
        // Use an enumerator to print information about the contents of each part
        Iterator<CustomPart> enumerator = doc.getPackageCustomParts().iterator();
        
        int index = 0;
        while (enumerator.hasNext()) {
            CustomPart customPart = (CustomPart) enumerator.next();
            System.out.println(MessageFormat.format("Part index {0}:", index));
            System.out.println(MessageFormat.format("\tName: {0}", customPart.getName()));
            System.out.println(MessageFormat.format("\tContentType: {0}", customPart.getContentType()));
            System.out.println(MessageFormat.format("\tRelationshipType: {0}", customPart.getRelationshipType()));
            if (customPart.isExternal()) {
                System.out.println("\tSourced from outside the document");
            } else {
                System.out.println(MessageFormat.format("\tSourced from within the document, length: {0} bytes", customPart.getData().length));
            }
            index++;
        }
        
        
        // Delete parts one at a time based on index
        doc.getPackageCustomParts().removeAt(2);
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 2);
        
        // Delete all parts
        doc.getPackageCustomParts().clear();
        Assert.assertEquals(doc.getPackageCustomParts().getCount(), 0);