Tuesday, March 18, 2014

PDF password protection

In this tutorial, I am going to show you how to protect a PDF document by passwords so that users that do not have permissions cannot open or modify the content of the PDF document. You can set two passwords to the PDF document,  a user password and an owner password. The user password must be entered before the PDF document can be opened in Adobe Reader or Foxit Reader. The owner password applies restrictions to the PDF document. For example, you can  allow only printing and copying to the PDF document.
By using iText library you can set the passwords to the PDF document by using the setEncryption method of PdfWriter class. The general form of the setEncryption method is shown below:

setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType)

- userPassword--an array of bytes that is the user password.

- ownerPassword--an array of bytes that is the owner password.

- permissions--permissions or restrictions that will be applied to the PDF document. These permissions can be AllowPrinting, AllowCopy, AllowScreenReaders, AllowFillIn, AllowAssembly, or AllowDegradedPrinting.
- encryptionType--the type of encryption. The encryption type can be one of the following STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.

Example:

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;



public class PdfItext {
public static void main(String[] args){
setPasswords();
}

static void setPasswords(){
try{
//create a document object
Document doc = new Document();
//FileOutputStream object to write the pdf file
String path="d:/pdfdocprotected.pdf";
FileOutputStream fos=new FileOutputStream(path);
//get PdfWriter object
PdfWriter writer =PdfWriter.getInstance(doc,fos);
//convert strings of passwords to arrays of bytes
byte[] USER="Hello".getBytes();
byte[] OWNER="world".getBytes();
//spcify encryption of the document
writer.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING,

PdfWriter.ENCRYPTION_AES_256);          
      writer.createXmpMetadata();
//open the document for writing
doc.open();
//write a paragraph to the document
doc.add(new Paragraph("This PDF document is protected."));
//close the document
doc.close();
//view the result pdf file
Desktop dt=Desktop.getDesktop();
if(Desktop.isDesktopSupported()){
dt.open(new File(path));
}
}catch(Exception e){e.printStackTrace();}
}
}

When you run the example code above, you might get the dependency error as shown below. To fix the error, you will download the extra jar files from http://sourceforge.net/projects/itext/files/extrajars/. Then extract the zip file and add the bcprov-jdk15on-1.48.jar file to the project. After that, run the program again.

ASN1Encodable not found error

25 comments:

  1. Your technical information related with java programming is very useful and interesting. Also share updated details about java in your website. Thanks for sharing this article.
    Java training institute in chennai

    ReplyDelete
  2. Great insights about protecting PDF software. Really helpful for encrypting my personal PDF documents.

    ReplyDelete
  3. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!

    JAVA Training Institutes in Chennai

    ReplyDelete
  4. Thanks for sharing this informative blog..

    Regards..
    Java Institutes in Chennai

    ReplyDelete
  5. I usually set passwords on PDF files with the help of the program PDF Password Protect Free.http://manyprog.com/pdf-password-protect.php

    ReplyDelete
  6. Thank you for this great information. It was really helpful.

    school website design

    ReplyDelete
  7. Really awesome blog. Your blog is really useful for me.
    Thanks for sharing this informative blog. Keep update your blog.
    Oracle Training In Chennai

    ReplyDelete
  8. very informative blog. helps to gain knowledge about the password protection. Thanks for posting this information
    SEO Training in Chennai

    ReplyDelete
  9. Really a great article..Thanks for sharing this valuable information.
    Regards,

    Regards,
    CCNA Training in Chennai | CCNA Training Institute in Chennai | Best CCNA Training in Chennai

    ReplyDelete
  10. Really great post thanks for useful sharing with lots of apps thanks for useful sharing ivr telephony

    ReplyDelete
  11. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting. So know it's helpful..

    Weblogic Training in Chennai

    ReplyDelete
  12. The war between humans, orcs and elves continues earn to die . Lead your race through a series of epic battles, using your crossbow to fend off foes and sending out units to destroy castleshappy wheels . Researching and upgrading wisely will be crucial to your success! There are 5 ages total and each one will bring you new units to train to fight in the war for you cause.
    earn to die 2
    Whatever you do, don’t neglect your home base because you cannot repair it and once it is destroyed, you lose! Age of War is the first game of the series and really sets the tone for the Age of War games . Also try out the Age of Defense series as it is pretty similar.
    In this game, you start at the cavern men’s age, then evolvetank trouble ! There is a total of 5 ages, each with its units and turrets. Take control of 16 different units and 15 different turrets to defend your base and destroy your enemy.
    The goal of the game also differs depending on the level. In most levels the goal is to reach a finish line or to collect tokens. Many levels feature alternate or nonexistent goals for the player.

    ReplyDelete
  13. Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info! , Regards, servicenow training in hyderabad

    ReplyDelete
  14. It is really a great and useful piece of info. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

    Digital marketing company in Chennai

    ReplyDelete
  15. A good blog. Thanks for sharing the information. It is very useful for my future. keep sharing
    red ball 2 | duck life 2 | happy wheels | Red Ball | Red ball 3 | Flash Games| Tank trouble

    ReplyDelete
  16. Inventory Management Solution is an inventory controlling system solution and yet still easy to use Inventory Management as well as billing management Solutions, introduced by Aavennue Softech. Basically it works between three different layers of Inventory system i.e. Supplier / Purchaser, inventory and Customers.visit this site

    ReplyDelete
  17. My Arcus offer java training with 100% placement. Our java training course that includes fundamentals and advance java training program with high priority jobs. java j2ee training with placement having more exposure in most of the industry nowadays in depth manner of java

    java training in chennai

    ReplyDelete
  18. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,'

    Best PMP Training in Chennai | SAP Training in Chennai

    ReplyDelete
  19. Use same technique...but same error face!

    ReplyDelete