Codecs: How to work with secured XLSX file?
In This Topic
VintaSoft Imaging .NET SDK allows to open and view secured XLSX document. Opened secured XLSX document cannot be edited but can be saved to a new not secured XLSX document.
Here is C#/VB.NET code that shows how convert secured XLSX file to a multipage TIFF file:
/// <summary>
/// Converts encrypted XLSX document to a TIFF file using ImageCollection and TiffEncoder classes.
/// XLSX document is rendered with specified resolution.
/// </summary>
public static void ConvertEncryptedXlsxToTiff(string xlsxFileName, string password, string tiffFileName, float dpi)
{
System.IO.Stream xlsxFileStream = System.IO.File.OpenRead(xlsxFileName);
// if document is encrypted
if (Vintasoft.Imaging.Office.OfficeDocumentCryptography.IsSecuredOfficeDocument(xlsxFileStream))
{
// create memory stream
System.IO.MemoryStream xlsxDectyptedFileStream = 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(xlsxFileStream, password, xlsxDectyptedFileStream);
// if XLSX document password is not correct
if (!result)
throw new System.Exception("Invalid password.");
xlsxFileStream.Dispose();
xlsxFileStream = xlsxDectyptedFileStream;
}
using (xlsxFileStream)
// create image collection
using (Vintasoft.Imaging.ImageCollection imageCollection = new Vintasoft.Imaging.ImageCollection())
{
// add XLSX document to image collection
imageCollection.Add(xlsxFileStream);
// 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 XLSX pages) of image collection to TIFF file using TiffEncoder
imageCollection.SaveSync(tiffFileName, tiffEncoder);
}
// dispose images
imageCollection.ClearAndDisposeItems();
}
}
''' <summary>
''' Converts encrypted XLSX document to a TIFF file using ImageCollection and TiffEncoder classes.
''' XLSX document is rendered with specified resolution.
''' </summary>
Public Shared Sub ConvertEncryptedXlsxToTiff(xlsxFileName As String, password As String, tiffFileName As String, dpi As Single)
Dim xlsxFileStream As System.IO.Stream = System.IO.File.OpenRead(xlsxFileName)
' if document is encrypted
If Vintasoft.Imaging.Office.OfficeDocumentCryptography.IsSecuredOfficeDocument(xlsxFileStream) Then
' create memory stream
Dim xlsxDectyptedFileStream 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(xlsxFileStream, password, xlsxDectyptedFileStream)
' if XLSX document password is not correct
If Not result Then
Throw New System.Exception("Invalid password.")
End If
xlsxFileStream.Dispose()
xlsxFileStream = xlsxDectyptedFileStream
End If
Using xlsxFileStream
' create image collection
Using imageCollection As New Vintasoft.Imaging.ImageCollection()
' add XLSX document to image collection
imageCollection.Add(xlsxFileStream)
' 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 XLSX 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 XLSX file and save it to a not secured secured XLSX file:
/// <summary>
/// Converts encrypted XLSX document to not encrypted XLSX document.
/// </summary>
public static void ConvertEncryptedXlsxToXlsx(string xlsxFileName, string password, string resultXlsxFileName)
{
// if document is encrypted
if (Vintasoft.Imaging.Office.OfficeDocumentCryptography.IsSecuredOfficeDocument(xlsxFileName))
{
// try to decrypt XLSX document and save not encrypted XLSX document to a new file
bool result = Vintasoft.Imaging.Office.OfficeDocumentCryptography.TryDecryptOfficeDocument(xlsxFileName, password, resultXlsxFileName);
// if XLSX document password is not correct
if (!result)
throw new System.Exception("Invalid password.");
}
// if document is not encrypted
else
{
System.IO.File.Copy(xlsxFileName, resultXlsxFileName, true);
}
}
''' <summary>
''' Converts encrypted XLSX document to not encrypted XLSX document.
''' </summary>
Public Shared Sub ConvertEncryptedXlsxToXlsx(xlsxFileName As String, password As String, resultXlsxFileName As String)
' if document is encrypted
If Vintasoft.Imaging.Office.OfficeDocumentCryptography.IsSecuredOfficeDocument(xlsxFileName) Then
' try to decrypt XLSX document and save not encrypted XLSX document to a new file
Dim result As Boolean = Vintasoft.Imaging.Office.OfficeDocumentCryptography.TryDecryptOfficeDocument(xlsxFileName, password, resultXlsxFileName)
' if XLSX 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(xlsxFileName, resultXlsxFileName, True)
End If
End Sub