Unlock Password-Protected PDF
This guide will show you how to use UniPDF to unlock a PDF document. This guide tries to decrypt the protected PDF using the provided password first, then an empty password as the last resort.
Before you begin
You should get your API key from your UniCloud account.
If this is your first time using UniPDF SDK, follow this guide to set up a local development environment.
Clone the project repository
In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.
git clone https://github.com/unidoc/unipdf-examples.git
Navigate to the
security folder in the unipdf-examples directory.
Configure environment variables
UNIDOC_LICENSE_API_KEY with API credentials from your UniCloud account.
How it works
10-16 import the UniPDF packages and other required dependencies.
18-25 authenticate your request with your
UNIDOC_LICENSE_API_KEY with the init function.
The main function in lines
27-44 validates your input and passes it as arguments to the unlockPdf function.
46-84 define the
unlockPdf function, which accepts three parameters: inputPath, outpathPath, and password. The pdfReader reads the PDF from the inputPath and determines whether or not it is encrypted. If the PDF document is encrypted, the function attempts to decrypt it using the provided password and writes the decrypted PDF document to the outputPath using pdfWriter.
Run the code
Run this command to unlock the password-protected PDF. This will also get all the required dependencies to run the program.
You will get a decrypted version of the input PDF in your current directory. You will be able to access the document without passwords.