Generate A Strongly Secured Encrypted PDF File Using PDFFileEcrypter

In this work, I will present a new approach for generating encrypted and secure pdf files using a custom html helper for creating a form for sending files via a post method.

This custom HTML helper tag belongs to the PDFFileEncrypter plugin that I published in Nuget Packages.

It is a very simple method based on a ready to use tag called "FerdawsForm" (Ferdaws is my little daughter. :) ).

Step 1

Let's create new ASP.NET MVC project in visual studio " TestPDFEncrypted".

Generate a strongly secured encrypted PDF file with a password using my PDFFileEcrypter NuGet Package

Step 2

Add "PDFFileEncrypter" plugin from Nuget Package Manager.

Or just run this command,

NuGet\Install-Package PDFFileEncrypter -Version 1.0.0

Generate a strongly secured encrypted PDF file with a password using my PDFFileEcrypter NuGet Package

Step 3

Change the "Index.cshtml" view to add @Html.FerdawsForm("/Home/Index").

@using PDFEncrypter;
@{
    ViewBag.Title = "Home Page";
}
@Html.FerdawsForm("/Home/Index")

The first line is to call the PDFEncrypter namespace.

Line number 6 allows the creation of a form calling the "Index" action in the Home controller.

Step 4

 Add the code bellow to the Home Controller.

public ActionResult Index() {
    return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file, string nom) {
    string s = CustomHelpers.Save(file, nom, "abcd", @ "C:\\Users\\dell\\Desktop\\LIB", "csharpTest");
    CustomHelpers.Generate("abcd", s, @ "C:\\Users\\dell\\Desktop\\LIB");
    return View();
}

 

 string s = CustomHelpers.Save(file, nom, "abcd", @"C:\\Users\\dell\\Desktop\\LIB", "csharpTest");

The Save() method allows saving the file after encrypting and securing it in a folder of the user's choice.

Allows to generate a PDF file after decrypting and checking the given password.

-  allows to save the file after having encrypted and secured it in a folder of the user's choice.
> @"C:\\Users\\dell\\Desktop\\LIB": represents the full path of the file you want to encrypt.
> password: represents the password used to encrypt and/or decrypt the file.
> folderWhereToSave : represents the folder where you want to save the encrypted file.

The Generate() method allows to generate a PDF file after decrypting and checking the given password.

- allows to generate a PDF file after decrypting and checking the given password.      
> password: represents the password used to encrypt and/or decrypt the file.
> s: represents the full path of the encrypted file you want to decrypt
> folderWhereToSave : represents the folder where you want to save the decrypted file.

Result:

Generate a strongly secured encrypted PDF file with a password using my PDFFileEcrypter NuGet Package

Generated files

Generate a strongly secured encrypted PDF file with a password using my PDFFileEcrypter NuGet Package

  • is the file result of the Save method.
  • is the file result of Generate method.

NB: You can download all project source code from my github page.

Conclusion

I think the procedure is very clear with snapshots. If you have found any mistake in concept, please do comment.

Your comments will make me perfect in the future.

Thanks for reading.


Similar Articles