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


PDF Viewer - Reader

66 comments:

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

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

    JAVA Training Institutes in Chennai

    ReplyDelete
  3. 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
  4. Thank you for this great information. It was really helpful.

    school website design

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

    ReplyDelete
  6. 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
  7. Really great post thanks for useful sharing with lots of apps thanks for useful sharing ivr telephony

    ReplyDelete
  8. 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
  9. Nice article. Keep posting on Java Programming Language.

    ReplyDelete
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. Use same technique...but same error face!

    ReplyDelete
  16. 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

    ReplyDelete
  17. 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.

    ReplyDelete
  18. 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

    ReplyDelete
  19. 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

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

    ReplyDelete
  21. 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!

    ReplyDelete
  22. 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

    ReplyDelete
  23. 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

    ReplyDelete
  24. 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

    ReplyDelete
  25. hello sir,
    thanks for giving that type of information.led lawn lights in delhi

    ReplyDelete
  26. THANKS FOR INFORMATION

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

    web development company in delhi

    levantro
    interior designers delhi


    livewebindia
    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.

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

    ReplyDelete
  28. 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

    ReplyDelete
  29. WINNING303 - JUDI BOLA ONLINE | CASINO ONLINE | SLOT ONLINE | POKER ONLINE TERPERCAYA

    TOGEL POKER

    BO POKER

    TOGEL303

    CASINO303

    BOLA303

    * KUNJUNGI SITUS KAMI DI *

    www.winning303.com


    *NB : MENANG BERAPAPUN, PASTI KAMI BAYAR !!! *

    * Melayani LiveChat 7 x 24 Jam Nonstop :
    - WA : +6281717177303
    - BBM : WINNING303
    - LINE : WINNING303*

    ReplyDelete
  30. Hola! I've been reading your website for some time now and finally got the courage to go ahead and give you a shout out from Atascocita Tx! Just wanted to tell you keep up the good job! 10ft lorry rental

    ReplyDelete
  31. I love this post.

    เว็บไซต์คาสิโนออนไลน์ที่ได้คุณภาพอับดับ 1 ของประเทศ
    เป็นเว็บไซต์การพนันออนไลน์ที่มีคนมา สมัคร Gclub Royal1688
    และยังมีเกมส์สล็อตออนไลน์ 1688 slot อีกมากมายให้คุณได้ลอง
    สมัครสมาชิกที่นี่ >>> Gclub Royal1688

    ReplyDelete
  32. Thanks for sharing excellent informations. Your web site is very cool. I am impressed by the details that you have on this web site. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found just the information I already searched everywhere and simply could not come across. What a great site. singapore domain

    ReplyDelete
  33. I am grateful to the owner of this site which really shares this wonderful work of this site. That is actually great and useful information. I would also like to share some useful information regarding digital marketing company in delhii(www.nexcuit.com)This is the digital marketing company in delhi located in Laxmi Nagar, East Delhi. So if anyone is looking out for the Best SEO company in Delhi then reach out to them on the given number+919910326510 and get more details.related to the packages they offer and their prices. One thing I can bet on is that you will get 100% satisfaction from there services.
    digital marketing company in delhii
    Best Digital marketing agency serving in Delhi
    best digital marketing company in Delhi NCR
    SEARCH ENGINE OPTIMIZATION (SEO)

    ReplyDelete
  34. Dapat Angka Bocor SGP.!! Terbukti 100%
    Begini Cara Menghitung Angka Bocor

    ReplyDelete
  35. I am really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Either way keep up the excellent quality writing, it’s rare to see a great blog like this one these days.secured payment gateways

    ReplyDelete
  36. Hey there! Would you mind if I share your blog with my facebook group? There's a lot of people that I think would really appreciate your content. Please let me know. Cheers
    Top SEO Experts

    ReplyDelete
  37. Hi there! This is kind of off topic but I need some advice from an established blog. Is it difficult to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about creating my own but I'm not sure where to start. Do you have any ideas or suggestions? simple website design

    ReplyDelete
  38. My wife and i ended up being thankful when Raymond could round up his analysis while using the ideas he made out of your weblog. It's not at all simplistic to simply possibly be releasing secrets which some others may have been selling. And we all fully grasp we have got the website owner to appreciate for this. Most of the explanations you've made, the easy web site navigation, the relationships you can give support to foster - it is many extraordinary, and it's helping our son in addition to the family reason why this issue is excellent, which is especially serious. Thank you for the whole lot!
    seo business

    ReplyDelete
  39. hello!,I like your writing so much! share we communicate more about your article on AOL? I need an expert on this area to solve my problem. Maybe that's you! Looking forward to see you.
    oxford graphic Singapore

    ReplyDelete
  40. I am really loving the theme/design of your web site. Do you ever run into any web browser compatibility problems? A couple of my blog audience have complained about my site not operating correctly in Explorer but looks great in Chrome. Do you have any tips to help fix this problem?
    promising the #1 ranking

    ReplyDelete
  41. Woah! I'm really digging the template/theme of this blog. It's simple, yet effective. A lot of times it's very hard to get that "perfect balance" between superb usability and appearance. I must say you've done a excellent job with this. In addition, the blog loads very quick for me on Safari. Excellent Blog!
    international conferences singapore

    ReplyDelete
  42. This comment has been removed by the author.

    ReplyDelete
  43. This comment has been removed by the author.

    ReplyDelete
  44. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

    tirupati darshan package from chennai

    ReplyDelete
  45. thanks for the info. If you are interested in cars then make sure to checkout my website.

    ReplyDelete