Export (0) Print
Expand All

PackageStore.GetPackage Method

Returns the Package with a specified URI from the store.

Namespace:  System.IO.Packaging
Assembly:  PresentationCore (in PresentationCore.dll)

public static Package GetPackage(
	Uri uri
)

Parameters

uri
Type: System.Uri

The uniform resource identifier (URI) of the package to return.

Return Value

Type: System.IO.Packaging.Package
The package with a specified packageUri; or null, if a package with the specified packageUri is not in the store.

ExceptionCondition
ArgumentNullException

packageUri is null.

ArgumentException

packageUri is an invalid package URI.

packageUri is matched to the URIs specified in the AddPackage method call.

Security Note   This method demands EnvironmentPermission for all custom Package (non-ZipPackage) types.

The following example shows how to use the GetPackage method.

// --------------------------- OpenDocument --------------------------- 
/// <summary> 
///   Loads and displays a given XPS document file.</summary> 
/// <param name="filename">
///   The path and file name of the XPS 
///   document to load and display.</param> 
/// <returns> 
///   true if the document loads successfully; otherwise false.</returns> 
public bool OpenDocument(string xpsFile)
{
    // Check to see if the document is encrypted. 
    // If encrypted, use OpenEncryptedDocument(). 
    if (EncryptedPackageEnvelope.IsEncryptedPackageEnvelope(xpsFile))
        return OpenEncryptedDocument(xpsFile);

    // Document is not encrypted, open normally.
    ShowStatus("Opening '" + Filename(xpsFile) + "'");

    _packageUri = new Uri(xpsFile, UriKind.Absolute);
    try
    {
        _xpsDocument = new XpsDocument(xpsFile, FileAccess.Read);
    }
    catch (System.IO.FileFormatException ex)
    {
        MessageBox.Show(xpsFile + "\n\nThe file " +
            "is not a valid XPS document.\n\n" +
            "Exception: " + ex.Message + "\n\n" +
            ex.GetType().ToString() + "\n\n" + ex.StackTrace,
            "Invalid File Format",
            MessageBoxButton.OK, MessageBoxImage.Error);
        return false;
    }


    // Get the document's PackageStore into which 
    // new user annotations will be added and saved.
    _xpsPackage = PackageStore.GetPackage(_packageUri);
    if ((_xpsPackage == null) || (_xpsDocument == null))
    {
        MessageBox.Show("Unable to get Package from file.");
        return false;
    }

    // Get the FixedDocumentSequence from the open document.
    FixedDocumentSequence fds = _xpsDocument.GetFixedDocumentSequence();
    if (fds == null)
    {
        MessageBox.Show(xpsFile + "\n\nThe document package within " +
            "the specified file does not contain a " +
            "FixedDocumentSequence.", "Package Error");
        return false;
    }

    // Load the FixedDocumentSequence to the DocumentViewer control.
    DocViewer.Document = fds;

    // Enable document menu controls.
    menuFileClose.IsEnabled  = true;
    menuFilePrint.IsEnabled  = true;
    menuViewIncreaseZoom.IsEnabled = true;
    menuViewDecreaseZoom.IsEnabled = true;

    // Give the DocumentViewer focus.
    docViewer.Focus();

    this.Title = "RightsManagedPackageViewer SDK Sample - " +
                 Filename(xpsFile);
    return true;
}// end:OpenDocument()

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft