I am trying to merge multiple pdf files created from Crystal Reports. The first one is created and will always show, then based on criteria, others are appended. Sample Code:
Dim sReport As New MemoryStream
Dim doc As MergeDocument
Dim QuoteReportSource As New CrystalReportSource
Dim quoteRpt As New Report
quoteRpt.FileName = "Quote.rpt"
QuoteReportSource.Report = quoteRpt
QuoteReportSource.ReportDocument.SetDatabaseLogon(crUserName, crPassword)
QuoteReportSource.ReportDocument.SetParameterValue("@QuoteID", quoteID)
sReport = QuoteReportSource.ReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
doc = New MergeDocument(New PdfDocument(sReport))
PrintQuote(quoteID, doc, crUserName, crPassword)
This gets the first generated pdf from crystal reports (Quote.rpt) exports it to the memory stream, and then the merge document is created from that. The last line (PrintQuote()), calls a function that will determine what others need appending. It is shown below:
Private Sub PrintQuote(ByVal quoteID As Integer, ByVal quoteDoc As MergeDocument, ByVal crUserName As String, ByVal crPassword As String, ByVal startPos As Integer)
Dim db As New ArtisansLinqDataContext
Dim company = (From d In db.WorksheetDetails Where d.QuoteID = quoteID Select d.CompanyName).First
Dim triaSource As New CrystalReportSource
Dim triaRpt As New Report
Dim triaStream As New MemoryStream
If company = "Colony Insurance" Then
triaRpt.FileName = "Colony/ColonyTRIA.rpt"
triaSource.Report = triaRpt
triaSource.ReportDocument.SetDatabaseLogon(crUserName, crPassword)
triaSource.ReportDocument.SetParameterValue("@QuoteID", quoteID)
triaStream = triaSource.ReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
quoteDoc.Append(New PdfDocument(triaStream))
End If
quoteDoc.DrawToWeb(String.Format("JJArtisansQuote_{0}.pdf", quoteID), True)
End Sub
Now, what happens is if the condition is met, there are 2 pages to the merged pdf, as there should be (one for the quote.rpt, and one for the ColonyTRIA.rpt), bu both pages hold the quote.rpt - it's as if that first just gets repeated in the append.
If the condition isn't met, then it does just the one page as it should. Am I using the memory stream incorrectly here, or am I trying to do something that can't be done?
Thank you,
Merging Multiple Crystal Reports
Re: Merging Multiple Crystal Reports
Support · May 20, 2008Hello,
We are able to recreate the behavior using the above code and we have done some more testing in the above scenario.
This problem is occuring only when getting the stream from the QuoteReportSource.ReportDocument.ExportToStream method. This method is returning the same number of bytes for the second report file (from the method PrintQuote) as it returned for the first report file. This could be the reason for seeing the same report two times.
We have tested the same scenario with different streams without using the above method, and it is working fine. Following is the sample code we have used.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim stream1 As New MemoryStream
'Main PDF document
Dim doc As MergeDocument
'First report PDF
Dim pdf As New MergeDocument("C:\Temp\DocA.pdf", 1, 1)
'draw the first report to stream using the Draw method
pdf.Draw(stream1)
'Create the main PDF using the above stream
doc = New MergeDocument(New PdfDocument(stream1))
'Calling the method to add another stream to the main PDF
PrintQuote(doc)
'Draw the PDF to web
doc.DrawToWeb()
End Sub
Private Sub PrintQuote(ByVal quoteDoc As MergeDocument)
If 1 = 1 Then 'some condition
Dim stream2 As New MemoryStream
'second report PDF
Dim pdf As New MergeDocument("C:\Temp\DocB.pdf", 1, 1)
'draw the second report to stream using the Draw method
pdf.Draw(stream2)
'Append the second stream to the document
quoteDoc.Append(New PdfDocument(stream2))
End If
End Sub
Thanks,
ceTe Software Support Team.