Hi there,
Following is my code. I am trying to generate MS Word and PDF document after following things. I will really if you can help me fixing this problem as this is going in circle since last few days.
- Reading values from DB
- Merging to one or more document
- Final output will be multipage document.
This is a function from my business object, This is generating document via Aspose and returns Byte array and UI throws that Byte array to response object.
I was getting this when I used old version of ASPOSE, so, I have asked my company to upgrade with your new version, but no difference.
It’s working Great with MS Word. So, Rest of the bits are correct. except I can’t generate PDF. I have multiple tables and text, no images.
See Attached image when I generate PDF doc. Image shows error. I am not doing anything with email.
Interesting thing:
I saw your website for example and I found that you guys are saving first as a XML file and then saving as PDF. I did same and it worked fine. Means, I have generated PDF in C:\temp\abc.pdf. But, So, ASPOSE PDF product doesn’t mind my word document manipulation.
But, it’s something to do with Memory stream. When I use memory stream, it doesn’t work well.
- I have tried SaveFormat.AsposePdf & SaveFormat.PDF
- aSaveFormat is my variable where I am passing either SaveFormat.AsposePdf or SaveFormat.Doc
- oDocSummaryServices is used for some data manipulation
Public Function ExportView(ByVal aSaveFormat As SaveFormat, ByVal oDocSummaryServices As List(Of NEMICSDO.DocSummaryService), ByRef SizeOfFile As Integer) As Byte()
Try
Me.dstDoc = New Document(Path.Combine(Me.sTemplateDir, Me.DOC_MasterPage_Template))
Me.m_oMemoryStream = New IO.MemoryStream ' Doing Document manipulation
For Each aSerivce In oDocSummaryServices
Me.srcDoc = New Document(Path.Combine(Me.sTemplateDir, Me.sTemplateFile))
Dim srcStream As New IO.MemoryStream Me.srcDoc.Save(srcStream, SaveFormat.Doc)
FillServiceTemplate(aSerivce, Me.srcDoc)
CType(Me.srcDoc.ChildNodes(0), Aspose.Words.Section).PageSetup.SectionStart = SectionStart.Continuous
Appendchild()
Next
' At this point, full document is ready as client expects.
'Saving Document Me.dstDoc.Save(Me.m_oMemoryStream, aSaveFormat)
If aSaveFormat = SaveFormat.AsposePdf Then
' If we want PDF
Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()
pdf.BindXML(Me.m_oMemoryStream, Nothing)
pdf.Save(Me.m_oMemoryStream)
End If
SizeOfFile = Me.m_oMemoryStream.Length
Return ConveryMemoryStreamToBytes()
Catch ex As Exception
Throw
End Try
Return Nothing
End Function
Private Function ConveryMemoryStreamToBytes() As Byte()
Try
'Seek to the beginning so it can be read by XmlDocument.
Me.m_oMemoryStream.Seek(0, SeekOrigin.Begin)
Dim oData(Me.m_oMemoryStream.Length) As Byte
Me.m_oMemoryStream.Read(oData, 0, Me.m_oMemoryStream.Length)
Return oData
Catch ex As Exception
Throw
End Try
End Function
On UI
Dim ByteDoc As Byte() = oDocExport.ExportView(ExportType, _DocSummaryServices, SizeOfFile)
Try
If ExportType = Constants.ExportType.PDF Then
Response.ContentType = "application/pdf"
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.AddHeader("Content-Disposition", "attachment;filename=" + "Services_" & Now.Date.ToShortDateString & ".pdf")
ElseIf ExportType = Constants.ExportType.MSWord Then
Response.ContentType = "application/msword"
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.AddHeader("Content-Disposition", "attachment;filename=" + "Services_" & Now.Date.ToShortDateString & ".doc")
End If
Response.OutputStream.Write(ByteDoc, 0, SizeOfFile)
Catch ex As Exception
Throw
End Try