• Home
  • /
  • Blog
  • /
  • Remove Password from PDF – ByteScout PDF Protection Tutorial

Remove Password from PDF – ByteScout PDF Protection Tutorial

Remove Password from PDF

Creating PDF with ByteScout is quite a simple task. It needs to add ByteScout PDF SDK in your project at first, and include its namespace in your .cs extension file where you are looking to create a PDF file. i.e. using Bytescout.PDF.

Go through the short tutorial which will create a sample PDF and adding password to it. The PDF file can be created by creating the object of Document class and calling its default constructor.

 Document pdfDocument = new Document();

After this, you can set multiple properties of that document for example name and key.

pdfDocument.RegistrationName = "demo";

pdfDocument.RegistrationKey = "demo";

Now add pages to the newly created PDF file by the following code.

pdfDocument.Pages.Add(new Page(PaperFormat.A4));

For protection, you can set the encryption type of the PDF document. This is done by following lines of code.

pdfDocument.Security.EncryptionAlgorithm = EncryptionAlgorithm.RC4_40bit;

PDF document has two types of password, one is the owner’s password and other is client/user’s password. ByteScout PDF SDK allows you to set both types of passwords to your PDF document.

Following lines of code will set both owner’s and user’s password to the document.

pdfDocument.Security.OwnerPassword = "ownerpassword";

pdfDocument.Security.UserPassword = "userpassword";

Lastly, you can save the newly created PDF by .Save() method.

pdfDocument.Save("result.pdf");

See below sample in C# which demonstrates the creation of PDF document with protection and also set passwords to it.

using System.Diagnostics;

using Bytescout.PDF;

namespace PasswordsAndPermissions

{

        /// <summary>

    /// This example demonstrates how to create a PDF document with a Protection.

    /// PDF format supports two kinds of passwords: owner and user password.

    /// User password allows to view document and perform allowed actions.

    /// Owner password allows everything, including changing passwords and permissions.

    /// </summary>

   class Program

    {

        static void Main()

        {

            // Create new document

            Document pdfDocument = new Document();

            pdfDocument.RegistrationName = "demo";

            pdfDocument.RegistrationKey = "demo";

            // Add page

            pdfDocument.Pages.Add(new Page(PaperFormat.A4));

 

            // Set document encryption algorythm

            pdfDocument.Security.EncryptionAlgorithm = EncryptionAlgorithm.RC4_40bit;

 

            // Set owner password

            pdfDocument.Security.OwnerPassword = "ownerpassword";

 

            // Set user password

            pdfDocument.Security.UserPassword = "userpassword";

 

            // Save a document to file

            pdfDocument.Save("result.pdf");

 

            // Cleanup

            pdfDocument.Dispose();

 

            // Open document in default PDF viewer app

            Process.Start("result.pdf");

        }

    }

}

 

About the Author

ByteScout Team

ByteScout Team of Writers

ByteScout has a team of professional writers specialized in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.

 

 

Try Free SQL Trainer - learn by doing!
Data Playround - upload CSV and sort, filter, view data using natural language.
prev
next