Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print to file using ReportDocument object model?

Status
Not open for further replies.

N11689

Programmer
Jan 29, 2002
113
0
0
US
We used to use the PrintOutEx method in our old VB6 program (when using RDC). Now, with VB.NET, we have switched to using Crystal Reports for .NET. I need to print a report to a file, but the PrintOutEx mehod is no longer available.

How do you print to a file using Crystal Reports for .NET (ReportDocument object model)? We are using VS2005.

Thank you.
 
Although we're using VS2002, the following code allows a given report (after setting parameters information, etc.) to be exported to either PDF, Excel or Word format and saved as a file on the web server's file system. The "objRpt" variable is instantiated as a ReportDocument object. Hope this helps a bit!
===================================================
Try
Dim ExportPath As String
Dim crExportOptions As ExportOptions
Dim crDiskFileDestinationOptions As DiskFileDestinationOptions
Dim FName As String
Dim tFileExtn As String
Dim tExportDir As String
Dim tFileName As String
Dim tDirName As String
Dim blnShowRpt As Boolean = False

crDiskFileDestinationOptions = New DiskFileDestinationOptions()
crExportOptions = objRpt.ExportOptions

' Based on the format the user selects...
Select Case Request.QueryString("format").Trim.ToUpper
Case "PDF"
blnShowRpt = True
tFileExtn = ".pdf"
crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat
Case "WORD"
tFileExtn = ".doc"
crExportOptions.ExportFormatType = ExportFormatType.WordForWindows
Case "EXCEL"
tFileExtn = ".xls"
crExportOptions.ExportFormatType = ExportFormatType.Excel
Case "CSV"
tFileExtn = ".csv"
End Select

' Specify an export directory (tExportDir already previously specified)
If Directory.Exists(tExportDir) = False Then Directory.CreateDirectory(tExportDir)

FName = "test" & tFileExtn
tFileName = FName
FName = tExportDir & "\" & FName
crDiskFileDestinationOptions.DiskFileName = FName

' Set the required report ExportOptions properties
With crExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.DestinationOptions = crDiskFileDestinationOptions
End With

Try
objRpt.Export()

If blnShowRpt Then
'This code directly exports and opens the PDF file
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.WriteFile("../" & tDirName & "/" & tFileName)
Response.Flush()
Response.Close()

' Delete the PDF from the file system since it has been streamed to the browser
System.IO.File.Delete(FName)
Else
' Provide a link allowing the user access to the exported Word/Excel file on the file system
hlnkViewFile.NavigateUrl = "../" & tDirName & "/" & tFileName

Catch err As Exception
' Do error handling here
End Try
Catch ex as Exception
' Do error handling here
End Try

-- Creator of the XModuleSB Application: --
 

Make sure you add the VC_User_CRT... and VC_User_STL... merge modules to your application's deployment project too. They contain the libraries needed to export data. Without them, exports that run great on your development servers will tank with a licensing error on machines with VS installed.

Pat
 
Thanks, but we need to export the file as is. We don't want to 'format' it to excel, word, pdf, etc.

We create a postscript file that we use to fax, but we need to export it to a directory in its postscript format. Therefore, we cannot export it as a pdf or any other format.

Just need to know how to print to file.

Thanks for you help. Anyone else have a suggestion?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top