Free Support Forum - aspose.com

Aspose Corrupting doc - while using Conversion feature

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);

}

****************************************************************************

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. But I think you are confused in your own code. Why do you think that document produced by Aspose.Words is corrupted? Cannot you open the output in MS Word?

As I can see in your case after saving document to stream using Aspose.Words, you try to open this stream using DocumentFormat.OpenXml.Packaging.WordprocessingDocument, but in this case, you should note, that you can open only DOCX documents using this class. So if you saved document to stream in DOC document using Aspose.Words you will not be able to open it using DocumentFormat.OpenXml.Packaging.WordprocessingDocument.

Best regards,

but i can open doc file , if i rename as doc file, so why i think , aspose creating error while converting file, becoz if i convert docx to doc , then passwing in openxml document object i am able to open file, without error

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. In your first code snippet, you just change extension of DOCX document to DOC, you do not actually convert document. The output document is still DOCX file, but with DOC extension.

Changing an extension of a file is not enough to change its format, I suppose, you should know this.

Best regards,