Merging and Password Protecting PDFs Exported by Crystal Reports
 
Published: 30 Nov 2004
Unedited - Community Contributed
Abstract
Crystal Reports has the ability to produce output in PDF format. Often, it is desirable to have multiple reports appear in the same PDF file. Crystal Reports does not have the ability to export multiple reports into the same PDF, or to password protect a PDF, but several third party components can process PDFs post-export from Crystal Reports. We'll use one .NET control named Dynamic PDF Merger to combine two PDFs into a single file, and return the single file to the end user.
by Richard Dudley
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 82469/ 109

Introduction

[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")
  Response.Redirect(strExportFile1)


  'strExportFile2 = GenerateReport("suppliers")
  'Response.Redirect(strExportFile2)

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
  MyReport.Export()
  MyReport.Close()


  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")
  'Response.Redirect(strExportFile1)


  strExportFile2 = GenerateReport("suppliers")
  'Response.Redirect(strExportFile2)

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.

Conclusion

[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.



User Comments

Title: Bookmark   
Name: Kirk
Date: 2010-02-05 7:36:29 AM
Comment:
How do you add bookmarks?
Title: You don't need to shell out on commercial components   
Name: Zarkonia
Date: 2008-05-28 8:09:20 PM
Comment:
Hi All,

Take a look at Pdftk...

Pdftk is free software (GPL) and it runs on Windows, Linux, Mac OS X, FreeBSD and Solaris.

It's a command line app - but with careful use of the SHELL command works like a charm in classic VB or the System.Diagnostics namespace in the .NET world.

I recommend it...
Title: Lib. Demo   
Name: XTemplar
Date: 2007-10-22 12:10:18 AM
Comment:
Dear Richard,
1.)May i have the demo. lib ??
My email: XTemplar@gmail.com
2.)What PDF version it support? All?
3.)What's the charges for it if it's suitable for me?

Thanks,
- XTemplar
Title: Problem for Merge PDF   
Name: Shekh Mohasin
Date: 2007-03-15 12:49:38 AM
Comment:
Hi Richard,
i have try ur code for Merage two PDF file, but in ur sources code i not found "ceTe" Libray file which is very imp it code to Mearge so, can u pls send me the Libray file
Imports ceTe.DynamicPDF
Imports ceTe.DynamicPDF.Merger

my MailId is
shekhmohasin@gmail.com

Thanks,
Shekh Mohasin
Title: SE   
Name: Yathish
Date: 2007-02-19 1:28:42 AM
Comment:
Hi Richard,

Is there any mechanism to open rpt files directly in Acrobat Reader instead of Crystal Report Viewer

Thanx,

Yathish M.P
Title: Passwords   
Name: Debbie
Date: 2006-12-28 3:53:25 PM
Comment:
What is the syntax for passing the password to the merger?
Title: How is it better than PDFLib for .Net   
Name: Richard Dudley
Date: 2006-10-26 9:31:09 PM
Comment:
Tech4U,

Only one way to find out--download the demo version and try it! "Load" depends on how large your reports are, and how your application is coded.
Title: How is it better than PDFLib for .Net   
Name: Tech4U
Date: 2006-10-24 2:07:32 PM
Comment:
Hi,
I've an existing app - Windows service, which generates multiple PDF files (Data retrieved from cubes and then Exported to pdf using Crystal) and PDFLib merges all the pdf files together into one file, but the problem is, as mine is a multithreaded app, the PDFLib fails badly and it allows me to select only a couple of Reports(which contains individual reports) but I want to allow user to select more than 5 reports or so, so that they can get all the 5 PDF files (including 100-200 individual pdf files). Can Dynamic PDF Merger handle this kind of load.
Title: Need Information about Continous PAging while merging reports   
Name: Brijesh (brijeshs@nous.soft.net)
Date: 2006-09-07 5:34:36 AM
Comment:
Hi Richard,
I have gone through your article of merging two crystal reports and its really very interesting.I just want to know that if we are using page numbers on both reports so while merging Reports whether the continous paging will happen.

In other words, if 1st report is having 5 pages and the second report is having 4 pages. When we merge these two reports, whether the second report will start from 6th page or Is there any option/setting that will make the second report to start from 6th page not from 1st page.

Any help would be appreciated.

Thanks
Brijesh Singh
Title: PW Protecting XML Files   
Name: Richard Dudley
Date: 2006-06-09 11:09:36 AM
Comment:
This really isn't the best place for that question, since this is an article on PDFs.

Since XML files are just text files, there's really not a good way to password protect them in the file system. Th ebest you could to would be to encrypt them with DPAPI somehow, or some other reversible encryption scheme.
Title: I have gone through the article and found to be interesting. I too have a problem and hope your component have the support feature. I have a requireme   
Name: I have gone through the article and found to be interesting. I too have a problem and hope your component have the suppo
Date: 2006-06-09 7:30:33 AM
Comment:
I have gone through the article and found to be interesting. I too have a problem and hope your component have the support feature. I have a requirement where I should password protect the xml files. This feature should be used from the .net desktop application. The software which I am developing for a client should use/read the xml files from the application. But these files should be password protected from the external drives/location, so that no one should be able to open these xml files.
Title: Reports with ASP   
Name: Richard Dudley
Date: 2006-03-27 1:32:48 PM
Comment:
Kevin,

You can generate Crystal Reports with ASP, but not in this example. This example uses the version bundled with Visual Studio .NET, and you'd have to use a .NET language.

To work in ASP, you'd need the retail version of Crystal Reports. There are tutorials available in the help docs and on the Business objects website.
Title: Generate report in ASP   
Name: Kevin
Date: 2006-03-27 3:14:41 AM
Comment:
Hi,

I wanted to know if it's possible to generate the report in asp instead of .net. And how to do it?

Thanks,

Kevin
Title: Add PW On Open   
Name: Richard Dudley
Date: 2005-11-28 9:31:36 AM
Comment:
Circle,

This article shows you how to add a password to a PDF exported from Crystal Reports. If you add a user password, the end user will be required to enter the password when the file is opened. You can use the same component I did, you just set a different property.
Title: Password protection on PDF File   
Name: Circle
Date: 2005-11-27 11:57:26 AM
Comment:
I m new on .Net & crystal report. Have develop project in Window Form and have to produce report in PDF format ultimately. So i prepare a crystal report and plan to use export function to generate the PDF file, but user request password protection on open the PDF file. Would like to have your advice.
Title: 3rd-party alternative (Visual CUT)   
Name: Ido Millet
Date: 2005-11-22 6:04:03 PM
Comment:
For those who are looking for a 3rd-party solution to do the above and more, my Visual CUT software can automate the exporting of Crystal reports to PDFs, the merging of these PDF files (including the merging of any bookmark information) and, optionally, adding page numbers, a table of contents, and password protection to the resulting merged pdf file.

Cheers,
- Ido
www.MilletSoftware.com
Title: Protecting XML Files   
Name: Richard Dudley
Date: 2005-10-24 3:19:27 PM
Comment:
Mallikarjun,

This has nothing to do with protecting XML files. Since XML files are plain text, they really can't be protected completely. One option might be to store the XML as an encrypted string, which you decrypt when you need to process the XML. If you have questions on how to accomplosh that, try the forums at http://asp.net.
Title: Project Manager   
Name: Mallikarjun
Date: 2005-10-22 11:28:03 PM
Comment:
Hi,

I have gone through the article and found to be interesting. I too have a problem and hope your component have the support feature. I have a requirement where I should password protect the xml files. This feature should be used from the .net desktop application. The software which I am developing for a client should use/read the xml files from the application. But these files should be password protected from the external drives/location, so that no one should be able to open these xml files.

Please assist me if your component has this feature.

Thanks in advance.
Title: Dynamic PDF merger   
Name: Shilpa
Date: 2005-10-13 11:41:27 PM
Comment:
Hi Richard,
I know you are discussing about different scenario of generating PDF files.But with my experience I don't prefer to use DynamicPDF merger.If you generate a PDF file some how and a add any kind of password to it and try to merge it with another PDF.It gives error!
Title: Dynamic PDF Merger   
Name: Richard Dudley
Date: 2005-10-05 8:12:55 PM
Comment:
Shilpa,

PDF documents created by Crystal Reports won't be password protected just ater they are created.

Perhaps your problem is in the way you are using the component? Are you supplying the password?
Title: Some problem with Dynamic PDF merger   
Name: Shilpa
Date: 2005-10-05 1:53:06 PM
Comment:
You article is good.But there is a problem in Dynamic PDF MERGER COMPONENT.
If any of the PDF document is already protected with password then you cannot merge them. It shows an error: "Document can not be decrypted. Invalid owner password."

Product Spotlight
Product Spotlight 



Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-05 11:39:57 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search