Monday, May 16, 2011

Hide the PDF properties Using iText

Requried jar files: bcprov-jdk15-140.jar, and itext-2.1.2.jar
Source code:

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class HidePdfProperties {
/**
* @param args
*/
public static void main( String[] args ) {
// TODO Auto-generated method stub
hidePdfProperties( "D:\\Ganga\\ganga_Test_New.pdf" );
}
/**
* To restrict the PDF properties
*
* @return response OutputStream
*/
public static void hidePdfProperties( String aFileName ) {
PdfReader reader;
Document document = null;
try {
reader = new PdfReader( aFileName );
// Already Encrypted
if ( reader.isEncrypted() ) {
reader = new PdfReader( aFileName, "test".getBytes() );
}
int totalPages = reader.getNumberOfPages();
document = new Document( reader.getPageSizeWithRotation( 1 ) );
PdfCopy copy =
new PdfCopy( document, new FileOutputStream(
"D:\\Ganga\\ganga_Test_Ency.pdf" ) );
// Hide the PDF properties
copy.setViewerPreferences( PdfWriter.HideMenubar );
copy.setViewerPreferences( PdfWriter.HideToolbar );
// test is the password for encrypt the file
// Using the same name we can UNENCRYPT the file
// if the file is encrypted then only print button own be displayed
// in right option
copy.setEncryption( null, "test".getBytes(),
PdfWriter.ALLOW_MODIFY_CONTENTS, PdfWriter.ENCRYPTION_AES_128 );
PdfAction action = null;
document.open();
for ( int pageNumber = 1; pageNumber <= totalPages; pageNumber++ ) {
copy.addPage( copy.getImportedPage( reader, pageNumber ) );
}
// Resize the page
action =
PdfAction.gotoLocalPage( 1, new PdfDestination(
PdfDestination.XYZ, 0, 10000, 0.70f ), copy );
copy.setOpenAction( action );
document.close();
} catch ( IOException anIOException ) {
anIOException.printStackTrace();
} catch ( DocumentException aDocumentException ) {
aDocumentException.printStackTrace();
} finally {
try {
if ( document != null && document.isOpen() ) {
document.close();
}
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
}

Split existing pdf file into two pdf files

Required Jar : itext-2.1.2.jar
Source Code:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfReader;
public class SplitPdf {
public static void main( String[] args ) {
try {
String fileName = "D:\\Ganga\\ganga_Test.pdf";
String delFileName = "D:\\Ganga\\ganga_Test_Del.pdf";
String newFileName = "D:\\Ganga\\ganga_Test_New.pdf";
PdfReader reader = new PdfReader( fileName );
// List of pages deleted from the existing file
List newPages = new ArrayList();
newPages.add( 4 );
// newPages.add( 5 );
com.lowagie.text.Document document =
new com.lowagie.text.Document( reader
.getPageSizeWithRotation( 1 ) );
PdfCopy copy =
new PdfCopy( document, new FileOutputStream( delFileName ) );
PdfCopy newCopy =
new PdfCopy( document, new FileOutputStream( newFileName ) );
document.open();
for ( int pageNumber = 1; pageNumber <= reader.getNumberOfPages(); pageNumber++ ) {
if ( !newPages.contains( pageNumber ) ) {
copy.addPage( copy.getImportedPage( reader, pageNumber ) );
} else {
newCopy
.addPage( copy.getImportedPage( reader, pageNumber ) );
}
}
document.close();
} catch ( FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( DocumentException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Concat Pdf files into one file Using iText

Required Jar file Name: itext-2.1.2.jar

Source code:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopyFields;
import com.lowagie.text.pdf.PdfReader;
public class ConcatPdf {
/**
* @param args
* the command line arguments
*/
public static void main( String[] args ) {
PdfCopyFields copy;
try {
//New File Name
copy =
new PdfCopyFields( new FileOutputStream(
"D:\\Ganga\\ganga_Test.pdf" ) );
//Existing file 1. Add this into New File Name
copy.addDocument( new PdfReader( "D:\\Ganga\\gg5.pdf" ) );
//Existing file 2. Add this into New File Name
copy.addDocument( new PdfReader( "D:\\Ganga\\gg6.pdf" ) );
//Existing file 3. Add this into New File Name
copy.addDocument( new PdfReader( "D:\\Ganga\\gg7.pdf" ) );
//Existing file 4. Add this into New File Name
copy.addDocument( new PdfReader( "D:\\Ganga\\gg8.pdf" ) );
copy.close();
} catch ( FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( DocumentException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

To reset the pagination in rich:datascroller

1. Add the page attribute in rich:datascroller
page="#{cvQaReviewSearchBackingBean.pageNumber}";

2. Initialize the page Number in reset method in backing bean
public final void reset() {
pageNumber = 1;
}

To reset the sorting column in rich:dataTable

public void resetTableSorting() {
UIDataTable table = (UIDataTable) FacesContext.getCurrentInstance().getViewRoot()
.findComponent("FormName:TableName" );
resetSorting( table );
}
public void resetSorting( UIDataTable table ) {
if ( table != null ) {
List uiComponentBase = (List) table.getChildren();
for ( UIComponent column : uiComponentBase ) {
( (UIColumn) column ).setSortOrder( Ordering.UNSORTED );
}
}
}