Hi I am using Aspose with open xml feature to set protection, my req is:
1. Convert Docx to Doc using Aspose
2. applyu password protection.
i notice if i use aspose then using converted doc , getting error file corrupted, but if i use traditanal way to convert docx to doc , its working , i am sending both code, code 1 is working and code 2 is not, can you let me know why aspose corrupt file:
**************working set**********
try
{
FileStream fs = new FileStream("C:\\GUS_Project_Status_122209.docx", FileMode.OpenOrCreate, FileAccess.Read);
Byte[] mydata = new Byte[fs.Length]; // This is what is commited to the db.
fs.Read(mydata, 0, (int)fs.Length);
fs.Close();
//Reading the data from the db into a stream.
MemoryStream stream = new MemoryStream();
stream.Write(mydata, 0, mydata.Length);
byte[] mergedData = stream.ToArray();
stream.Seek(0, SeekOrigin.Begin);
using (FileStream fileStream = File.Create(@"c:\outputnew2.doc"))
{
int size = 4096;
byte[] buffer = new byte[4096];
while (true)
{
size = stream.Read(buffer, 0, buffer.Length);
if (size > 0) fileStream.Write(buffer, 0, size);
else break;
}
fileStream.Close();
}
stream.Close();
string[] args = null;
string file = "lock|C:\\outputnew2.doc";
///
args = file.Split('|');
if (args.Length != 2)
{ Console.WriteLine("Usage: lockdoc lock|unlock filename.docx"); return; }
bool isLock = false;
if (args[0].Equals("lock", StringComparison.OrdinalIgnoreCase))
{ isLock = true; }
else if (!args[0].Equals("unlock", StringComparison.OrdinalIgnoreCase))
{ Console.Error.WriteLine("Wrong action!"); return; }
//DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(args[1], true);
DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(args[1], true);
doc.ExtendedFilePropertiesPart.Properties.DocumentSecurity = new DocumentFormat.OpenXml.ExtendedProperties.DocumentSecurity(isLock ? "8" : "0");
doc.ExtendedFilePropertiesPart.Properties.Save();
DocumentProtection dp = doc.MainDocumentPart.DocumentSettingsPart.Settings.ChildElements.First<DocumentProtection>(); if (dp != null)
{ dp.Remove(); }
if (isLock)
{
dp = new DocumentProtection();
dp.Edit = DocumentProtectionValues.Comments;
dp.Enforcement = DocumentFormat.OpenXml.Wordprocessing.BooleanValues.One;
dp.Hash = "1qYbFPV2bFX/WpB/ii0KfllanDc=";
dp.Salt = "hIqOl99kIr/+N0hyovEq7Q==";
doc.MainDocumentPart.DocumentSettingsPart.Settings.AppendChild(dp);
}
doc.MainDocumentPart.DocumentSettingsPart.Settings.Save();
doc.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
*****************************************************************************
code 2:
*******************not working*********************************************
try
{
//Convert Docx to Doc
string[] args = null;
string file = "lock|C:\\outputnew.doc";
FileStream fs = new FileStream("C:\\GUS_Project_Status_122209.docx", FileMode.OpenOrCreate, FileAccess.Read);
Byte[] mydata = new Byte[fs.Length]; // This is what is commited to the db.
fs.Read(mydata, 0, (int)fs.Length);
fs.Close();
//Reading the data from the db into a stream.
MemoryStream stream = new MemoryStream();
stream.Write(mydata, 0, mydata.Length);
SaveFormat outputFormat = SaveFormat.Doc;
Aspose.Words.Document docnew = new Aspose.Words.Document(stream);
Aspose.Words.ProtectionType protectionType = docnew.ProtectionType;
docnew.Save(stream, outputFormat);
//
args = file.Split('|');
if (args.Length != 2)
{ Console.WriteLine("Usage: lockdoc lock|unlock filename.docx"); return; }
bool isLock = false;
if (args[0].Equals("lock", StringComparison.OrdinalIgnoreCase))
{ isLock = true; }
else if (!args[0].Equals("unlock", StringComparison.OrdinalIgnoreCase))
{ Console.Error.WriteLine("Wrong action!"); return; }
//DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(args[1], true);
DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(stream, true);
doc.ExtendedFilePropertiesPart.Properties.DocumentSecurity = new DocumentFormat.OpenXml.ExtendedProperties.DocumentSecurity(isLock ? "8" : "0");
doc.ExtendedFilePropertiesPart.Properties.Save();
DocumentProtection dp = doc.MainDocumentPart.DocumentSettingsPart.Settings.ChildElements.First<DocumentProtection>(); if (dp != null)
{ dp.Remove(); }
if (isLock)
{
dp = new DocumentProtection();
dp.Edit = DocumentProtectionValues.Comments;
dp.Enforcement = DocumentFormat.OpenXml.Wordprocessing.BooleanValues.One;
dp.Hash = "1qYbFPV2bFX/WpB/ii0KfllanDc=";
dp.Salt = "hIqOl99kIr/+N0hyovEq7Q==";
doc.MainDocumentPart.DocumentSettingsPart.Settings.AppendChild(dp);
}
doc.MainDocumentPart.DocumentSettingsPart.Settings.Save();
doc.Close();
byte[] mergedData = stream.ToArray();
stream.Seek(0, SeekOrigin.Begin);
using (FileStream fileStream = File.Create(@"c:\outputnew1.doc"))
{
int size = 4096;
byte[] buffer = new byte[4096];
while (true)
{
size = stream.Read(buffer, 0, buffer.Length);
if (size > 0) fileStream.Write(buffer, 0, size);
else break;
}
fileStream.Close();
}
stream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
****************************************************************************