ServerDocument.Document Propriedade

Definição

Obtém a matriz de bytes de um documento na memória, que é carregado no ServerDocument.

public byte[] Document { get; }

Valor da propriedade

Byte[]

A matriz de bytes de um documento na memória, que é carregado no ServerDocument.

Exceções

O documento foi fechado.

Exemplos

O exemplo de código a seguir usa o ServerDocument(Byte[], String) construtor para criar um novo a ServerDocument partir de uma matriz de bytes que contém uma pasta de trabalho do Excel com a extensão de nome de arquivo. xlsx. Em seguida, o exemplo usa a Document propriedade para exibir o número de bytes no documento.

Este exemplo requer:

  • Um projeto de aplicativo do console ou qualquer outro projeto que não seja do Office.

  • Referências para os seguintes assemblies:

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll e Microsoft.VisualStudio.Tools.Applications.Runtime.dll (caso o projeto segmente o .NET Framework 4 ou o .NET Framework 4.5).

      ou

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll e Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll (caso o projeto segmente o .NET Framework 3.5).

  • Instruções Imports (para Visual Basic) ou using (para C#) para namespaces Microsoft.VisualStudio.Tools.Applications e Microsoft.VisualStudio.Tools.Applications.Runtime na parte superior do arquivo de código.

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();
    }
}
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

Comentários

Essa propriedade retornará uma matriz de bytes preenchida se o ServerDocument tiver sido criado usando o ServerDocument(Byte[], String) Construtor que tem um parâmetro de matriz de bytes ou o ServerDocument(Stream, String) Construtor que tem um Stream parâmetro. Caso contrário, essa propriedade retornará uma matriz de bytes vazia.

O uso dessa propriedade permite que você faça alterações em um documento e envie-o a um cliente sem gravar o documento em disco.

Aplica-se a