Sunday, June 30, 2013

Word to Text Converter

The WordToTextConverter can be used to convert from a microsoft word 2007+ file to a text file. Again, this program uses the Apache POI library or tika library to extract text from Microsoft Word 2007+ file.

Word file to text converter



import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordToTextConverter{
public static void main(String[] args){
try{
convertWordToText(args[0],args[1]);
}catch(ArrayIndexOutOfBoundsException aiobe){
System.out.println("Usage:java WordToTextConverter <word_file> <text_file>");

}

}


public static void convertWordToText(String src, String desc){
try{
//create file inputstream object to read data from file
FileInputStream fs=new FileInputStream(src);
//create document object to wrap the file inputstream object
XWPFDocument docx=new XWPFDocument(fs);
//create text extractor object to extract text from the document
XWPFWordExtractor extractor=new XWPFWordExtractor(docx);
//create file writer object to write text to the output file
FileWriter fw=new FileWriter(desc);
//write text to the output file
fw.write(extractor.getText());
//clear data from memory
fw.flush();
//close inputstream and file writer
fs.close();
fw.close();

}catch(IOException e){e.printStackTrace();}
}
}

In the code of the program above,  XWPFDocument is used to construct a Microsoft Word document object from the FileInputStream object. FileInputStream object contains all data of the original Microsoft Word file. To extract all text from the document, you need to use the  XWPFWordExtractor class. You will pass the document object to the constructor of the XWPFWordExtractor when you create an object of the XWPFWordExtractor class. From the XWPFWordExtractor object, you can get the text content of this object by using its getText method. Once you have the text, the FileWriter class can be used to output it to the destination file.

Friday, June 28, 2013

Text To Word Converter

The TextToWordConverter is able to convert from a text file to a microsoft word 2007+ (docx) file. To work with microsoft office documents from Java code, you will need to use the Apache POI library. You can download this library in jar file from Apache POI website or download tika library jar file from Apache tika. The tika library contains Apache POI, PdfBox, SAX, log4j,jdom, and other useful libraries that are packaged together.

text to word converter in Java

TexToWordConverter source code:

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.File;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class TextToWordConverter {
public static void main(String[] args){
              try{
                convertTextToWord(args[0],args[1]);
                }catch(ArrayIndexOutOfBoundsException aiobe){System.out.println("Usage: TextToWordConverter <text_file> <word_file>");}
}

        public static void convertTextToWord(String src,String des){
try{

                        //check source file existence
                        File sourcefile=new File(src);
                        if(!sourcefile.exists()){
                                System.out.println("Source not found");
                                System.exit(-1);
                        }
                        System.out.println("Please wait...");
                        FileReader fr=new FileReader(sourcefile); //create file reader
                        BufferedReader br=new BufferedReader(fr); //wrap file reader in BufferedReader
                        //so the reader can read the text by line
                        XWPFDocument docx=new XWPFDocument(); //create document for ms word 2007+
String text="";
while((text=br.readLine())!=null){
                                XWPFParagraph pa=docx.createParagraph(); //create paragraph in the document
                                XWPFRun run=pa.createRun(); //create run object in the paragraph
                                run.setFontFamily("Arial"); //set the font name of the text to be written
                                run.setFontSize(12); //set the font size of the text to be writtten
                                run.setText(text);//add text to paragraph
                                run.addBreak(); //add break
}

                        //check destination directory
                        File desdir;
                        if(des.lastIndexOf('/')!=-1){
                                desdir=new File(des.substring(0,des.lastIndexOf('/')));
                                if(!desdir.exists())
                                        desdir.mkdirs(); //create destination directory
                        }
                        docx.write(new FileOutputStream(des)); //write the content of the document to the output file
                                                           
br.close();
                        System.out.println("Conversion completed");

                }catch(Exception e){e.printStackTrace();}

}
}

Converting a text file to a pdf file is straightforward. Firstly, you will have a FileReader object to read the source text file. To read line by line from the text file, you will BufferedReader class. This class accepts the FileReader as its argument. To output the text to the Microsoft Word file, you need to create a Microsoft Word document object by using the XWPFDocument class. Once you have the Microsoft Word document object, you can create paragraphs in the document by using the createParagraph method. The Run objects of the paragraph is used to store text. You can apply styles to the text by using its methods (setFontFamily, setFontSize,...). After you prepare the Microsoft Word document already, to write it to the output file, use its write(FileOutputStream fos) method. You will need a FileOutputStream object to point to the output file.

-->

Wednesday, June 26, 2013

Extract images from PDF

PDFImageExtractor is a simple program that can extract all images on a PDF document. Sometimes, we don't want to convert PDF pages to image files. We only want to take all images from each page. In this scenario, PDFImageExtractor is useful. It is easy to use. When the program runs, its allows you select a PDF file that contains images to be extracted out. The extracted image files are stored in your current working directory.

extract images from pdf java


PDFImageExtractor source code

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfImageObject;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;

public class PDFImageExtractor{
public static void main(String[] args){

selectPDF();
}

//allow pdf file selection for extracting
public static void selectPDF(){

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF","pdf");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(false);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
File file=chooser.getSelectedFile();
System.out.println("Please wait...");  
            extractImage(file.toString());
System.out.println("Extraction complete");
            }

     
}


public static void extractImage(String src){

try{

//create pdf reader object
PdfReader pr=new PdfReader(src);
PRStream pst;
PdfImageObject pio;
PdfObject po;
int n=pr.getXrefSize(); //number of objects in pdf document
for(int i=0;i<n;i++){
po=pr.getPdfObject(i); //get the object at the index i in the objects collection
if(po==null || !po.isStream()) //object not found so continue
continue;
pst=(PRStream)po; //cast object to stream
PdfObject type=pst.get(PdfName.SUBTYPE); //get the object type
//check if the object is the image type object
if(type!=null && type.toString().equals(PdfName.IMAGE.toString())){
pio=new PdfImageObject(pst); //get the image
BufferedImage bi=pio.getBufferedImage(); //convert the image to buffered image
ImageIO.write(bi, "jpg", new File("image"+i+".jpg")); //write the buffered image
//to local disk

}

}


}catch(Exception e){e.printStackTrace();}

}


}

In the example code above, the getPdfObject(int index) is used to extract an object from the pdf document at the specified index. To determine whether the object is an image, you need to get the type of the object by using the get  method of the stream created from the object.

Note: When you use this program to extract the images from the PDF document, some images might be in wrong order (different from what you see on the PDF pages). It is the problem from iText library itself. I tried to solve this problem with PdfBox. However, it can not be solved.

Tuesday, June 25, 2013

PDF To JPG Converter

The PDFToJPGConverter program is able to convert all pages of a PDF document to JPG image format. Each page is converted to one image file.
In Java, it is simple to convert a PDF document to images by using PdfBox library. You can download the library jar file at http://pdfbox.apache.org/.  One additional library that you also need is FontBox. It can be dowloaded  from http://sourceforge.net/projects/fontbox/.


pdf to jpg converter java

PDFToJPGConverter source code:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import org.pdfbox.TextToPDF;
import org.pdfbox.exceptions.COSVisitorException;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDPage;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;

public class PDFToJPGConverter{
public static void main(String[] args){

selectPdf();

}

//allow images selection for converting
public static void selectPdf(){

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF","pdf");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(false);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
File file=chooser.getSelectedFile();  
            convertPDFToJPG(file.toString());
            }

     
}

public static void convertPDFToJPG(String src){

try{

//load pdf file in the document object
PDDocument doc=PDDocument.load(new FileInputStream(src));
//Get all pages from document and store them in a list
List<PDPage> pages=doc.getDocumentCatalog().getAllPages();
//create iterator object so it is easy to access each page from the list
Iterator<PDPage> i= pages.iterator();
int count=1; //count variable used to separate each image file
//Convert every page of the pdf document to a unique image file
System.out.println("Please wait...");
while(i.hasNext()){
PDPage page=i.next();
BufferedImage bi=page.convertToImage();
ImageIO.write(bi, "jpg", new File("pdfimage"+count+".jpg"));
count++;
}
System.out.println("Conversion complete");
}catch(IOException ie){ie.printStackTrace();}
}



}

In the example code above, the PDDocument class is used to create a pdf document from the InputStream object. The InputStream object contains all data of the original pdf file. Once you have the pdf document object, you can get all pages of the pdf document by using the getAllPages method from the document catalog object. The document catalog object is returned by the getDocumentCatalog method of the pdf document object. You will use the List data structure to store all pages of the pdf document. Each page of the pdf document is represented by the PDPage object. From this object, you can convert the page to the BufferedImage object by using the convertToImage method.

Monday, June 24, 2013

JPG To PDF Converter

The JPGToPDFConverter program is able to convert an image to a PDF file. You can convert different image formats (e.g jpg, png, gif, bmp, ...) to PDF files. When the program runs, it allows you to select many image files as you want. Each image file is converted to a PDF file. The PDF file is stored in the current working folder.
By using the JPGToPDFConverter, you are not worry about a big image that can't fit well on the PDF page. For the big image, it is resized automatically to fit the A4 paper size.


JPG to image PDF converter java

JPGToPDFConverter source code:

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;

public class JPGToPDFConverter{
public static void main(String[] args){

selectImage();
}


//allow images selection for converting
public static void selectImage(){

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "gif","png","bmp");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(true);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
File[] Files=chooser.getSelectedFiles();
System.out.println("Please wait...");
            for( int i=0;i<Files.length;i++){    
            convertJPGToPDF(Files[i].toString(),"pdfimage"+i+".pdf");
            }
System.out.println("Conversion complete");
        }
}

public static void convertJPGToPDF(String strimg,String desc){
try{
//create document object
Document doc=new Document();
//create pdf writer object to write the document to the output file
PdfWriter.getInstance(doc,new FileOutputStream(desc));
//get a4 paper size
Rectangle r=PageSize.A4;
//read the image
BufferedImage orImg=ImageIO.read(new File(strimg));
//initialize image width and height
int width=orImg.getWidth();
int height=orImg.getHeight();
//resize the image that is bigger than A4 size
if(width>r.getWidth())
width=(int)r.getWidth();
if(height>r.getHeight())
height=(int)r.getHeight();
//create a blank buffered image
BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//create graphic2d object from the buffered image
Graphics2D g2d=bi.createGraphics();
//draw the original image on the buffered image
//so the image is resized to fit the A4 paper size if it is bigger than A4 size
g2d.drawImage(orImg, 0,0,width,height,null);
//store the image data in memory
ByteArrayOutputStream bas=new ByteArrayOutputStream();
ImageIO.write(bi, "png", bas);
//create image from the image data stored in memory
Image img=Image.getInstance(bas.toByteArray());
//centrally align the image on the pdf page
img.setAlignment(Element.ALIGN_CENTER);
//open document
doc.open();
//add image to the document
doc.add(img);
//close the document
doc.close();


}catch(Exception e){e.printStackTrace();}
}



}

The process of converting an image to a PDF file in the code above is simple. Firstly, you need to have a BufferedImage object of the original image file. This can be done by using the read method of the ImageIO class. The BufferedImage is resized to fit the A4 paper size if its size is greater than the A4 paper size. This is to make sure that the output PDF file does not contain an image that greater than its size. For the image that is not larger than the A4 paper size, its size remains unchanged. You will need to convert the BufferedImage object back to an Image object since the add method of the Document object of the iText library accepts only the image object ( not the BufferedImage object). This can be done by using the getInstance method of the Image class in iText library. When the image is ready, you can call the add method of the Document object to add the image to the Document object.

Saturday, June 22, 2013

PDF To Text Converter

The PDFToTextConverter program can be used to convert a PDF file in to a text file. When the program runs you can selected one or many PDF files for converting. Then wait for a while. The amount of time waiting depends mainly on the number of PDF files you selected and each file size. It is noted that a PDF file that does not have text can not be converted.


PDF To Text Converter

PDFToTextConverter source code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.awt.Desktop;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;

public class PDFToTextConverter{
public static void main(String[] args){
selectPDFFiles();
}


//allow pdf files selection for converting
public static void selectPDFFiles(){

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF","pdf");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(true);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
File[] Files=chooser.getSelectedFiles();
System.out.println("Please wait...");
            for( int i=0;i<Files.length;i++){    
            convertPDFToText(Files[i].toString(),"textfrompdf"+i+".txt");
            }
System.out.println("Conversion complete");
            }

     
}

public static void convertPDFToText(String src,String desc){
try{
//create file writer
FileWriter fw=new FileWriter(desc);
//create buffered writer
BufferedWriter bw=new BufferedWriter(fw);
//create pdf reader
PdfReader pr=new PdfReader(src);
//get the number of pages in the document
int pNum=pr.getNumberOfPages();
//extract text from each page and write it to the output text file
for(int page=1;page<=pNum;page++){
String text=PdfTextExtractor.getTextFromPage(pr, page);
bw.write(text);
bw.newLine();

}
bw.flush();
bw.close();



}catch(Exception e){e.printStackTrace();}

}

}

Converting a pdf document to text file is simple. Firstly, you need to use the PdfReader class (in iText library) to get all pages of the pdf document. One you have the PdfReader object, you can extract the text from the pdf document by using the getTextFromPage(PdfReader pdfreader, int page_num) method of the PdfTextExtractor class.  This method extract the text from each page of the PdfReader object. While getting the text, you will use the BufferedWriter class to write the text out to a destination file.


Thursday, June 20, 2013

Text to PDF Converter

The TextToPDFConverter program is able to convert a text file to a PDF file. It is easy to use. When the program runs, it displays a file dialog that you can select one or multiple text files to convert. Click OK to accept the selection. The result PDF files are stored in your current working folder where you place the TextToPDFConverter program.

This program uses the iText library to create a PDF file from a text file. Each line of text in the text file is read by using the BufferedReader class of Java. The line of text is added to the Document object of iText. The PdfWriter writes the document to the PDF output file.


text to pdf converter


TextToPDFConverter source code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;


public class TextToPDFConverter{
public static void main(String[] args){

selectTextFiles();
}

//allow text files selection for converting
public static void selectTextFiles(){

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT","txt");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(true);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
File[] Files=chooser.getSelectedFiles();
System.out.println("Please wait...");
            for( int i=0;i<Files.length;i++){    
            convertTextToPDF(Files[i].toString(),"pdffromtext"+i+".pdf");
            }
System.out.println("Conversion complete");
            }

     
}

public static void convertTextToPDF(String src,String desc){

try{

//create file reader to read text from the source file
FileReader fr=new FileReader(src);
//create buffered reader to wrap the file reader
//so you can read text by line
BufferedReader br=new BufferedReader(fr);
//create document
Document doc=new Document();
//create pdf writer to write the document to the destination file
PdfWriter.getInstance(doc, new FileOutputStream(desc));
//open the document so it is ready to add text
doc.open();
//read text  line by line and add it to the document
String text="";
while((text=br.readLine())!=null){
doc.add(new Paragraph(text));
}
//close the document
doc.close();
//close the buffered reader
br.close();


}catch(Exception e){e.printStackTrace();}
}

}

Wednesday, June 19, 2013

Phonebook

This is a phonebook program that can be used to save a phonebook to a file, view all entries in the phonebook, add a new entry, delete an entry from the phonebook, find an entry by name, create pdf and html files of the phonebook.

The Hashtable data structure is used to store the list of phonebook entries. Each Hashtable entry contains the name and phone number. An operation (e.g add entry to phonebook) is performed on the Hashtable phonebook list. The updated phonebook list will be saved to the phonebook.bin file. When the program firstly loads, the file is read so that all entries in the phonebook are retrieved for later used in the program.

Phonebook in Java


phonebook html report

phonebook pdf report


PhoneBook source code:

import java.awt.Desktop;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfWriter;

//The PhoneBookEntry class is a template for every phonebook entry object
//that will be saved in a file called phonebook.bin
//Each entry has name and phone number
//The PhoneBookEntry must implement the Serializable interface since its object
//will be added to the Hashtable for saving in the file

class PhoneBookEntry implements Serializable{

private String Name;
private String Phone;

//constructor
PhoneBookEntry(String name,String phone){

this.Name=name;
this.Phone=phone;


}
//get name
public String getName(){
return Name;
}


//get phone number
public String getPhone(){
return Phone;
}

//print entry information
public void printInfo(){
System.out.println("Name:"+Name+", Phone:"+Phone);
}
}


public class PhoneBook{
static Hashtable<String,PhoneBookEntry> phonebook;
public static void main(String[] args){
phonebook=readList(); //read phonebook
int ch;
char con='y';
Scanner sc=new Scanner(System.in); //create scanner object to receive choice input

while(con=='y'){
showMenu(); //show menu
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch){
case 1:viewAll();break;
case 2:addToPhoneBook();break;
case 3:deleteFromPhonebook();break;
case 4:searchByName();break;
case 5:createPdf();break;
case 6:createHTML();break;
case 7:System.exit(0);break;
default: System.out.println("Invalid choice");
}

try{
//prompt for continuing the program
InputStreamReader isr=new InputStreamReader(System.in);
System.out.println("Press y to continue:");
con=(char)isr.read();
}catch(IOException ie){}


}


}
//The viewAll method displays all entries in the phonebook
public static void viewAll(){

if(phonebook!=null){

for(Enumeration<String> e=phonebook.keys(); e.hasMoreElements();){
PhoneBookEntry entry=phonebook.get(e.nextElement());
entry.printInfo();
}

}

}


//The addToPhoneBook method is able to add each entry to the phonebook
public static void addToPhoneBook(){
//If the phonebook null, allocate memory for it so it is ready to get the new item
if(phonebook==null) phonebook=new Hashtable<String,PhoneBookEntry>();
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name:");
String name=br.readLine();
System.out.println("Enter phone:");
String phone=br.readLine();
PhoneBookEntry st=new PhoneBookEntry(name,phone);
phonebook.put(name,st); //add new entry to the phonebook
writeIt(phonebook); //save the update phonebook
}catch(IOException e){}
}

//The deleteFromPhonebook method is able to delete an entry when the name
//is correctly input
public static void deleteFromPhonebook(){
if(phonebook!=null){
int si=phonebook.size(); //number of entries in the phonebook before an entry is removed
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Name:");
String key=br.readLine();
phonebook.remove(key); //remove the contact
if(phonebook.size()<si){ //removing is successful
writeIt(phonebook);
System.out.println("The entry has been deleted.");
}
else
System.out.println("Wrong name");
}catch(IOException ie){}


}
}

//The searchByName method has code to find a phonebook entry by name in the list
public static void searchByName(){
if(phonebook!=null){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Search by name:");
String key=br.readLine();
PhoneBookEntry cu=phonebook.get(key);
if(cu!=null)
cu.printInfo();

else
System.out.println("Not found");
}catch(IOException ie){}


}
}

//Write the Hashtable object representing the phonebook to the file
public static void writeIt(Hashtable<String,PhoneBookEntry> obj){
try{
FileOutputStream fos=new FileOutputStream("phonebook.bin");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.flush();
oos.close();
}catch(IOException ie){}

}

//The readList method has code to read phonebook from the file

public static Hashtable<String,PhoneBookEntry> readList(){

Hashtable<String,PhoneBookEntry> phbook=null;
try{
FileInputStream fis=new FileInputStream("phonebook.bin");
ObjectInputStream ois=new ObjectInputStream(fis);
phbook=(Hashtable<String,PhoneBookEntry>)ois.readObject();
ois.close();

}catch(Exception ie){}
return phbook;

}
//create phonebook pdf file
public static void createPdf(){

if(phonebook!=null){
try{
//create document object
Document doc=new Document(PageSize.A4);
//create a pdf writer object to write text to mypdf.pdf file
PdfWriter pwriter=PdfWriter.getInstance(doc, new FileOutputStream("phonebook.pdf"));
//open the document so it is ready to write text to the pdf file
doc.open();
//create font objects for table heading column labels to be written to the file
Font fontBoldRed=new Font(FontFamily.TIMES_ROMAN, 20, Font.BOLD, BaseColor.RED);
Font fontBold=new Font(FontFamily.TIMES_ROMAN, 15, Font.BOLD, BaseColor.BLACK);
//create table object that has two columns and two rows
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
cell=new PdfPCell(new Phrase("Phonebook",fontBoldRed));
cell.setColspan(2);
cell.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
table.addCell(cell);
cell=new PdfPCell(new Phrase("Name",fontBold));
table.addCell(cell);
cell=new PdfPCell(new Phrase("Phone",fontBold));
table.addCell(cell);
for(Enumeration<String> e=phonebook.keys(); e.hasMoreElements();){
PhoneBookEntry entry=phonebook.get(e.nextElement());
cell=new PdfPCell(new Phrase(entry.getName()));
table.addCell(cell);
cell=new PdfPCell(new Phrase(entry.getPhone()));
table.addCell(cell);
}
//add table to the document
doc.add(table);
//close the document when writing finishes
doc.close();
displayReport("phonebook.pdf");

}catch(DocumentException de){ de.printStackTrace();}
catch(FileNotFoundException fnfe){fnfe.printStackTrace();}

}
}

//create phonebook html file
public static void createHTML(){

try{
FileWriter fw=new FileWriter("phonebook.html");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("<!Doctype html>");
bw.newLine();
bw.write("<html><head><title>Phone Book</title></head>");
bw.newLine();
bw.write("<body style='padding-left:50px'>");
bw.newLine();
bw.write("<p style='color:#9900ff; font-size:16pt'>Phonebook</p>");
bw.write("<table>");
bw.newLine();
bw.write("<tr style='background-color:#00aaaa'><th>Name</th><th>Phone</th></tr>");
if(phonebook!=null){
Set<String> set=phonebook.keySet();
Iterator<String> iterator=set.iterator();
while(iterator.hasNext()){
PhoneBookEntry cu=phonebook.get(iterator.next());
bw.write("<tr><td>"+cu.getName()+"</td><td>"+cu.getPhone()+"</td></tr>");
bw.newLine();
}

}
bw.write("</table>");
bw.newLine();
bw.write("</body></html>");
bw.flush();
bw.close();
displayReport("phonebook.html");
}catch(IOException ie){ie.printStackTrace();}

}

//display the pdf or html report
public static void displayReport(String src){
try{

if(Desktop.isDesktopSupported()){
File f=new File(src);
Desktop.getDesktop().open(f);

}
}catch(IOException ie){}
}

//This method display options menu
public static void showMenu(){
System.out.println("1. View all phonebook entries");
System.out.println("2. Add to phonebook");
System.out.println("3. Remove from phonebook");
System.out.println("4. Find an entry");
System.out.println("5. Create phonebook pdf file");
System.out.println("6. Create phonebook html file");
System.out.println("7. Exit");
}


}

Tuesday, June 18, 2013

Merge pdf files

The MergePdf program can be used to merge or combine many pdf files together. The merged pdf file that will be generated is called mergedfile.pdf.

Note: Again you need to download iText library and add it to your classpath. It is used to generate
pdf file.

merge pdf files in Java


MergePdf source code:
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;


public class MergePdf {
public static void main(String[] args){
try{
mergePdf(args);
}catch(ArrayIndexOutOfBoundsException aiobe){System.out.println("Invalid parameters input");}
}
public static void mergePdf(String[] srcs){
try{
//create document object
Document document = new Document();
//create pdf copy object to copy current document to the output mergedfile file
PdfCopy copy = new PdfCopy(document, new FileOutputStream("mergedfile.pdf"));
//open the document
document.open();
PdfReader pr;
int n;
for (int i = 0; i < srcs.length; i++) {
pr = new PdfReader(srcs[i]); //create reader object to read each input pdf file
n = pr.getNumberOfPages();//get the number of pages per pdf file
for (int page = 1; page <=n;page++) {
copy.addPage(copy.getImportedPage(pr, page)); //extract each page from the file
}
}
document.close(); //close the document
}catch(Exception e){e.printStackTrace();}
}
}

In the program code above, the PdfReader class is used to read an array of the pdf source files. You need to use the PdfCopy class to import all pages found on the PdfReader. When constructing the PdfCopy object you need to specify the Document object and the FileOuputStream object. The Document object will store all pdf pages while the FileOuputStream object specify the output file and write all pages to the file. This class has a method called getImportedPage. This method returns the pdf Page object based on the specified page number.

Sunday, June 16, 2013

Split Pdf

The SplitPdf program can be used to split a pdf file to many pdf files. You can split the pdf file in every any page that you want. For example, if you have 100-page pdf file and you split it in every two pages, you will get fifty pdf files. Each file has two pages.
The API used to split a pdf file is iText. It is a useful and  free library. For more information about iText, you can visit its web site listed at the right side bar of this post.

split pdf in Java

SplitPdf source code:

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;

public class SplitPdf {
public static void main(String[] args){
try{

splitPdf(args[0],Integer.parseInt(args[1]));

}catch(ArrayIndexOutOfBoundsException aiobe){System.out.println("Invalid parameters input");}

}

public static void splitPdf(String src, int every_n_page){
try{
Document doc=null; //declare document
PdfReader pr=new PdfReader(src); //create pdf reader object
PdfCopy pc=null; //declare pdf copy object
int start=0; //initialize the start variable

for(int i=1;i<=pr.getNumberOfPages();i++){ //loop through the pdf document
//split the pdf document in every n page
if(i%every_n_page==0){
doc=new Document();
pc=new PdfCopy(doc, new FileOutputStream("split"+i+".pdf"));
doc.open();
for(int page=start+1;page<=i;page++)
pc.addPage(pc.getImportedPage(pr, page));
start+=every_n_page;
doc.close();
}
}
}catch(Exception e ) {}
}



}

To split a Pdf file to many Pdf files. Firstly, you need to have a PdfReader object to get all pages in the source Pdf file. Once you have the PdfReader object, you can use the PdfCopy class to get every Pdf page to be added to the Document object and write the Document out to the output file.

Saturday, June 15, 2013

Create a pdf

With the CreatePdf program, you learn how to create a pdf document and write a few lines of text in it. In Java, you can simply create a pdf document by using iText library. It is free and small. To use the iText library to create pdf documents, you need to download the library zip file from http://itextpdf.com/download.php. Then extract the zip file and add the jar file (e.g. itextpdf-5.4.2.jar) to your CLASSPATH of your operating system. For the Eclipse users, you can add the jar file to your project by clicking the Add External JARs... in the Libraries tab of the New Java Project window.

add itext library in eclipse


Width iText, you can create a pdf in a few steps as shown below:
1. Create a document object
2. Create a pdf writer object
3. Open the document
4. Add text to the document by using Chunk, Phrase, or Paragraph object
5. Close the document

CreatePdf source code:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class CreatePdf {
public static void main(String[] args){
createPdf();
}
public static void createPdf(){
try{
//create a document object with paper size A4 and
//margin left=72 units, margin right=72 units, margin top=72 units, and margin bottom=72 units
//72 units=1 inch
Document doc=new Document(PageSize.A4,72,72,72,72);
//create a pdf writer object to write text to mypdf.pdf file
PdfWriter pwriter=PdfWriter.getInstance(doc, new FileOutputStream("mypdf.pdf"));
//specify the vertical space between the lines of text
pwriter.setInitialLeading(20);
//open the document so it is ready to write text to the pdf file
doc.open();
//create font objects for the chunks of text to be written to the file
Font fontNormalBlack=new Font(FontFamily.TIMES_ROMAN, 20, Font.NORMAL, BaseColor.BLACK);
Font fontBoldWhite=new Font(FontFamily.TIMES_ROMAN, 20, Font.BOLD, BaseColor.WHITE);
//create chunk objects for text with specific fonts
Chunk chObj1=new Chunk("Hello World",fontNormalBlack);
Chunk chObj2=new Chunk("This pdf document is created with iText.",fontBoldWhite);
chObj2.setBackground(BaseColor.BLACK); //set background to the chunk
//add the chunk objects to the document
doc.add(chObj1);
doc.add(Chunk.NEWLINE); //add a new line between the chunks
doc.add(chObj2);
//close the document when writing finishes
doc.close();
//open the pdf file
try{

if(Desktop.isDesktopSupported()){
File f=new File("mypdf.pdf");
Desktop.getDesktop().open(f);

}
}catch(IOException ie){}

}catch(DocumentException de){ de.printStackTrace();}
catch(FileNotFoundException fnfe){fnfe.printStackTrace();}
}
}

create pdf file in Java

For code explanation, please read comments along with the code.

Friday, June 14, 2013

Contact List

With the ContactList program, you are able to save a contact list of customers to a file, view all contacts information, add a contact to the list, delete a contact from the list, search a contact by name, and generate html file for the contact list so that it can be printed.

The contact list is stored in a data structure called HashMap. Each HashMap entry contains the name of the customer as its key and its value is an customer object that contains customer name, sex, address, email, and phone number.

Contact List in Java

 contact list html report

Contact List source code:

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

//The Customer class is a template for every customer object
//that will be saved in a file called contacts.bin
//Each customer has name, sex, address, email, and phone
class Customer implements Serializable{
private String cusName;
private String cusSex;
private String cusAddress;
private String cusEmail;
private String cusPhone;
//constructor
Customer(String name, String sex,String address, String email,String phone){
cusName=name;
cusSex=sex;
cusAddress=address;
cusEmail=email;
cusPhone=phone;
}
//get customer name
public String getName(){
return cusName;
}
//get customer sex
public String getSex(){
return cusSex;
}
//get customer address
public String getAddress(){
return cusAddress;
}
//get customer email
public String getEmail(){
return cusEmail;
}
//get customer name
public String getPhone(){
return cusPhone;
}
//print customer information
public void printInfo(){
System.out.println("Name:"+cusName+", Sex:"+cusSex+", Adress:"+cusAddress+", Email:"+cusEmail+", Phone:"+cusPhone);
}
}


public class ContactList{
static HashMap<String,Customer> ls;
public static void main(String[] args){
ls=readList(); //read all contacts and place them in the list
int ch;
char con='y';
Scanner sc=new Scanner(System.in); //create scanner object to receive choice input
while(con=='y'){
showMenu(); //show menu
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch){
case 1:viewAll();break;
case 2:addToList();break;
case 3:deleteFromList();break;
case 4:searchByName();break;
case 5:generateHTML();break;
case 6:System.exit(0);break;
default: System.out.println("Invalid choice");
}
try{
//prompt for continuing the program
InputStreamReader isr=new InputStreamReader(System.in);
System.out.println("Press y to continue:");
con=(char)isr.read();
}catch(IOException ie){}
}
}
//The viewAll method displays all customers in the list
public static void viewAll(){
if(ls!=null){
Set<String> set=ls.keySet(); //get all customer names
//based these names all contacts can be retrieved
Iterator<String> iterator=set.iterator();
while(iterator.hasNext()){
Customer cu=ls.get(iterator.next());
cu.printInfo();
}
}
}
//The addToList method is able to add each customer to the list
public static void addToList(){
//If the ls null, allocate memory for it so it is ready to get the new item
if(ls==null) ls=new HashMap<String,Customer>();
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name:");
String name=br.readLine();
System.out.println("Enter sex:");
String sex=br.readLine();
System.out.println("Enter address:");
String address=br.readLine();
System.out.println("Enter email:");
String email=br.readLine();
System.out.println("Enter phone:");
String phone=br.readLine();
Customer st=new Customer(name,sex,address,email,phone);
ls.put(name,st); //add new customer to the list
writeIt(ls); //save the update list
}catch(IOException e){}
}
//The deleteFromList method is able to delete a contact when customer name 
//is correctly input
public static void deleteFromList(){
if(ls!=null){
int si=ls.size(); //number of contacts in the list before a contact is removed
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Customer name:");
String key=br.readLine();
ls.remove(key); //remove the contact
if(ls.size()<si){ //removing is successful
writeIt(ls);
System.out.println("The contact has been deleted.");
}
else
System.out.println("Wrong customer name");
}catch(IOException ie){}
}
}
//The searchByName method has code to find a contact by customer name in the list
public static void searchByName(){
if(ls!=null){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Search by name:");
String key=br.readLine();
Customer cu=ls.get(key);
if(cu!=null)
cu.printInfo();
else
System.out.println("Not found");
}catch(IOException ie){}
}
}
//Write the HashMap object representing the contact list to the file
public static void writeIt(HashMap<String,Customer> obj){
try{
FileOutputStream fos=new FileOutputStream("contacts.bin");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.flush();
oos.close();
}catch(IOException ie){}
}
//The readList method has code to read all contacts from the file
public static HashMap<String,Customer> readList(){
HashMap<String,Customer> lObject=null;
try{
FileInputStream fis=new FileInputStream("contacts1.bin");
ObjectInputStream ois=new ObjectInputStream(fis);
lObject=(HashMap<String,Customer>)ois.readObject();
ois.close();
}catch(Exception ie){}
return lObject;
}
//generate contact list html file
public static void generateHTML(){
try{
FileWriter fw=new FileWriter("contacts.html");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("<!Doctype html>");
bw.newLine();
bw.write("<html><head><title>Contacts List</title></head>");
bw.newLine();
bw.write("<body>");
bw.newLine();
bw.write("<p style='color:#9900ff; font-size:16pt'>Contacts List</p>");
bw.write("<table>");
bw.newLine();
bw.write("<tr style='background-color:#00aaaa'><th>Name</th><th>Sex</th><th>Address</th><th>Email</th><th>Phone</th></tr>");
if(ls!=null){
Set<String> set=ls.keySet(); 
Iterator<String> iterator=set.iterator();
while(iterator.hasNext()){
Customer cu=ls.get(iterator.next());
bw.write("<tr><td>"+cu.getName()+"</td><td>"+cu.getSex()+"</td><td>"+cu.getAddress()+"</td><td><a href='mailto:"+cu.getEmail()+"'>"+cu.getEmail()+"</a></td><td>"+cu.getPhone()+"</td></tr>");
bw.newLine();
}
}
bw.write("</table>");
bw.newLine();
bw.write("</body></html>");
bw.flush();
bw.close();
}catch(IOException ie){ie.printStackTrace();}
}
//This method display a list of choices
public static void showMenu(){
System.out.println("1. View all contacts");
System.out.println("2. Add to contacts list");
System.out.println("3. Remove from contacts list");
System.out.println("4. Find a contact");
System.out.println("5. Generate contact list html file");
System.out.println("6. Exit");
}

}
For code explanation, please read comments along with the code.