Free Support Forum - aspose.com

SetPrivilege throwing exception

In the following code "SetPrivilege" is throwing exception.

Workbook workbook = new Workbook();
string filename = @"C:\Temp\Untitled\CPS_files\Sample_1.xlsx";
string outputFile = @"C:\Temp\Untitled\CPS_files\Sample_2.pdf";
workbook.Open(filename);
MemoryStream inputStream = workbook.SaveToStream();
MemoryStream outputStream = new MemoryStream();
outputStream.Seek(0, SeekOrigin.Begin);
Aspose.Cells.SaveOptions option = workbook.SaveOptions;
option.SaveFormat = SaveFormat.Pdf;
inputStream.Seek(0, SeekOrigin.Begin);
outputStream.Seek(0, SeekOrigin.Begin);
PdfExtractor ext = new PdfExtractor();
PdfFileSecurity fileSecurity = new PdfFileSecurity(inputStream, outputStream);
//fileSecurity.ChangePassword(null, "B", "B");
fileSecurity.SetPrivilege("userPassword","ownerPassword", DocumentPrivilege.AllowAll);
FileStream fs = new FileStream(outputFile,FileMode.CreateNew);
StreamReader rs = new StreamReader(outputStream);
fs.Write(outputStream.ToArray(),0, (int)outputStream.Length);
fs.Close();

Hi Snutula,

We’re looking into this issue at our end and you’ll be updated with the results the earliest possible.

We’re sorry for the inconvenience.
Regards,

Hi Snutula,

You are missing the following line of code:

workbook.Save(outputStream, SaveFormat.Pdf);

Code snippet:

string filename = @"C:\Sample_1.xlsx";
string outputFile = @"C:\Sample_2.pdf";

Workbook workbook = new Workbook(filename);

MemoryStream inputStream = workbook.SaveToStream();
MemoryStream outputStream = new MemoryStream();
MemoryStream outputStream_PDF = new MemoryStream();

inputStream.Seek(0, SeekOrigin.Begin);
outputStream.Seek(0, SeekOrigin.Begin);
outputStream_PDF.Seek(0, SeekOrigin.Begin);

workbook.Save(outputStream, SaveFormat.Pdf);

Aspose.Pdf.Kit.PdfFileSecurity fileSecurity = new Aspose.Pdf.Kit.PdfFileSecurity(outputStream, outputStream_PDF);
fileSecurity.SetPrivilege("usrpass", "ownrpass", Aspose.Pdf.Kit.DocumentPrivilege.AllowAll);
System.IO.File.WriteAllBytes(outputFile, outputStream_PDF.ToArray());

Thanks,