Bewerken

Share via


Package.CreatePart Method

Definition

Creates a new package part.

Overloads

CreatePart(Uri, String)

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

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.

// 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://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

CreatePart(Uri, String)

Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs

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

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

contentType
String

The content type of the data stream.

Returns

The new created part.

Exceptions

partUri or contentType is null.

partUri is not a valid PackagePart URI.

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

The package is not open (Dispose(Boolean) or Close() has been called).

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.

// 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://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

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

Applies to

CreatePart(Uri, String, CompressionOption)

Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs
Source:
Package.cs

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
Public Function CreatePart (partUri As Uri, contentType As String, compressionOption As CompressionOption) As PackagePart

Parameters

partUri
Uri

The URI of the new part.

contentType
String

The content type of the data stream.

compressionOption
CompressionOption

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

Returns

The new created part.

Exceptions

partUri or contentType is null.

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

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

The compressionOption value is not valid.

The package is not open (Dispose(Boolean) or Close() has been called).

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.

// 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://www.ecma-international.org/publications-and-standards/standards/ecma-376/.

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