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

Free Support Forum - aspose.com

Doubt regarding Aspose.word.Document

Hello Support,

Can you please let me know if I want a function to return Aspose.Words.Document object then what should be the return type specified for the function.

Regards,

Meera

This message was posted using Email2Forum by alexey.noskov.

Hi

Thanks for your inquiry. Your method should return Aspose.Words.Document object. For example please see the following simple method that reads document from web:

///

/// Opens document from web.

///

private Document OpenDocumentFromUrl(string url)

{

//Prepare the web page we will be asking for

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.Method = “GET”;

request.ContentType = “application/msword”;

request.UserAgent = “Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0”;

//Execute the request

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//We will read data via the response stream

Stream resStream = response.GetResponseStream();

//Write content into the MemoryStream

BinaryReader resReader = new BinaryReader(resStream);

MemoryStream docStream = new MemoryStream(resReader.ReadBytes((int)response.ContentLength));

// Open document from stream.

Document doc = new Document(docStream);

return doc;

}

Hope this helps. Please feel free to ask if you need more assistance, I will e glad to help you.

Best regards,

Hello Alexey,

I have a web method as shown below. In my case I need to expose Aspose.Word.document object objWordDoc to the users in a web method.
[WebMethod(Description = “Expose Word document”)]
public Aspose.Words.Document WordExpose(string sFileName)
{
string[] sTemp;
sTemp = sFileName.Split(’.’);
string sSpecFile = sTemp[0];
string sRevision = sTemp[1];
string dataDir = @“D:\temp”;
try
{
file = File.ReadAllBytes(dataDir + “HHV00101.00A”)
objStream = new System.IO.MemoryStream(file, 0, file.Length);
objWordDoc = new Aspose.Words.Document(objStream);
}

catch (Exception ex)
{
throw ex;
}

return (objWordDoc);
}

When I build the web site then I am getting the below error.
To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. Aspose.Words.Document does not implement Add(System.Object).

Can you please help me to fix this problem at the earliest. Can you let me know how to use the above code as a webmethod. If I use like a simple function then it will work but my case is I want to use this as a web method which will return document object as output.

Best Regards,
Meera

Hi

Thank you for additional information. You can return byte array in order to serialize the document:

private byte[] YourMethod()

{

// Some your code to generate document.

// ………………………………………………………

MemoryStream outStream = new MemoryStream();

// Save document to stream.

doc.Save(outStream, SaveFormat.Doc);

// Return byte array.

return outStream.ToArray();

}

Hope this helps.

Best regards,

Hello Alexey,

Returning the byte is not a good solution for me.
I was to return Aspose.Words.Document objWordDoc object not the byte information.

[WebMethod(Description = “Expose Word document”)]
public Aspose.Words.Document WordExpose(string sFileName)
{
string[] sTemp;
sTemp = sFileName.Split(’.’);
string sSpecFile = sTemp[0];
string sRevision = sTemp[1];
string dataDir = @“D:\temp”;
byte[] file;
System.IO.Stream objStream;
Aspose.Words.Document objWordDoc = new Aspose.Words.Document();
try
{
file = File.ReadAllBytes(dataDir + “HHV00101.00A”)
objStream = new System.IO.MemoryStream(file, 0, file.Length);
objWordDoc = new Aspose.Words.Document(objStream);
}

catch (Exception ex)
{
throw ex;
}

return (objWordDoc);
}

Please help me.

Kind Regards,
Meera

Hi

Thank you for additional information. You should return byte array and then restore the document using code like the following:

// Create memory stream from bytearray.

MemoryStream docStream = new MemoryStream(docBytes);

// Create document from stream.

Document doc = new Document(docStream);

Your web method should look like this:

[WebMethod(Description = “Expose Word document”)]

public byte[] WordExpose(string sFileName)

{

string[] sTemp;

sTemp = sFileName.Split(’.’);

string sSpecFile = sTemp[0];

string sRevision = sTemp[1];

string dataDir = @“D:\temp”;

byte[] file;

System.IO.Stream objStream;

Aspose.Words.Document objWordDoc = new Aspose.Words.Document();

try

{

file = File.ReadAllBytes(dataDir + “HHV00101.00A”)

objStream = new System.IO.MemoryStream(file, 0, file.Length);

objWordDoc = new Aspose.Words.Document(objStream);

}

catch (Exception ex)

{

throw ex;

}

MemoryStream outStream = new MemoryStream();

// Save document to stream.

objWordDoc.Save(outStream, SaveFormat.Doc);

// Return byte array.

return outStream.ToArray();

}

Best regards,