VintaSoft Imaging .NET SDK 15.1: Документация для .NET разработчика
В этом разделе
Кодеки: Как работать с защищенным DOCX-файлом?
В этом разделе
VintaSoft Imaging .NET SDK позволяет открывать и просматривать защищенный DOCX-документ. Открытый защищенный DOCX-документ нельзя редактировать, но его можно сохранить в новый не защищенный DOCX-документ.


Вот C#/VB.NET код, который демонстрирует, как преобразовать защищенный DOCX-файл в многостраничный TIFF-файл:
/// <summary>
/// Converts encrypted DOCX document to a TIFF file using ImageCollection and TiffEncoder classes.
/// DOCX document is rendered with specified resolution.
/// </summary>
public static void ConvertEncryptedDocxToTiff(string docxFileName, string password, string tiffFileName, float dpi)
{
    System.IO.Stream docxFileStream = System.IO.File.OpenRead(docxFileName);

    // if document is encrypted
    if (Vintasoft.Imaging.Office.OfficeDocumentCryptography.IsSecuredOfficeDocument(docxFileStream))
    {
        // create memory stream
        System.IO.MemoryStream docxDectyptedFileStream = new System.IO.MemoryStream();
        // try to decrypt DOCX document and save not encrypted DOCX document to a stream
        bool result = Vintasoft.Imaging.Office.OfficeDocumentCryptography.TryDecryptOfficeDocument(docxFileStream, password, docxDectyptedFileStream);
        // if DOCX document password is not correct
        if (!result)
            throw new System.Exception("Invalid password.");

        docxFileStream.Dispose();
        docxFileStream = docxDectyptedFileStream;
    }

    using (docxFileStream)
    {
        // create image collection
        using (Vintasoft.Imaging.ImageCollection imageCollection = new Vintasoft.Imaging.ImageCollection())
        {
            // add DOCX document to image collection
            imageCollection.Add(docxFileStream);

            // set rendering settings
            imageCollection.SetRenderingSettings(new Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(dpi, dpi));

            // create TiffEncoder
            using (Vintasoft.Imaging.Codecs.Encoders.TiffEncoder tiffEncoder =
                new Vintasoft.Imaging.Codecs.Encoders.TiffEncoder(true))
            {
                // set TIFF compression to Zip
                tiffEncoder.Settings.Compression =
                    Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffCompression.Zip;

                // save images (rendered DOCX pages) of image collection to TIFF file using TiffEncoder
                imageCollection.SaveSync(tiffFileName, tiffEncoder);
            }

            // dispose images
            imageCollection.ClearAndDisposeItems();
        }
    }
}
''' <summary>
''' Converts encrypted DOCX document to a TIFF file using ImageCollection and TiffEncoder classes.
''' DOCX document is rendered with specified resolution.
''' </summary>
Public Shared Sub ConvertEncryptedDocxToTiff(docxFileName As String, password As String, tiffFileName As String, dpi As Single)
    Dim docxFileStream As System.IO.Stream = System.IO.File.OpenRead(docxFileName)

    ' if document is encrypted
    If Vintasoft.Imaging.Office.OfficeDocumentCryptography.IsSecuredOfficeDocument(docxFileStream) Then
        ' create memory stream
        Dim docxDectyptedFileStream As New System.IO.MemoryStream()
        ' try to decrypt DOCX document and save not encrypted DOCX document to a stream
        Dim result As Boolean = Vintasoft.Imaging.Office.OfficeDocumentCryptography.TryDecryptOfficeDocument(docxFileStream, password, docxDectyptedFileStream)
        ' if DOCX document password is not correct
        If Not result Then
            Throw New System.Exception("Invalid password.")
        End If

        docxFileStream.Dispose()
        docxFileStream = docxDectyptedFileStream
    End If

    Using docxFileStream
        ' create image collection
        Using imageCollection As New Vintasoft.Imaging.ImageCollection()
            ' add DOCX document to image collection
            imageCollection.Add(docxFileStream)

            ' set rendering settings
            imageCollection.SetRenderingSettings(New Vintasoft.Imaging.Codecs.Decoders.RenderingSettings(dpi, dpi))

            ' create TiffEncoder
            Using tiffEncoder As New Vintasoft.Imaging.Codecs.Encoders.TiffEncoder(True)
                ' set TIFF compression to Zip
                tiffEncoder.Settings.Compression = Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffCompression.Zip

                ' save images (rendered DOCX pages) of image collection to TIFF file using TiffEncoder
                imageCollection.SaveSync(tiffFileName, tiffEncoder)
            End Using

            ' dispose images
            imageCollection.ClearAndDisposeItems()
        End Using
    End Using
End Sub


Вот C#/VB.NET код, который демонстрирует, как открыть защищенный DOCX-файл и сохранить его в не защищенный DOCX-файл:
/// <summary>
/// Converts encrypted DOCX document to not encrypted DOCX document.
/// </summary>
public static void ConvertEncryptedDocxToDocx(string docxFileName, string password, string resultDocxFileName)
{
    // if document is encrypted
    if (Vintasoft.Imaging.Office.OfficeDocumentCryptography.IsSecuredOfficeDocument(docxFileName))
    {
        // try to decrypt DOCX document and save not encrypted DOCX document to a new file
        bool result = Vintasoft.Imaging.Office.OfficeDocumentCryptography.TryDecryptOfficeDocument(docxFileName, password, resultDocxFileName);
        // if DOCX document password is not correct
        if (!result)
            throw new System.Exception("Invalid password.");
    }
    // if document is not encrypted
    else
    {
        System.IO.File.Copy(docxFileName, resultDocxFileName, true);
    }
}
''' <summary>
''' Converts encrypted DOCX document to not encrypted DOCX document.
''' </summary>
Public Shared Sub ConvertEncryptedDocxToDocx(docxFileName As String, password As String, resultDocxFileName As String)
    ' if document is encrypted
    If Vintasoft.Imaging.Office.OfficeDocumentCryptography.IsSecuredOfficeDocument(docxFileName) Then
        ' try to decrypt DOCX document and save not encrypted DOCX document to a new file
        Dim result As Boolean = Vintasoft.Imaging.Office.OfficeDocumentCryptography.TryDecryptOfficeDocument(docxFileName, password, resultDocxFileName)
        ' if DOCX document password is not correct
        If Not result Then
            Throw New System.Exception("Invalid password.")
        End If
    Else
        ' if document is not encrypted
        System.IO.File.Copy(docxFileName, resultDocxFileName, True)
    End If
End Sub