How to Generate an SSRS Report as a Password-Protected PDF Using the Bold Reports Report Writer
Tags: netcore, export, pdf, security
Published: 2023-05-04 05:44:00

SSRS (SQL server reporting services) is a powerful tool that enables users to create, deploy, and manage reports across an organization. However, there may be times when you need to secure sensitive information contained in your SSRS report by generating a password-protected PDF. In this blog post, we will show you how to convert an SSRS report into a password-protected PDF using the Bold Reports Report Writer.

The [Bold Reports Report Write](https://www.boldreports.com/embedded-reporting/aspnet-core-report-writer/?utm_source=hashnode&utm_hashnode=partner_blog&utm_campaign=dev_ssrsreportwriter_bpexternal)r is a library that enables developers to programmatically create and export reports in various formats including PDF, Excel, and DOCX. With its ability to generate password-protected PDFs, you can ensure that your sensitive information is secure while still delivering high-quality reports to your users.

To get started, let’s assume you have an existing SSRS report that you want to export as a password-protected PDF. Here’s how you can do that using the Bold Reports Report Writer for .NET apps.

**Prerequisites**
Ensure your development environment includes the following:

. Visual Studio 2022
. .NET 6.0+

**Step 1: Create a .NET console application.**
1. Open Visual Studio 2022 and click** Create a new project.**

![This blog explains how to generate an SSRS Report as a password-protected PDF using the Bold Reports Report Writer in a .NET application | Embedded Reporting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rbrq0goyxiuo9pffs0c.jpg)Create a new project

2. Choose the .NET **Console app **with C# and click **Next**.

![This blog explains how to generate an SSRS Report as a password-protected PDF using the Bold Reports Report Writer in a .NET application | Embedded Reporting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjwme71hxyqrbuf6iykq.jpg)Create a .NET console app

3. Provide a project name, click Next, select the .NET version, and click Create. The default .NET console app is created.

![This blog explains how to generate an SSRS Report as a password-protected PDF using the Bold Reports Report Writer in a .NET application | Embedded Reporting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obq77ryldxb0fhk4tm3d.jpg).NET console app

**Step 2: Install the Bold Reports Report Writer library NuGet package**.

To use the Bold Reports Report Writer library in your project, you need to install the BoldReports.Net.Core NuGet package. You can do this by opening the Package Manager Console in Visual Studio and running the following command.

```
Install-Package BoldReports.Net.Core

```

![This blog explains how to generate an SSRS Report as a password-protected PDF using the Bold Reports Report Writer in a .NET application | Embedded Reporting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzwll0bykhpll0t3r26d.jpg)

**Step 3: Add the namespaces related to the Bold Reports Report Writer**.

In the Program.cs file, add the following namespaces at the top of the file.

```
using BoldReports.Writer;
using BoldReports.Web;
```
**Step 4: Create a ReportWriter instance**.

Create a ReportWriter instance and set its properties as shown in the following.

```
ReportWriter writer = new ReportWriter();
writer.ReportProcessingMode = ProcessingMode.Remote;
writer.ReportServerUrl = "http://192.1.168.2/ReportServer";
writer.ReportPath = "/Report_Path";
writer.ReportServerCredential = new System.Net.NetworkCredential("username", "password");
```
Replace the **ReportServerUrl **property with your SSRS report server URL. Also, replace the Report_Path, username, and password with your own values.

**Step 5: Set the data source credentials to the writer**.

If your SSRS report uses data sources that require credentials, you need to set the data source credentials to the writer. In the following code, replace the Datasource_name, username, and password with your own values.

```
List<DataSourceCredentials> dataSourceCredentialsList = new List<DataSourceCredentials>();
dataSourceCredentialsList
    .Add(new DataSourceCredentials("Datasource_name", "username", "password"));
writer.SetDataSourceCredentials(dataSourceCredentialsList);
```
**Step 6: Create a password-protected PDF**.

To create a password-protected PDF, you need to use the Report Writer’s PDFOptions property. The following steps achieve this.

1. In the Program.cs file, include the namespace Syncfusion.Pdf.Security.

2. Add the following lines in the Program.cs file.

```
writer.PDFOptions = new PDFOptions();
writer.PDFOptions.Security = new PdfSecurity();
writer.PDFOptions.Security.UserPassword = "password";
```
This code sets the **UserPassword** property of the **PdfSecurity** class to the desired password. You can also set the **OwnerPassword** property to set a separate password for the owner of the PDF file.

3. Next, you can generate the password-protected PDF by calling the Report Writer’s Save method by passing PDF as the export format.

```
MemoryStream pdfReport = new MemoryStream();
writer.Save(pdfReport, WriterFormat.PDF);
```

4. Save the PDF stream as a file after generating the password-protected PDF.

```
using (FileStream file = new FileStream("C:\\Reports\\Report1.pdf", 
    FileMode.Create, System.IO.FileAccess.Write))
{
  pdfReport.WriteTo(file);
}

```
Replace “C:\\Reports\\Report1.pdf” with the desired file path and file name.

5. Run the application by pressing F5. You can see the generated password-protected PDF file in the provided path location. You can open the PDF file after entering the password.


![This blog explains how to generate an SSRS Report as a password-protected PDF using the Bold Reports Report Writer in a .NET application | Embedded Reporting](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ntxm52kgj0dam9olh08.jpg)Password-protected PDF file

That’s it! You have now generated an SSRS report as a password-protected PDF using the Bold Reports Report Writer. Following is the complete code for reference.

```
using BoldReports.Writer;
using BoldReports.Web;
using Syncfusion.Pdf.Security;
using System.Net;

Console.WriteLine("Hello, World!");

ReportWriter writer = new ReportWriter();
writer.ReportProcessingMode = ProcessingMode.Remote;
writer.ReportServerUrl = "http://192.1.168.2/ReportServer";
writer.ReportPath = "/Report_Path";
writer.ReportServerCredential = new System.Net.NetworkCredential("username", "password");

writer.PDFOptions = new PDFOptions();
writer.PDFOptions.Security = new PdfSecurity();
writer.PDFOptions.Security.UserPassword = "password";

//Set the data source credentials.
List<DataSourceCredentials> dataSourceCredentialsList = new List<DataSourceCredentials>();
dataSourceCredentialsList
    .Add(new DataSourceCredentials("Datasource_name", "username", "password"));
writer.SetDataSourceCredentials(dataSourceCredentialsList);


// Generate a password-protected PDF.
MemoryStream pdfReport = new MemoryStream();
writer.Save(pdfReport, WriterFormat.PDF);

// Save the PDF stream as a file.
using (FileStream file = new FileStream("C:\\Reports\\Report1.pdf", 
    FileMode.Create, System.IO.FileAccess.Write))
{
    pdfReport.WriteTo(file);
}
```
Visit: https://www.boldreports.com/contact/?How to Generate an SSRS Report as a Password-Protected PDF Using the Bold Reports Report Writer
**Conclusion**
I hope this blog provided sufficient guidance to generate an SSRS report as a password-protected PDF document in a .NET application. To learn more about the Report Writer for .NET and its report items, look through our [documentation](https://help.boldreports.com/embedded-reporting/aspnet-core-reporting/report-writer/export-ssrs-rdl-report/?utm_source=hashnode&utm_hashnode=partner_blog&utm_campaign=dev_ssrsreportwriter_bpexternal). To experience the features live, check out our [demo samples](hashnode).

If you have any questions, please post them in the comments section below. You can also contact us through our [contact page](hashnode), or if you already have an account, you can [log in](hashnode) to ask your question.

Bold Reports offers a [15-day free trial ](hashnode)without any credit card information required. We welcome you to start a free trial and experience Bold Reports for yourself. Try it and let us know what you think!

Catch us on Twitter, Facebook, and LinkedIn for info about upcoming releases.