次の方法で共有


PackageStore.GetPackage(Uri) メソッド

定義

指定した URI を持つ Package をストアから返します。

public:
 static System::IO::Packaging::Package ^ GetPackage(Uri ^ uri);
public static System.IO.Packaging.Package GetPackage (Uri uri);
static member GetPackage : Uri -> System.IO.Packaging.Package
Public Shared Function GetPackage (uri As Uri) As Package

パラメーター

uri
Uri

返されるパッケージの Uniform Resource Identifier (URI)。

戻り値

指定した packageUri を持つパッケージ。指定した packageUri を持つパッケージがストア内に存在しない場合は null

例外

packageUrinullです。

packageUri が無効なパッケージ URI です。

GetPackage メソッドを使用する方法の例を次に示します。

// --------------------------- 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()
' --------------------------- 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 Function OpenDocument(ByVal xpsFile As String) As Boolean
    ' Check to see if the document is encrypted.
    ' If encrypted, use OpenEncryptedDocument().
    If EncryptedPackageEnvelope.IsEncryptedPackageEnvelope(xpsFile) Then
        Return OpenEncryptedDocument(xpsFile)
    End If

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

    _packageUri = New Uri(xpsFile, UriKind.Absolute)
    Try
        _xpsDocument = New XpsDocument(xpsFile, FileAccess.Read)
    Catch ex As System.IO.FileFormatException
        MessageBox.Show(xpsFile & vbLf & vbLf & "The file " & "is not a valid XPS document." & vbLf & vbLf & "Exception: " & ex.Message & vbLf & vbLf & ex.GetType().ToString() & vbLf & vbLf & ex.StackTrace, "Invalid File Format", MessageBoxButton.OK, MessageBoxImage.Error)
        Return False
    End Try


    ' Get the document's PackageStore into which
    ' new user annotations will be added and saved.
    _xpsPackage = PackageStore.GetPackage(_packageUri)
    If (_xpsPackage Is Nothing) OrElse (_xpsDocument Is Nothing) Then
        MessageBox.Show("Unable to get Package from file.")
        Return False
    End If

    ' Get the FixedDocumentSequence from the open document.
    Dim fds As FixedDocumentSequence = _xpsDocument.GetFixedDocumentSequence()
    If fds Is Nothing Then
        MessageBox.Show(xpsFile & vbLf & vbLf & "The document package within " & "the specified file does not contain a " & "FixedDocumentSequence.", "Package Error")
        Return False
    End If

    ' Load the FixedDocumentSequence to the DocumentViewer control.
    DocViewerProperty.Document = fds

    ' Enable document menu controls.
    menuFileClose.IsEnabled = True
    menuFilePrint.IsEnabled = True
    menuViewIncreaseZoom.IsEnabled = True
    menuViewDecreaseZoom.IsEnabled = True

    ' Give the DocumentViewer focus.
    docViewer.Focus()

    Me.Title = "RightsManagedPackageViewer SDK Sample - " & Filename(xpsFile)
    Return True
End Function ' end:OpenDocument()

注釈

packageUri は、メソッド呼び出しで AddPackage 指定された URI と一致します。

セキュリティに関する注意 このメソッドでは、 EnvironmentPermission すべてのカスタム Package (非ZipPackage) 型が必要です。

適用対象

こちらもご覧ください