How to сonvert a password protected workbook to PDF in C# and .NET


In today's world of business communications, exchanging data in PDF format is becoming a standard due to its readability and versatility. However, it is often necessary to convert a password-protected Excel workbook to PDF while preserving all important data and ensuring the security of the original data. In this article, we will take a detailed look at how to implement automated conversion of a password-protected Excel workbook to PDF using the Excel to PDF .NET from API SautinSoft in C# and .NET. We will discuss the advantages of this method, its practical value, and areas of application.

The importance of converting protected Excel workbooks to PDF:

  1. Data protection and ease of distribution.
    Excel files often contain sensitive data, some of which is password-protected. However, for sharing or archiving, it is sometimes necessary to present this data in a more universal and secure format—PDF. It is important to preserve the original data and document structure, and consider the level of security.
  2. Automation and Process Optimization.
    Manually removing protection and exporting to PDF takes time and resources, especially when processing large numbers of files. Automating such processes reduces human error, reduces human error, and achieves high productivity.
  3. Workflows and Frequent Use.
    This scenario is in demand in automated reporting systems, archiving systems, and document management systems, where quick data conversion between formats is required.

What is this code useful for?

  • Reporting automation. Generate PDF versions of protected Excel reports for external clients or internal use.
  • Data archiving. Convert password-protected workbooks to PDF for secure storage.
  • Integration into corporate systems. Embed automatic conversions into business processes and ERP systems.
  • Ensuring compliance. Create PDF documents with preserved structure and protection to comply with standards and security policies.

Input file:

convert password protected workbook to pdf input

Intermediate stage:

convert-password-protected-workbook-to-pdf-intermediate

Output result:

convert-password-protected-workbook-to-pdf-output

Start automating today — and always keep your documents under control!

Complete code

using SautinSoft;
using System;
using System.IO;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Convert a password protected workbook
            ExcelToPdf x = new ExcelToPdf();

            // Set PDF as output format.
            x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Pdf;

            // Set the user password for restricting access to opening the PDF file.
            x.Options.PdfSecurity.UserPassword = "qwerty";

            // Set the owner password for controlling permissions for editing, printing, and other modifications.
            x.Options.PdfSecurity.OwnerPassword = "";

            string excelFile = Path.GetFullPath(@"..\..\..\test.xlsx");
            string pdfFile = Path.ChangeExtension(excelFile, ".pdf"); ;

            try
            {
                x.ConvertFile(excelFile, pdfFile);
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(pdfFile) { UseShellExecute = true });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }

        }
    }
}

Download

Imports SautinSoft
Imports System
Imports System.IO

Namespace Sample
	Friend Class Sample
		Shared Sub Main(ByVal args() As String)
			' Convert a password protected workbook
			Dim x As New ExcelToPdf()

			' Set PDF as output format.
			x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Pdf

			' Set the user password for restricting access to opening the PDF file.
			x.Options.PdfSecurity.UserPassword = "qwerty"

			' Set the owner password for controlling permissions for editing, printing, and other modifications.
			x.Options.PdfSecurity.OwnerPassword = ""

			Dim excelFile As String = Path.GetFullPath("..\..\..\test.xlsx")
			Dim pdfFile As String = Path.ChangeExtension(excelFile, ".pdf")


			Try
				x.ConvertFile(excelFile, pdfFile)
				System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(pdfFile) With {.UseShellExecute = True})
			Catch ex As Exception
				Console.WriteLine(ex.Message)
				Console.ReadLine()
			End Try

		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.