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

80 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
    Replies
    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.

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

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
  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

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

    ReplyDelete
  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

    ReplyDelete
  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

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

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

    ReplyDelete
  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

    https://s1288pokergroup.weebly.com/blog/solusi-menang-bermain-poker-online
    https://s1288pokercs.wordpress.com/2018/07/07/s1288poker-website-judi-online-terpercaya/
    https://clarastevani55.tumblr.com/post/174494937259/situs-judi-online-terpercaya-2018

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  37. 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
  38. Thanks for sharing such helpful an informative article. I hope you will keep sharing such an informative articles more and more

    ReplyDelete
  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

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

    ReplyDelete
  41. 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
  42. DAFTAR ID GRATIS SABUNG AYAM ONLINE


    * KUNJUNGI SITUS KAMI DI *

    WWW.ID303.INFO


    MENANG BERAPAPUN, PASTI KAMI BAYAR !!! *


    * Melayani LiveChat 7 x 24 Jam Nonstop :

    - WA : 08125522303
    - BBM : CSID303



    BONUS JACKPOT SABUNG AYAM ONLINE


    DEPOSIT VIA PULSA

    ReplyDelete
  43. 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
  44. I love this post.

    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสมาชิกที่นี่ >>> Gclub online

    ReplyDelete
  45. I love this post.

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

    ReplyDelete
  46. Thank you so much for providing individuals with such a wonderful chance to read articles and post posts from here. It is often very nice and packed with amusement for me personally and my office acquaintances to visit your web site particularly thrice weekly to study the latest guides you have. And of course, we are certainly amazed with all the incredible inspiring ideas served by you. Some two points in this post are without a doubt the very best we've had. Air Cargo Service

    ReplyDelete
  47. 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 SEO services in Delhi(www.nexcuit.com)This is the best SEO company 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.

    seo services in delhi
    best seo company in delhi
    seo company in delhi
    seo company in laxmi nagar

    ReplyDelete
  48. 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.If
    you are Looking for the satta result Delhi Darbar in delhi,ncr or satta result Gali, satta result Deshawar. Then open the below link for more details.

    satta result Taj
    satta result Delhi Darbar
    satta result Gali
    satta result Deshawar
    Disawar Satta Result
    satta result Ghaziabad
    satta result Faridabad

    satta king shri ganesh
    satta result chart
    satta king chart
    satta king result
    satta bazar result

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