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.


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){

static void setPasswords(){
//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,

//open the document for writing
//write a paragraph to the document
doc.add(new Paragraph("This PDF document is protected."));
//close the document
//view the result pdf file
Desktop dt=Desktop.getDesktop();
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


  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

    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

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

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

    JAVA Training Institutes in Chennai

  4. Thanks for sharing this informative blog..

    Java Institutes in Chennai

  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

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

    school website design

  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

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

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

    CCNA Training in Chennai | CCNA Training Institute in Chennai | Best CCNA Training in Chennai

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

  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

  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.

  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

  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

  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

  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

  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

  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.

    Best PMP Training in Chennai | SAP Training in Chennai

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

  20. Very Nice Blog I like the way you explained these things. I’ve been looking for ways to improve my website and overall rankings.I hope your future article will help me further.Take SEO Training in Chennai to mould yourself.

  21. Finding the time and actual effort to create a superb article like this is great thing. I’ll learn many new stuff right here! Good luck for the next post buddy..
    PHP training in chennai

  22. Bán thuốc diệt kiến Nhật Bản Super Arinosu Koroki siêu an toàn, diệt 1 con lây chết cả tổ LH 0983131528

  23. There are so many useful information in your post, I like it all the time. And I want to share this boost app ranking with you, you can take a look. This is very useful.

  24. Thanks for sharing this blog. This is so informative blog
    Learned a lot of new things from your post! Good creation and HATS OFF to the creativity of your mind.
    Very interesting and useful blog!
    Best CCNA Training in Gurgaon

  25. Khi sử dụng vong huyet ap nhat ban sẽ làm cho huyết áp của bạn luôn cân bằng, ổn định, công dụng cụ thể như sau :

    - Điều hòa ổn định huyết áp
    - Chống đau cứng vai cổ
    - Khi đi tầu xe đeo rất tốt vì vòng này tuần hoàn máu rất tốt nên - không gây ra cảm giác chóng mặt, say xe.

  26. Dịch Vụ Làm Visa Trọn Gói 247 Thủ Tục Nhanh Chóng, Gọn Nhẹ, Giá Ưu Đãi​ 0983.1315.28

  27. Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Mindmajix EMC Training

  28. Wethecoders provides standard Java Project Help to students at affordable rate.

  29. It is nice article to improve my knowledge.thank you for sharing useful post
    web programming tutorial

  30. Dapatkan freechips 10rb untuk member baru hanya di S1288poker. HOKI KAMU ADA DISINI
    Info Lebih Lanjut Bisa Hub kami Di :
    BBM : 7AC8D76B
    WA : 087782869981


  31. View mieng tham hut mo hoi vung nach
    The strongest and most secure level of encryption within PDF format is 256-bit AES (for Advanced Encryption Standard), which has never been cracked!

  32. Ensure that your digital books have the correct organization. On the off chance that a book has a defective configuration, it will probably get negative audits, poor deals execution, and expulsion from Amazon's Kindle Store. altocompresspdf.com

  33. Tìm mua sản phẩm máy lọc nước điện giải Panasonic hàng nội địa Nhật chuẩn chính hãng giá rẻ call 0983131528

  34. One program is Cute PDF Writer. This is a free for individual utilize program that relies on Ghostscript programming to create PDF records. unlock pdf

  35. nice work keep it up thanks for sharing the knowledge.Thanks for sharing this type of information, it is so useful. Epoxy Grout manufacturer in delhi

  36. hi, nice information is given in this blog. Thanks for sharing this type of information, it is so useful for me. nice work keep it up. Laminated Doors manufacturer in hubli


    you can search low-cost website with high-quality website functions.
    Today Join Us
    Call: +91 - 8076909847

    web development company in delhi

    interior designers delhi

    web design company

    top seo company in delhi

    Best It Service Provider:

    1. Website Designing And Development.
    2. SEO Services.
    3. Software Development.
    4. Mobile App Development.

  38. Thanks for sharing such helpful an informative article. I hope you will keep sharing such an informative articles more and more

  39. A lot of thanks for your whole hard work on this website. My mum takes pleasure in doing research and it is obvious why. I notice all of the dynamic way you convey great items by means of the website and therefore inspire contribution from other individuals on that issue while our own princess is always discovering a great deal. Have fun with the remaining portion of the new year. You are doing a glorious job. social media strategy

  40. we have provide the best website designing service in Gurgaon.
    website designing company in Gurgaon