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