Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
This is a PDFOne Java version of PDFOne .NET article published last year.
Here is what we mentioned in that article.
An encrypted PDF document places certain restrictions on how the document can be used - to open it, to modify it, to print it, to copy content, etc... An encrypted document may require either one of two passwords - owner or user password. If you provide the owner password, you have full access to the document including the ability to specify new password and change document restrictions. If you provide the user password, you will be able to open the document but may be subject to restrictions placed by the owner of the document. If a document creator encrypted a document without specifying a user password, then anyone can open the document without providing a password althought document restrictions would still apply.
In this article, we will see how to password-protect PDF documents and also how to read an encrypted PDF document.
While creating an encrypted PDF document, we will need to use the PdfEncryption
class extensively. The PdfDocument.getEncryptor()
method returns a PdfEncryption
object. By setting its properties and then applying the modified PdfEncryption
to the document using the PdfDocument.setEncryptor()
method, a document can be encrypted.
While reading an encrypted document, we will need to implement the PdfPasswordHandler
interface. We will have to provide the password when we implement the PdfPasswordHandler.onPassword()
event. This event will be called when we try to create a PdfDocument
object for the encrypted document.
import java.io.IOException; import com.gnostice.pdfone.PDFOne; import com.gnostice.pdfone.PdfDocument; import com.gnostice.pdfone.PdfEncryption; import com.gnostice.pdfone.PdfException; import com.gnostice.pdfone.PdfPasswordHandler; import com.gnostice.pdfone.PdfReader; import com.gnostice.pdfone.PdfWriter; public class September_2008_Encryption implements PdfPasswordHandler { static { PDFOne.activate( "your-activation-key", "your-product-key"); } public static void main(String[] args) throws PdfException, IOException { createEncryptedDoc(); readEncryptedDoc(); } static void createEncryptedDoc() throws IOException, PdfException { // Create a blank document PdfDocument document = new PdfDocument(); // Create a document encryption object PdfEncryption crypt = document.getEncryptor(); // Set owner and user passwords crypt.setOwnerPwd("Gnostice"); crypt.setUserPwd("PDFOne"); // Set encryption key length to 128 bits crypt.setLevel(PdfEncryption.LEVEL_128_BIT); // Specify document restrictions crypt.setPermissions( PdfEncryption.AllowPrinting | PdfEncryption.AllowHighResPrint); // Write text to default page 1 of the document document.writeText( "This document is encrypted. " + "The user password is PDFOne. " + "The owner password is Gnostice."); // Apply encryption settings to the document document.setEncryptor(crypt); // Write the document to file document.save("encrypted_doc.pdf"); document.close(); } static void readEncryptedDoc() throws IOException, PdfException { PdfDocument document = new PdfDocument(); try { document.setOnPasswordHandler( new September_2008_Encryption()); // Load the document document.load("encrypted_doc.pdf"); // Display the number of pages after // reading from the decrypted document System.out.println( "Success: Password is correct and the file has " + document.getPageCount() + " page(s)."); } catch (PdfException e) { System.out.println("Failure: " + e.getMessage()); } finally { document.close(); } } public String onPassword( PdfDocument d, boolean[] flags) { return "PDFOne"; // Return user password // return "Gnostice"; // Return owner password } }
---o0O0o---
Our .NET Developer Tools | |
---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
Our Delphi/C++Builder developer tools | |
---|---|
Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
Our Java developer tools | |
---|---|
Gnostice Document Studio JavaMulti-format document-processing component suite for Java developers. |
PDFOne (for Java)A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications. |
Our Platform-Agnostic Cloud and On-Premises APIs | |
---|---|
StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2025 Gnostice Information Technologies Private Limited. All rights reserved. |