VintaSoft Imaging .NET SDK 14.0: Documentation for .NET developer
In This Topic
    Codecs: How to work with secured DOCX file?
    In This Topic
    VintaSoft Imaging .NET SDK allows to open and view secured DOCX document. Opened secured DOCX document cannot be edited but can be saved to a new not secured DOCX document.


    Here is C#/VB.NET code that shows how convert secured DOCX file to a multipage TIFF file:
    /// <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
    


    Here is C#/VB.NET code that shows how open secured DOCX file and save it to a not secured DOCX file:
    /// <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