We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Docx invalid file format error -> New Document(Stream)

We are using Aspose.Total and we have a problem with Aspose.Words (which we are using for several years now). We just upgraded to the last version so that we can work with docx files.

We read the document file from a database and create a new document using a stream, the code is below:

byte[] arForm = GetDocumentFromDatabase(lDocumentID);

MemoryStream streamDoc = new MemoryStream();

streamDoc.Write(arForm,0,arForm.Length);

streamDoc.Seek(0, SeekOrigin.Begin);

Document doc = new Document(streamDoc);

This works fine for .doc (Word 200/2003 etc) but when we use .docx (Word 2007) file error occurs saying that it is an invalid file format. The exact error is:

Unknown file format.
Stack trace
at Aspose.Words.Document.?(Stream ?, LoadFormat ?, String ?)
at Aspose.Words.Document..ctor(Stream stream, String baseUri, LoadFormat loadFormat, String password)
at Aspose.Words.Document..ctor(Stream stream, String baseUri)
at Aspose.Words.Document..ctor(Stream stream)

I hope someone can help.

Best regards Patrick Keukens

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

Thanks for your request. Could you please attach your document for testing? I tried to reproduce the problem with simple DOCX document but all works fine on my side.

Best regards.

Hi Alexy,

I attached the document that I'm testing. It's a docx and there are some mergefields in the document.

Best Regards,

Patrick Keukens

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

Thanks you for additional information. I tested your document and it seems all works fine on my side. I use the latest version of Aspose.Words (6.0.1) for testing.

I used the following code to write your document into DB

//Create sql command

string commandString = "INSERT INTO [Documents] VALUES('test.docx', @Doc)";

System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandString, conn);

//Add paramenter @Doc

command.Parameters.AddWithValue("Doc", File.ReadAllBytes(@"C:\Temp\adres_sjabloon.docx"));

//Open connection

conn.Open();

//Write document to DB

command.ExecuteNonQuery();

//Close DB connection

conn.Close();

and the following code to get it from DB

//Create DataSet

DataSet ds = new DataSet();

//Create sql command

string commandString = "SELECT FileContent FROM Documents WHERE ID=10";

System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandString, conn);

//Create data adapter

System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(command);

//Open connection

conn.Open();

//Read dataset

adapter.Fill(ds);

conn.Close();

//Save document to hard disk

if (ds.Tables.Count > 0)

{

if (ds.Tables[0].Rows.Count > 0)

{

byte[] buffer = (byte[])ds.Tables[0].Rows[0][0];

MemoryStream stream = new MemoryStream(buffer);

Document doc = new Document(stream);

doc.Save(@"C:\Temp\out.doc");

}

}

Maybe you can provide me additional information that could help me to reproduce the problem.

Best regards.

Hi,

Oke I found it, I have several versions of the aspose.words file on my system. When I added the reference, it copied the correct dll file to the project. But when I started to run the project it copied an old version of the dll to the project's bin directory and changed the reference automatically.

Now I have the right version in my project but it gives another error:

c:\SERVER\InnoView\Projects\VS2003\arbeidsvoorwaarden\bin\Aspose.Words.dll Cannot declare a namespace and a type both named 'Aspose.Words.Lists'

Do you know what this means?

Best Regards Patrick Keukens

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

Thanks for your request. In the old version, there was Aspose.Words.Lists class. Currently, there is Aspose.Words.Lists namespace.

Here is changes in API:

  • All collection classes, such as Paragraphs, Runs, Tables, Bookmarks etc have been renamed into ParagraphCollection, RunCollection, TableCollection, BookmarkCollection, CellCollection accordingly.
  • New namespaces Aspose.Words.Fields, Aspose.Words.Lists, Aspose.Words.Markup, Aspose.Words.Properties and Aspose.Words.Tables, Aspose.Words.Rendering were introduced and classes moved into the new namespaces.

Best regards.