LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Merging and Password Protecting PDFs Exported by Crystal Reports
by Richard Dudley
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 82469/ 111


[Download Code]

Being able to produce reports in PDF files is an extremely useful feature of Crystal Reports.  It may be necessary at times to combine two very different Crystal Reports into the same PDF, or to add a Crystal Report to a project document, or even to password protect a Crystal Report.  Unfortunately, Crystal Reports can not do these when it exports a report to PDF.

The process of combining two PDF files together is known as "merging" or "stitching", and can be accomplished easily in an ASP.NET application by using available components.  Dynamic PDF will be used for this article because it is written in managed code, and can be easily referenced and used in ASP.NET applications without needing COM Interop.  If you are in an environment where ActiveX controls are preferable, Persits Software produces an ActiveX Control named ASPPDF.  Both products offer trial versions you can download and experiment with.  You may wish to try both products to see which one fits your needs.

You can find Dynamic PDF Merger by clicking this link.

You can find ASPPDF by clicking this link.

(Note: The components discussed above are produced by sponsors of this site.  The links to these components are this site's advertising links.  There may be other available components that can perform the same functions that we discuss here.)

Creating the Project

[Download Code]

Follow the link above and download the trial version of Dynamic PDF Merger.  The installer will install both Dynamic PDF Merger and Dynamic PDF Generator Community Edition, and we will reference both in this project.

Start Visual Studio, and create an ASP.NET application in the language of your choice (VB.NET will be used for this article).  Close and delete WebForm1.aspx.  For the data source, we will use the Xtreme Traders sample database that is installed when you originally installed Visual Studio.

Since we will be exporting files in this application, we need to make sure the ASPNET user (Network Service in Windows 2003 Server) has MODIFY permissions on the application folder.  Open your web root folder (usually c:\inetpub\wwwroot), right-click on your application's folder, and choose Properties.  Open the Security tab, and add the ASPNET user (if not already present).  Grant the ASPNET user MODIFY permissions, and click OK a couple of times to set the permissions.

Add a new Crystal Report to your project, and name the file Customers.rpt.  Use the Report Wizard to create a Standard Report.  (Note: The RPT files are included in the code download.)

Figure 1 - Data WindowThe Data window (Figure 1 - click thumbnails for larger image) is the first step of the Report Wizard.  Expanding the Access/Excel (DAO) node opens the Connection window (Figure 2).Figure 2 - Connection Window  Navigate to the Xtreme Traders sample database located at C:\Program Files\Microsoft Visual Studio .NET 2003\Crystal Reports\Samples\Database\xtreme.mdb (if you are using VS 2002, the path will not include the year version).  Click OK when done.  You'll be back in the Data window, with the database selected and opened.

Figure 3 - Data WindowNext, we will create a report using the Customer table.  Expand the Tables node, and double-click the Customers table to select it (Figure 3).  Click "Next".

On the Fields tab, choose Customer ID, Customer Name, E-mail and Website (Figure 4).  Click Finish to create the first report.Figure 4 - Fields Window

Create the second report by adding another Crystal Report and following the same steps as above.  This time, name the file Suppliers.rpt, choose the Supplier table, and select all the fields in the table.  Click Finish after the fields window to create the second report.

Add a new web form to the project, named default.aspx.  Leave this page blank for now.  (Note: default.aspx is included in the code download.)

One final step is to add references to Dynamic PDF Merger and Dynamic PDF Generator.  Right-click on References, and select the ".NET" tab.  Scroll down until you find "DynamicPDF Merger for .NET" and "Dynamic PDF Generator for .NET", and click Select.  Click OK to finish adding the references.  Make sure you add both references to your project.

We are now ready to test our reports, and set up the export functionality.

Exporting the Reports

[Download Code]

Open the code behind file for default.aspx (default.aspx.vb), and add the following statements at the top of the file:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO

Before we go any further, we'll make sure that we have everything set correctly.  We will test loading and exporting each report, one at a time.

To test the first report, add the following code to your page_load:

  Dim strExportFile1, strExportFile2 As String

  strExportFile1 = GenerateReport("customers")

  'strExportFile2 = GenerateReport("suppliers")

Add the following code after your page_load but before the end of your page class:

 Function GenerateReport(ByVal ReportName As String)

  Dim MyReport As New ReportDocument
  Dim strExportFile As String
  Dim objExOpt As ExportOptions
  Dim objDiskOpt As New DiskFileDestinationOptions

  strExportFile = ReportName & ".pdf"

  MyReport.Load(Server.MapPath(ReportName & ".rpt"))

  objDiskOpt.DiskFileName = Server.MapPath(".") & "/" & strExportFile
  objExOpt = MyReport.ExportOptions
  objExOpt.ExportDestinationType = ExportDestinationType.DiskFile
  objExOpt.ExportFormatType = ExportFormatType.PortableDocFormat
  objExOpt.DestinationOptions = objDiskOpt

  Return strExportFile

 End Function

Compile your project and load the default page.  The customers report should open in Acrobat Reader.

To test the Suppliers report, comment out the line that generates the Customers report, and un-comment the lines that generate the Suppliers report.  Recompile your project and load the page again.  The suppliers report should open in Acrobat Reader.

Now that we know the reports will export properly, we are ready to merge the PDF files.

Merging the PDF Files

[Download Code]

Add the following statement to the top of our code-behind file:

Imports ceTe.DynamicPDF
Imports ceTe.DynamicPDF.Merger

Un-comment both calls to GenerateReport(), but comment out the Response.Redirect statements, as so:

  Dim strExportFile1, strExportFile2 As String

  strExportFile1 = GenerateReport("customers")

  strExportFile2 = GenerateReport("suppliers")

The above change will create two PDFs, but not redirect to either of them.  To merge the two PDFs, add the following lines of code to the end of page_load:

  ' Create a merge document and set a few properties
  ' Code adapted from DynamicPDF Help File
  Dim MyDocument As MergeDocument = MergeDocument.Merge(MapPath(strExportFile1), MapPath(strExportFile2))
  MyDocument.Creator = "Asp Alliance"
  MyDocument.Author = "Richard J. Dudley"
  MyDocument.Title = "Merging PDFs From Crystal Reports"

  ' Following line of code merges the two files and streams output to browser
  ' no file is saved on the server's hard drive
  MyDocument.DrawToWeb(Me, "MergedReport.PDF")

Recompile your project and load the default page.  You should see a single PDF with both reports in it.

DrawToWeb() directs output directly the the browser--no file is created on your server's disk.  If you want to save the file on your server, use the Draw() method, and then use Response.Redirect to send the file to the browser (or set the source of a download link).

Adding Password Protection

[Download Code]

In addition to merging two or more PDFs, we can also add security to our PDF files.

Add the following code to the page_load, before the call to DrawToWeb:

  ' Create a 128 bit encryption security object that prevents text copying.
  ' owner password = "owner"
  ' user password = "user"
  ' users can open the document, but cannot copy information
  Dim MySecurity As HighSecurity = New HighSecurity("owner", "user")
  MySecurity.AllowCopy = False
  ' Add the security object to the document
  MyDocument.Security = MySecurity

Recompile your project, and load the default page.  You should be prompted to enter a password when Acrobat Reader loads.  Enter the user password, and try to copy some text--you should not be able to copy any text.  Close the document, and reload the default page.  This time, enter the owner password.  You should be able to copy text.

There are a number of other document properties that can be protected with the HighSecurity class.  These include:

AllowEdit - controls whether a document can be edited by the user

AllowFormFilling - controls whether a PDF form can be filled in by the user

AllowPrint - controls whether a user can print a PDF

AllowHighResolutionPrinting - allows low-resolution printing of a PDF by the user, but not high-resolution printing

Additional properties and usages are documented in the CHM file that is included in the Dynamic PDF download.


[Download Code]

Merging two or more reports increases the flexibility a developer has in how the final output appears.  One could even use this technique to piece together several reports in different order as the user specifies, to allow a high degree of customization.

The ability to password protect a Crystal Report has great utility in distributing confidential information.  Since the ability of a user to print or edit the report can be controlled, a Crystal Report could include sensitive information without fear of it being compromised.

Although the options for creating PDFs provided by Crystal Reports are limited, a developer does not have to do without.  Commercially available products can be used to easily merge and add properties to one or more PDFs after being created by Crystal Reports.

About the Code Download

The code download contains the default.aspx page, both Crystal Reports (.RPT) files, and their code-behind pages.  To use the code, download and un-zip the archive.  Create an ASP.NET project, and use the 'Add Existing Item' functionality to add the files to your project.

You may receive a data error if the Xtreme database is installed in a path other than the one described above.  You can change the location of the database by opening the Field Explorer, expanding the Database node, and right-clicking on the database.  From the menu, choose "Change Location".  Navigate to the correct location, and click OK.  The new fields should automatically map to the old fields.

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-12-05 12:22:38 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search