Thursday, June 28, 2012

Display tag library at Struts2

Strut2 filter configuration
http://www.displaytag.org/10/export_filter.html
Required jars 
http://sourceforge.net/projects/displaytag/files/displaytaglibrary/  (any version)
iText1.3(for pdf export)
<display:table name="clientList"(list) export="true"(enable export) pagesize="5"(how many to display)
 requestURI="#"(click o/p is same page) class="mars"(look style)  style="text-align:center;">
 <display:column property="clientId"(list contains attribute) title="Client Id"(displays at header) group="1"(give as a group)
  sortable="true"(sort is choice)  href="viewClient"(Action name)
  paramId="clientDetails.clientId" (giving param default value is properity)></display:column>
 <display:column property="companyName" title="Company Name" group="2" sortable="true">
 <display:column property="emailId" title="EmailId" />
 <display:column property="website" title="website" />
 <display:column title="Edit" href="editClient" paramId="clientDetails.clientId" paramProperty="clientId" media="pdf/html/excel"(display on which media decide by u)>Edit(user clicks action)</display:column>
 <display:column title="Delete" href="deleteClient" paramId="clientDetails.clientId" paramProperty="clientId" >Delete</display:column>
 <display:setProperty name="export.pdf" value="true"></display:setProperty>(export as a pdf)
 <display:setProperty name="export.excel.filename" value="ClientDetails.xls"/>(define xml  name by default jsp/ action name)
    <display:setProperty name="export.pdf.filename" value="ClientDetails.pdf"/>
 </display:table>

Sunday, June 3, 2012

How to Add Password Protection to PDF using iText in Java


iText is very powerful library. It is used extensively to create PDF files in Java applications. Although not free, iText is the de-fecto Java library for working with PDF files. It has been extensively used in different Java related products for generating and manipulating PDF files.
Let us see how iText can be used to set password protection (encryption) to a PDF file. Also how to read an encrypted PDF file in Java.
For this we are using our Generate PDF in Java using iText tutorial. You might want to have a look into that tutorial to have an overview of iText and its different APIs that can be used for PDF generation.
To set encryption in iText following API can be used.
?
1
2
3
4
5
6
7
8
String USER_PASS = "Hello123";
 
String OWNER_PASS = "Owner123";
 
PdfWriter writer = PdfWriter.getInstance(document, file);
 
writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
        PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
Thus .setEncryption() method sets password protection to any PDF file.
Before we get started with code, we need few JAR files. iText internally uses bouncycastle.org library to encrypt the PDF files. We will need following JAR files in addition to iText JAR.
  • itextpdf-5.2.1.jar
  • bcmail-jdk16-1.46.jar
  • bcprov-jdk16-1.46.jar
  • bctsp-jdk16-1.46.jar
You can download all these JARs along with the demo source code at the end of this tutorial.
Now following is the Java code to Generate a PDF file and set Password protection to it.
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package net.viralpatel.pdf;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
 
public class GeneratePDF {
 
    private static String USER_PASS = "Hello123";
 
    private static String OWNER_PASS = "Owner123";
 
    public static void main(String[] args) {
        try {
 
            OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));
 
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, file);
 
            writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
                    PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
 
            document.open();
            document.add(new Paragraph("Hello World, iText"));
            document.add(new Paragraph(new Date().toString()));
 
            document.close();
            file.close();
 
        } catch (Exception e) {
 
            e.printStackTrace();
        }
    }
}
Thus we used the same steps to generate PDF file that we used in previous tutorial. Just added one line writer.setEncryption() to set password to generated PDF.
Note that in setEncryption() method, we passes several arguments. First two parameters are the password values in bytes. We passed Hello123 and Owner123 as password values. Also the next argument is permission you want to give to user. Following are several permission values.
?
1
2
3
4
5
6
7
8
PdfWriter.ALLOW_PRINTING
PdfWriter.ALLOW_ASSEMBLY
PdfWriter.ALLOW_COPY
PdfWriter.ALLOW_DEGRADED_PRINTING
PdfWriter.ALLOW_FILL_IN
PdfWriter.ALLOW_MODIFY_ANNOTATIONS
PdfWriter.ALLOW_MODIFY_CONTENTS
PdfWriter.ALLOW_SCREENREADERS
You can provide multiple permissions by ORing different values. For example PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY.
The last parameter is the type of encryption you want to use to encrypt PDF. Following are several allowed values.
?
1
2
PdfWriter.ENCRYPTION_AES_128
PdfWriter.ENCRYPTION_AES_256