ServerDocument.Document (Propiedad)

Obtiene la matriz de bytes de un documento almacenado en memoria que se carga en ServerDocument.

Espacio de nombres:  Microsoft.VisualStudio.Tools.Applications
Ensamblado:  Microsoft.VisualStudio.Tools.Applications.ServerDocument (en Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll)

Sintaxis

'Declaración
Public ReadOnly Property Document As Byte()
    Get
public byte[] Document { get; }

Valor de propiedad

Tipo: array<System.Byte[]
La matriz de bytes de un documento almacenado en memoria que se carga en ServerDocument.

Excepciones

Excepción Condición
DocumentClosedException

El documento se ha cerrado.

Comentarios

Esta propiedad devuelve una matriz de bytes llena si ServerDocument se creó utilizando el constructor ServerDocument(array<Byte[], String) que tiene un parámetro de matriz de bytes o el constructor ServerDocument(Stream, String) que tiene un parámetro Stream. De lo contrario, esta propiedad devuelve una matriz de bytes vacía.

La utilización de esta propiedad le permite realizar cambios en un documento y enviárselo a un cliente sin escribir el documento en el disco.

Ejemplos

En el ejemplo de código siguiente se utiliza el constructor ServerDocument(array<Byte[], String) para crear un nuevo objeto ServerDocument a partir de una matriz de bytes que contiene un libro de Excel con la extensión de nombre de archivo .xlsx. En el ejemplo se utiliza la propiedad Document para mostrar el número de bytes del documento.

Este ejemplo necesita:

  • Un proyecto de aplicación de consola o algún otro proyecto que no es de Office.

  • Referencias a los siguientes ensamblados:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll y Microsoft.VisualStudio.Tools.Applications.Runtime.dll (si el destino del proyecto es .NET Framework 4).

      O bien

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll and Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (si el proyecto está dirigido a .NET Framework 3.5).

  • Instrucciones Imports (para Visual Basic) o using (para C#) para los espacios de nombres Microsoft.VisualStudio.Tools.Applications y Microsoft.VisualStudio.Tools.Applications.Runtime en la parte superior de su archivo de código.

Private Sub CreateServerDocumentFromByteArray(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing
    Dim stream As System.IO.FileStream = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            ' Read the file into a byte array.
            stream = New System.IO.FileStream(documentPath, System.IO.FileMode.Open, _
                System.IO.FileAccess.Read)
            Dim buffer(Fix(stream.Length)) As Byte
            stream.Read(buffer, 0, Fix(buffer.Length))

            ' Display the number of bytes in the document.
            serverDocument1 = New ServerDocument(buffer, "*.xlsx")
            MessageBox.Show("The Document property contains " & _
                serverDocument1.Document.Length.ToString() & " bytes.")
        End If

    Catch ex As System.IO.FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As UnknownCustomizationFileException
        System.Windows.Forms.MessageBox.Show("The specified document has a file " & _
            "extension that is not supported by Visual Studio Tools for Office.")
    Finally
        If Not (serverDocument1 Is Nothing) Then
            serverDocument1.Close()
        End If
        If Not (stream Is Nothing) Then
            stream.Close()
        End If
    End Try
End Sub
private void CreateServerDocumentFromByteArray(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;
    System.IO.FileStream stream = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            // Read the file into a byte array.
            stream = new System.IO.FileStream(
                documentPath, System.IO.FileMode.Open,
                System.IO.FileAccess.Read);
            byte[] buffer = new byte[(int)stream.Length];
            stream.Read(buffer, 0, (int)buffer.Length);

            // Display the number of bytes in the document.
            serverDocument1 = new ServerDocument(buffer,
                "*.xlsx");
            MessageBox.Show("The Document property contains " +
                serverDocument1.Document.Length.ToString() +
                " bytes.");
        }
    }
    catch (System.IO.FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (UnknownCustomizationFileException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document has a file " +
            "extension that is not supported by Visual Studio Tools for Office.");
    }
    finally
    {
        if (serverDocument1 != null)
            serverDocument1.Close();
        if (stream != null)
            stream.Close();
    }
}

Seguridad de .NET Framework

Vea también

Referencia

ServerDocument Clase

Microsoft.VisualStudio.Tools.Applications (Espacio de nombres)