Figure 5-23

So with the toolbar turned off and our report showing as one long page, you can then print your report and have a somewhat decent output as shown in Figure 5-24, which is a preview from Internet Explorer.

The only problem is that this method does not take advantage of any of the neat formatting features for page headers and footers, as the browser just thinks this is one big page to be printed. In addition, the column headings are printed only on the first page, so it is difficult to read the report as you move through the pages.

This method is recommended only for reports with a small number of pages (1-20) as the entire report is concatenated into one long page, which may take a while to render on screen or print.

However, with that said, printing from the browser is the easiest method of printing your report from the Web, even with its limitations. For report developers who have put a lot of time and effort into their report design and want that report to be seen and printed by the users (and look good!), we need to look at another solution.

Figure 5-24

Printing from the Adobe Acrobat Plug-In

Crystal Reports .NET supports many export formats, and one of the more popular ones is Adobe's Portable Document Format, or PDF. Using the export functionality within Crystal Reports .NET and a copy of Adobe Acrobat Reader (or the plug-in) installed on the client machine, reports can be printed from the Web.

This is one of the methods recommended by Crystal Decisions for printing your reports in a presentation-quality format, and it actually developed the workaround used in this section to help developers who were used to the way Crystal Reports normally operates and were frustrated by not having that print button.

The first thing we need to do is create a new Web Form that will contain our instructions. We will call this form AcrobatPrinter.aspx, and create it by right-clicking the project name, and selecting Add ^ Add New Item. We will then select Web Form and name it as noted earlier. Right-click it and select Set As Start Page.

Draw or drag a button onto the Web Form and call it PDF_Button, and label it Export via PDF.

Now we need to do some setup to utilize the Crystal Reports Engine (covered in Chapter 9, "Working with the Crystal Reports Engine") and set some options available from the CrystalDecisions.Shared namespace.

So, we are going to put some code behind our export button to dimension variables for the export options that we want to use, and also for the specific options for exporting to a disk file. Double-click the button in the designer, and insert the following code:

Private Sub PDF_Button_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles PDF_Button.Click Dim myExportOptions As CrystalDecisions.Shared.ExportOptions Dim myDiskFileDestinationOptions As _


Next, we are going to create a variable to hold the name of the file that we are going to be exporting to, as well as creating a new instance of a Sales Report that has already been added both to the project and to this form, through the ReportDocument component.

Dim myExportFile As String

Dim myReport As New web_printing_report()

For our next order of business, we need to set a temporary location for the output file (this can be anywhere on your server) and we are going to build a unique file name using the session ID from the ASP .NET session and tacking the PDF extension on the end, so the file association will work correctly when we go to view this file in our browser.

myExportFile = "C:\Crystal.NET2003\Chapter05\PDF " & _ Session.SessionlD.ToString & ".pdf"

Now, for the meat of the matter—actually setting the destination options to export your report to a PDF file and write it to the disk.

myDiskFileDestinationOptions = New

CrystalDecisions.Shared.DiskFileDestinationOptions() myDiskFileDestinationOptions.DiskFileName = myExportFile myExportOptions = myReport.ExportOptions With myExportOptions

.DestinationOptions = myDiskFileDestinationOptions .ExportDestinationType = .ExportDestinationType.DiskFile .ExportFormatType = .ExportFormatType.PortableDocFormat End With

Then we call the Export method to export our report: myReport.Export()

But we are not done yet! We need to take the exported PDF file that has been generated and output it to the browser so the user can view it using the Acrobat Plug-In or standalone viewer. To do that, we are going to use some simple response statements to return the file to the browser:






.ContentType = "application/pdf"







Finally, once we have delivered the file to the user, we need to clean up after ourselves and remove the file from the server altogether.

System.IO.File.Delete(myExportFile) End Sub

So when all of this code is put together behind our export button and our application is run, the user can click the button and preview and print the report from Adobe Acrobat, with the page numbering and other features in place.

Printing from Other Export Formats

In addition to Adobe Acrobat format, you can also print to other supported export formats such as Excel, Word, or others, by changing the file extension, the MIME type, and the ExportFormatType property in the aforementioned code. There are a number of different destinations that are supported, including:





To export to a Microsoft Excel file


Was this article helpful?

0 0

Post a comment