Encrypting PDF with a Password

If you are developer and looking for a way to put a password on a PDF file for free, you may want to look into qpdf command line tool. You can install this in Linux and Windows as well.

I develop using c# and was able to utilize this tool inside the program that I was doing. You can actually use this in any kind of programming language by just calling it through a command line interface.

Please see code snippet below.

public static string qpdfEncrypt(string inFile, string outFile, 
     string password)
     using (System.Diagnostics.Process pProcess = new 
        pProcess.StartInfo.FileName = 
           + @"\\qpdf\\qpdf.exe";
        pProcess.StartInfo.Arguments = $"--encrypt {password} 
           {password} 256 -- \"{inFile}\" \"{outFile}\"";
        pProcess.StartInfo.UseShellExecute = false;
        pProcess.StartInfo.RedirectStandardOutput = true;
        pProcess.StartInfo.RedirectStandardError = true;
        pProcess.StartInfo.WindowStyle = 
        pProcess.StartInfo.CreateNoWindow = true;
        string output = pProcess.StandardError.ReadToEnd();
        return output;

As you can see, the code just opens the process command interface and calls the qpdf executable. You can actually use the same code in calling any kind of command line utility that you want to integrate in your code.

Happy Coding!

Share the love...