In the document wars Adobe’s PDF format seems to have won. Because they give away their Adobe Reader for free it is probably the most widely distributed document format and reader except for maybe MS-Word documents. New in XtraReports Suite v2009, volume 3 is the ability to password protect documents exported in the PDF format.
In short you can print an XtraReport and select various formats. If you select Print and Export from out demos (see Figure 1) or click the File|Export Document from the report previewer (see Figure 2) and pick the PDF format then the PDF Export Options dialog will displayed (see Figure 3). The PDF Export Options dialog has a Password Security input field. Click the ellipses to open the Password Security dialog (see Figure 4).Check Require a password to open the document and provide a password. The user will be required to repeat the password after closing the Password Security dialog (see Figure 6). If you want an additional password for changing the document then check “Restrict editing and printing of the document” and provide an additional password.
When the user attempts to open a secured document in Adobe Reader the password prompt will be displayed, and the reader will be required to provide a password. The Adobe Reader title will indicated—with the word SECURED—in the title bar when a document is password protected. Programmatically exporting a password report to PDF format couldn’t be easier—design the report, create an instance of the XtraReport object, set the XtraReport.ExportOptions.Pdf.PasswordSecurityOptions.OpenPassword and set this property with the password value (see Listing 1). [Note: if you are going to embed password information in your code then you might want to use a tool like Dotfuscator to obfuscate your code, protecting it from a disassembler.]
Listing 1: Two lines of code is all it takes to export a password protected PDF report created with XtraReports.
using System;
using System.Windows.Forms;
namespace PDFPasswordSecurity
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
XtraReport1 report = new XtraReport1();
private void button1_Click(object sender, EventArgs e)
{
report.ExportOptions.Pdf.PasswordSecurityOptions.OpenPassword = "password";
report.ExportToPdf("c:\\temp\\demo.pdf");
}
}
}
Figure 1: The Products List demo report exposes the export feature from the Print and Export menu.
Figure 2: The default previewer supports exporting (and mailing) from the File menu.
Figure 3: The PDF Export Options dialog lets you specify a password for tthe PDF document to be exported.
Figure 4: Check the Require a password to open the document and provide a password in the Document Open Password field.
Figure 5: For a separate password for editing permissions check Restrict editing and printing of the document and provide an additional password.
Figure 6: The user will be asked to confirm the password after you click OK on the Password Security dialog.
Figure 7: Adobe Reader will prompt the user and require the correct password before opening a password protected document.
Figure 8: The protected document in Adobe Reader shows the the document is SECURED in the window title bar.