How to Create Password Protected Pdf in java - iText Example


In this core java tutorial we will learn How to Create Password Protected Pdf in java - iText library Example - core java tutorial in Java with program and examples.


Follow these 8 steps to create Password Protected Pdf in java using iText>
1) Create FileOutputStream - the file in which created PDF will be stored
OutputStream fos = new FileOutputStream(new File(pdfFilePath));

2) create Document (This document will represent the PDF document)
Document class is present in com.itextpdf.text.Document.
Document document = new Document();

3) Attach PdfWriter to document,whenever any PDF element is added to document, it will get written to FileOutputStream

PdfWriter.getInstance(document, fos);

4) Before opening the document, Create userPassword and ownerPassword
   String userPassword = "a";
   String ownerPassword = "b";
5) Now use setEncryption method of pdfWriter class to set the encryption type, get bytes from password for encryption.
   
pdfWriter.setEncryption(userPassword.getBytes(),
                      ownerPassword.getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
6) Now you may open the document.
document.open();
                
7) add paragraph to document
document.add(new Paragraph("Hi!, This is my first PDF using iText"));
8) close document
document.close();
                

We will generate PDF files in java using iText library. PDF files can be generated dynamically in java using iText library.

How to Set the Font Name, Size, Style and Colour In Pdf using itext in java

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

Convert Html To Pdf in java using iText - iText java tutorial

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial





Download all jars required to execute program >

Or, you may download jars from here.


Program/Example to How to Create Password Protected Pdf in java - iText Example - core java tutorial

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
*  How can I Create Password Protected Pdf Example
*  Using iText library - core java tutorial
*
*/
public class CreatePasswordProtectedPdfExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "E:/Create Password Protected Pdf Example.pdf";
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
                 String userPassword = "a";
                 String ownerPassword = "b";
                 pdfWriter.setEncryption(userPassword.getBytes(),
                              ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING,
                              PdfWriter.ENCRYPTION_AES_128);
                 document.open();
                 document.add(new Paragraph("This is Password protected PDF file"));
                 document.close();
                 fos.close();
                 System.out.println("PDF created in >> " + pdfFilePath);
          } catch (Throwable e) {
                 e.printStackTrace();
          }
   }
}
/* OUTPUT of above program/Example -
PDF created in >> E:/Create Password Protected Pdf Example.pdf
*/


PDF formed after executing above java program(How to Create Password Protected Pdf in java - iText Example ) will look like this >
You will have to provide password for opening the pdf file. You may use userPassword (‘a’) or ownerPassword (‘b’) to open the file.

Summary -

So in this core java tutorial we learned How to Create Password Protected Pdf in java.
We created FileOutputStream - the file in which created PDF was stored, created Document, attached PdfWriter to document,whenever any PDF element was added to document, it got written to FileOutputStream,
Before opening the document, we created userPassword and ownerPassword , then we used setEncryption method of pdfWriter class to set the encryption type, got bytes from password for encryption.
then we opened document, then added  paragraph to document, then closed the document.




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java


Creating first pdf and setting attributes using itext in java - iText tutorial >

Create the first Pdf Example Using iText library in java - iText java tutorial example

Setting Pdf Attributes like Title, Author, Creator, subject, Keywords, Header and CreationDate in java



Creating TABLES in pdf in java - iText tutorial >

How to Create the Table In Pdf in java - iText java tutorial

How to do the Cell Alignment In Pdf generated table in Java program

How to Set Cell Background Colour In Pdf Table in java example

How to Set Cell Background Colour In Pdf Table in java - iText java tutorial

How to Create Images In Pdf generated Table in java itext

How to Create Nested Tables in Pdf in java - iText java tutorial

How to Create the Table With Multiple Rows In Pdf in java program

Creating IMAGES in pdf in java - iText tutorial >

Adding the Images In Pdf in java example using iText - iText java tutorial

How to Add And Rotate Images In Pdf in java - iText java tutorial example

How can you Add the Images from URL In Pdf in java program





Adding WATERMARK in pdf in java - iText tutorial >

How to add TEXT as Watermark In Pdf in java - iText java tutorial

How to add IMAGE as Watermark In Pdf in iText in java example



Creating CHAPTER, SECTION, CHUNK, PARAGRAPH AND PHRASE in pdf in java - iText tutorial >

Create Chapter And Section In Pdf in java - iText java tutorial

Create Chunk In Pdf in java - iText java tutorial program

how to Create Paragraph In Pdf - iText java tutorial program

Create Left, center and right aligned Paragraph In Pdf - iText java tutorial

Create Phrase In Pdf (with lead) Using Itext - iText java tutorial example



HEADER and FOOTER in pdf in java - iText tutorial >

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial



BAR AND PIE CHARTS in pdf in java - iText tutorial >

How to create Bar Chart In Pdf in java - iText java tutorial

Create Pie Chart In Pdf in java using itext - iText java tutorial



MODIFY/ EDIT pdf in java - iText tutorial >

How to Modify - Add Text To Existing PDF in java - iText java tutorial example

How To Read And Add Image To Existing Pdf in java Example - iText java tutorial



Solve common Exceptions in itext in java - iText tutorial >

How to solve the Document Has No Pages IOException in iText in java


Alternates to itext in java - iText tutorial >

What are alternatives to iText library for creating Pdf in java?




eEdit
Must read for you :