Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Oct 04, 2012 4:53 pm

Hi,
I have a byteArray which is a docx. I'm trying to convert it to pdf and add it to the Response object so the user can download it or open it.
I've tried this which the pdf is corrupted
'docArray is the byte file from the db
Try
Dim doc As Spire.Doc.Document = New Spire.Doc.Document()
Dim oFileStream As System.IO.FileStream

oFileStream = New System.IO.FileStream(strTempName, System.IO.FileMode.Create)
'Write file to stream
oFileStream.Write(docArray, 0, docArray.Length - 1)

'Add docx to stream
doc.LoadFromStream(oFileStream, Spire.Doc.FileFormat.Docx)

'Covert docx to PDF
doc.SaveToStream(oFileStream, Spire.Doc.FileFormat.PDF)

Dim streamLength As Integer = Convert.ToInt32(oFileStream.Length)
Dim fileData As Byte() = New Byte(streamLength) {}

' Read the file into a byte array
oFileStream.Read(fileData, 0, streamLength)

oFileStream.Close()
strTempName = strTempName.Substring(0, strTempName.IndexOf("."))
Context.Response.Clear()
Context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=""" & strTempName & ".pdf" & """")
Context.Response.AddHeader("Content-Length", fileData.Length.ToString())
Context.Response.ContentType = "application/octet-stream"
Context.Response.BinaryWrite(fileData)
Context.Response.Flush()
Catch ex As Exception
Context.Response.ContentType = "text/plain"
Context.Response.Write(ex.Message)
Finally
Context.Response.[End]()
End Try

And this gives me errors...

Using ms As Stream = New MemoryStream(docArray, True)

ms.Write(docArray, 0, docArray.Length)
doc.SaveToStream(ms, Spire.Doc.FileFormat.PDF)
Please help.

Thanks,
J Torres

jtingres
 
Posts: 6
Joined: Wed Oct 03, 2012 7:22 pm

Fri Oct 05, 2012 5:31 am

Hello J Torres,

Thanks for your feedback and so sorry for the inconvenience caused by us.

We have tested the issue you reported with the latest version(Spire.doc 4.4.30), it works fine. Would you please have a try with the ne version, you can download it at http://www.e-iceblue.com/Download/download-word-for-net-now/spiredoc-pack-hot-fix.html?Itemid=0 .

I attached a demo which illustrates Spire.Doc how to load a doc file using loadFomStream metod and convert it to pdf, and then response the pdf file to download it. Please have a look.

If you still have this problem, please send us your original doc file to help us reproduce and solve this problem.
Thank in advance.

Have a great day!
Suvi
e-iceblue support
User avatar

Suvi.Wu
 
Posts: 154
Joined: Thu Oct 20, 2011 2:53 am

Fri Oct 05, 2012 7:30 pm

Suvi,
Thanks for the Code, were evaluating to buy this product for our project but I'm still getting a problem, this is my new code:

Try
Dim doc As Spire.Doc.Document = New Spire.Doc.Document()
Dim stream As Stream = New MemoryStream(docArray) <--docArray is already a byteArray of the document which is a docx doc.LoadFromStream(stream, Spire.Doc.FileFormat.Docx)
doc.SaveToStream(stream, Spire.Doc.FileFormat.PDF) <--When I try to do this I get the error:Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index, even when I try to put this into an empty stream. I want to save it to stream not on the computer or server. Can this be done.
Dim streamLength As Integer = Convert.ToInt32(stream2.Length)
Dim fileData As Byte() = New Byte(streamLength) {}
stream.Read(fileData, 0, streamLength)
stream.Close()

strTempName = strTempName.Substring(0, strTempName.IndexOf("."))
Context.Response.Clear()
Context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=""" & strTempName & ".pdf" & """")
Context.Response.AddHeader("Content-Length", fileData.Length.ToString())
Context.Response.ContentType = "application/octet-stream"
Context.Response.BinaryWrite(fileData)
Context.Response.Flush()
Catch ex As Exception
Context.Response.ContentType = "text/plain"
Context.Response.Write(ex.Message)
Finally
Context.Response.[End]()
End Try


Thanks
JT

jtingres
 
Posts: 6
Joined: Wed Oct 03, 2012 7:22 pm

Mon Oct 08, 2012 6:26 am

Hello JT,

Could you save docArray as a docx-document by the method File.WriteAllBytes("sample.docx", docArray) and send it to us? So that we can reproduce your problem, and provide you a solution. Thanks.

Best regrads,
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Tue Oct 09, 2012 2:50 pm

Here is the doc.
Again here is what I want.
1. I get the doc from my oracle db in in byte array. This document was an original docx.
2. I want to in a stream, convert this into a pdf file.
3. Then I want to let the user Save or view it via the Response object via a byte object.
4. I don't at anytime store this document anywhere, unless in the Response object the user chooses to save it to there computer.

Thanks,

JT

jtingres
 
Posts: 6
Joined: Wed Oct 03, 2012 7:22 pm

Thu Oct 11, 2012 3:03 am

Hello JT,

Please try to use the following way, we have done a test for it, and it works fine, please try to do it. If you still have the problem, please tell us.
String path = "Soil Moisture Active and Passive (SMAP).docx";
FileStream fs = new FileStream(Server.MapPath(path), FileMode.Open);
byte[] array = new byte[fs.Length];
fs.Read(array, 0, array.Length);
Stream stream = new MemoryStream(GetDoc()); //GetDoc() get the doc from oracle db in in byte array.
Document document = new Document();
document.LoadFromStream(stream, FileFormat.Docx);
using (Stream stream2 = new MemoryStream())
{
document.SaveToStream(stream2, FileFormat.PDF);
}

Best regrads,
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc