Create word doc from a memory stream NO file stream used

N

Nitin Mahajan

Guys

Is there a way in C# to create a word object directly from a memory
stream without passing that to hard disk (file stream). I think it
doesn't makes sense to create a file just to read it again in to word
object. If some one has some brilliant idea please share.

In a nutshell this is what I'm currently doing
ByteArray --> File Stream --> Word Object
and this I want to do
ByteArray --> Memory Stream --> Word Object


thanks
nitin
 
R

raylopez99

Guys

Is there a way in C# to create a word object directly from a memory
stream without passing that to hard disk (file stream). I think it
doesn't makes sense to create a file just to read it again in to word
object. If some one has some brilliant idea please share.

In a nutshell this is what I'm currently doing
ByteArray --> File Stream --> Word Object
and this I want to do
ByteArray --> Memory Stream --> Word Object

thanks
nitin

There is a way. I've seen this in C++ code on somebody's blog a while
ago--it was too speed up the app for performance, as the bytearray was
streaming in from a port connected to the internet. I just don't have
a solution offhand right now. One way, however, that might work for
you is in the C#3.0 Cookbook (O'Reilly). Keywords: redirection,
RedirectStandardOutput property of the Process.StartInfo class, also
look to recipe 12.20, writing to multiple output files at one time.

Hope this helps...

RL
 
J

jake

I did something similar to this. I created an xml document, not word
document, using the StringReader class. It creates a memory stream
from a string, and I was able to substitute it into anywhere a file
stream was required.
Hope this helps.
jake
 
N

Nitin Mahajan

No "brilliant idea".  Just replace your FileStream instance with a  
MemoryStream instance, simple as that.

Ideally, your code would have created a FileStream but used it just as a  
Stream.  If so, it's a simple one-line change.  If you declared your  
stream usage as FileStream and then used something that's actually  
declared only in the FileStream class, you might have a little extra  
work.  But that's probably not the case.

Pete

Pete

I dont think it will work because I'm currently using .net 1.1 and
they way the word object can be created is creating a word document
from a file stream and then using that word doc. Now the problem is I
dont want to create a word document physically on the hard disk. I
want to create the word object directly from bytearray as the word
document is stored as blob in database.

Also its a different case from creating XML as in .net there is
inbuilt support for XML but not for word.

I hope I'm clear this time. In anycase thanks for your help.

cheers
nitin
 
R

raylopez99

I dont think it will work because I'm currently using .net 1.1 and
they way the word object can be created is creating a word document
from a file stream and then using that word doc.

Try upgrading to 3.0--the express version is free. Then try it and
see if it solves your problem.

RL
 
N

Nitin

Well, that's not the question you asked.  _Creating_ the Word document,as  
you asked, can be done just as has been suggested.  But yes, if you want  
to _use_ the document in Word, you need some way of getting that data to  
Word.  Unfortunately, as far as I know Word offers no memory-based API to  
load a document.  It has to come from a file.  And I'm not aware of any  
API that would virtualize a block of memory as a file per se.

You might want to look at the unmanaged Windows API, and see if the  
memory-mapped file i/o might suit your purposes.  You'd have to use  
p/invoke in order to write to the memory-mapped file object.  I don't  
recall off the top of my head whether you can create a memory-mapped file 
that is backed only by the swap-file, but if you can that might do what  
you want.

Personally, I think you're probably overthinking the problem.  Creatinga  
temporary file and letting Word read it seems like a fine solution to me. 
It's the exact model used by all sorts of other programs (including email 
programs opening file attachments).

Pete

I guess you are right. I was just checking if there are other ways to
achieve this...
In any case thanks a lot your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top