Package.CreatePart Package.CreatePart Package.CreatePart Package.CreatePart Method

Definition

Creates a new package part.

Overloads

CreatePart(Uri, String) CreatePart(Uri, String) CreatePart(Uri, String) CreatePart(Uri, String)

Creates a new uncompressed part with a given URI and content type.

CreatePart(Uri, String, CompressionOption) CreatePart(Uri, String, CompressionOption) CreatePart(Uri, String, CompressionOption)

Creates a new part with a given URI, content type, and compression option.

Examples

The following example shows how to create a new PackagePart and then store data into the part. For the complete sample, see Writing a Package Sample.

// Add the Document part to the Package
PackagePart packagePartDocument =
    package.CreatePart(partUriDocument,
                   System.Net.Mime.MediaTypeNames.Text.Xml);

// Copy the data to the Document Part
using (FileStream fileStream = new FileStream(
       documentPath, FileMode.Open, FileAccess.Read))
{
    CopyStream(fileStream, packagePartDocument.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)

' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
    CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.

Remarks

CreatePart initializes an empty Stream for the new part. The PackagePart.GetStream method can be used to obtain a reference to the stream instance associated with the part.

For more information about package parts, see section 1.1 of the Open Packaging Conventions (OPC) specification available for download at https://go.microsoft.com/fwlink/?LinkID=71255.

CreatePart(Uri, String) CreatePart(Uri, String) CreatePart(Uri, String) CreatePart(Uri, String)

Creates a new uncompressed part with a given URI and content type.

public:
 System::IO::Packaging::PackagePart ^ CreatePart(Uri ^ partUri, System::String ^ contentType);
public System.IO.Packaging.PackagePart CreatePart (Uri partUri, string contentType);
member this.CreatePart : Uri * string -> System.IO.Packaging.PackagePart
Public Function CreatePart (partUri As Uri, contentType As String) As PackagePart

Parameters

partUri
Uri Uri Uri Uri

The uniform resource identifier (URI) of the new part.

contentType
String String String String

The content type of the data stream.

Returns

Exceptions

A part with the specified partUri is already present in the package.

The package is read-only (a new part cannot be added).

Examples

The following example shows how to create a new PackagePart and then store data into the part. For the complete sample, see Writing a Package Sample.

// Add the Document part to the Package
PackagePart packagePartDocument =
    package.CreatePart(partUriDocument,
                   System.Net.Mime.MediaTypeNames.Text.Xml);

// Copy the data to the Document Part
using (FileStream fileStream = new FileStream(
       documentPath, FileMode.Open, FileAccess.Read))
{
    CopyStream(fileStream, packagePartDocument.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)

' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
    CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.

Remarks

CreatePart initializes an empty Stream for the new part. The PackagePart.GetStream method can be used to obtain a reference to the stream instance associated with the part.

The part CompressionOption is CompressionOption.NotCompressed.

For additional information about package parts, see section 1.1 in the Open Packaging Conventions (OPC) specification available for download at https://go.microsoft.com/fwlink/?LinkID=71255.

Notes to Inheritors

CreatePart(Uri, String) internally calls the derived class's CreatePartCore(Uri, String, CompressionOption) method to actually create the part based on the physical format implemented in the derived class.

See also

CreatePart(Uri, String, CompressionOption) CreatePart(Uri, String, CompressionOption) CreatePart(Uri, String, CompressionOption)

Creates a new part with a given URI, content type, and compression option.

public:
 System::IO::Packaging::PackagePart ^ CreatePart(Uri ^ partUri, System::String ^ contentType, System::IO::Packaging::CompressionOption compressionOption);
public System.IO.Packaging.PackagePart CreatePart (Uri partUri, string contentType, System.IO.Packaging.CompressionOption compressionOption);
member this.CreatePart : Uri * string * System.IO.Packaging.CompressionOption -> System.IO.Packaging.PackagePart

Parameters

partUri
Uri Uri Uri Uri

The URI of the new part.

contentType
String String String String

The content type of the data stream.

compressionOption
CompressionOption CompressionOption CompressionOption CompressionOption

The compression option for the data stream, NotCompressed or Normal compression.

Returns

Exceptions

partUri is not a valid PackagePart uniform resource identifier (URI).

A part with the specified partUri is already present in the package.

The package is read-only (a new part cannot be added).

Examples

The following example shows how to create a new PackagePart and then store data into the part. For the complete sample, see Writing a Package Sample.

// Add the Document part to the Package
PackagePart packagePartDocument =
    package.CreatePart(partUriDocument,
                   System.Net.Mime.MediaTypeNames.Text.Xml);

// Copy the data to the Document Part
using (FileStream fileStream = new FileStream(
       documentPath, FileMode.Open, FileAccess.Read))
{
    CopyStream(fileStream, packagePartDocument.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)

' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
    CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.

Remarks

For the default ZipPackage derived class, the CreatePart method only supports two compressionOption values, NotCompressed or Normal compression. Other CompressionOption values of Maximum, Fast, or SuperFast use Normal compression.

CreatePart creates an empty Stream for the new part. The PackagePart.GetStream method can be used to obtain a reference to the stream instance associated with the part.

For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://go.microsoft.com/fwlink/?LinkID=71255.

Notes to Inheritors

CreatePart(Uri, String, CompressionOption) internally calls the derived class's CreatePartCore(Uri, String, CompressionOption) method to actually create the part based on the physical format implemented in the derived class.

See also

Applies to