DEV Community

Cover image for How to Generate an SSRS Report as a Password-Protected PDF Using the Bold Reports Report Writer
BarbraWeke for Bold Reports

Posted on • Edited on • Originally published at boldreports.com

How to Generate an SSRS Report as a Password-Protected PDF Using the Bold Reports Report Writer

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 Writer 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 ReportingCreate a new project

  1. 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 ReportingCreate a .NET console app

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



Enter fullscreen mode Exit fullscreen mode

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

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;


Enter fullscreen mode Exit fullscreen mode

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");


Enter fullscreen mode Exit fullscreen mode

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);


Enter fullscreen mode Exit fullscreen mode

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";


Enter fullscreen mode Exit fullscreen mode

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.

  1. 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);


Enter fullscreen mode Exit fullscreen mode
  1. 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);
}



Enter fullscreen mode Exit fullscreen mode

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

  1. 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 ReportingPassword-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);
}


Enter fullscreen mode Exit fullscreen mode

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. To experience the features live, check out our demo samples.

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

Bold Reports offers a 15-day free trial 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.

Top comments (0)