Creating a 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 a password to it. The PDF file can be created by creating the object of the 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 the other is the client/user’s password. ByteScout PDF SDK allows you to set both types of passwords to your PDF document.
The following lines of code will set both owner’s and user’s passwords 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 us to view the 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");
}
}
}