August 01, 2016

pdf creator progam in java - portable document format

A pdf creator in java


Description

descripiton

Source code

package com.konzn.kims.utils;


import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;


import java.io.FileNotFoundException;

import java.io.FileOutputStream;
import java.io.File;

import javax.swing.JTable;

/**
 * 
 * @author aromal
 * @version 1.0
 */
public class PdfCreator {

String filename = "accounts_consumerfed";
String path = null;
String projectName = "accounts manager";

public PdfCreator(){
initialize();
}

private void initialize() {
path = System.getProperty("user.home");
createDirectory(path+"\\"+projectName);
}

/**

* @see 
* @param 
* @return void
* Description : to create a directory if not exists
* Date : Jan 4, 2012
* Coded by : +deepa
*/
private void createDirectory(String dirPath) {
File directory = new File(dirPath);
if(directory.exists() == false){
directory.mkdir();
path = dirPath;
}else if(directory.exists()){
path = dirPath;
}else{
System.out.println("error at create directory ");
}

}

public static void main(String[] args) {
}

public void createPdfFromTable(JTable test) {
String[] headers = new String[test.getColumnCount()];
for (int j = 0; j < test.getColumnCount(); j++) {
headers[j] = test.getColumnName(j);
}

//

// Create a new document.

//

Document document = new Document(PageSize.LETTER.rotate());

try {

// Get an instance of PdfWriter and create a Table.pdf file

// as an output.

//

PdfWriter.getInstance(document,

new FileOutputStream(new File("E:\\AromalTable.pdf")));

document.open();

//

// Create an instance of PdfPTable. After that we transform

// the header and data array into a PdfPCell object. When
// each table row is complete we have to call the

// table.completeRow() method.

//
// For better presentation we also set the cell font name,

// size and weight. And we also define the background fill

// for the cell.

//

PdfPTable table = new PdfPTable(headers.length);

for (int i = 0; i < headers.length; i++) {

String header = headers[i];

PdfPCell cell = new PdfPCell();

cell.setGrayFill(0.9f);

cell.setPhrase(new Phrase(header.toUpperCase(),

new Font(Font.HELVETICA, 10,

Font.BOLD)));

table.addCell(cell);

}

table.completeRow();

for (int i = 0; i < test.getRowCount(); i++) {

for (int j = 0; j < test.getColumnCount(); j++) {

String datum = test.getValueAt(i, j).toString();

PdfPCell cell = new PdfPCell();

cell.setPhrase(new Phrase(datum.toUpperCase(),

new Font(Font.HELVETICA, 10,

Font.NORMAL)));

table.addCell(cell);

}

table.completeRow();

}

document.addTitle("Table Demo");

document.add(table);

} catch (DocumentException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

document.close();

}


}


/**

* @see 
* @param 
* @return String
* Description : create a pdf by specified name and table
* Date : Jan 4, 2012
* Coded by : nithin,aromal
*/
public String createPdfFromTable(JTable test,String fileName) {
String message = "error in creation";
filename = fileName+".pdf";
String location = path+"\\"+filename;
String[] headers = new String[test.getColumnCount()];
for (int j = 0; j < test.getColumnCount(); j++) {
headers[j] = test.getColumnName(j);
}
Document document = new Document(PageSize.LETTER.rotate());
try {
PdfWriter.getInstance(document,
new FileOutputStream(new File(location)));
document.open();
PdfPTable table = new PdfPTable(headers.length);
for (int i = 0; i < headers.length; i++) {
String header = headers[i];

PdfPCell cell = new PdfPCell();

cell.setGrayFill(0.9f);

cell.setPhrase(new Phrase(header.toUpperCase(),

new Font(Font.HELVETICA, 10,

Font.BOLD)));

table.addCell(cell);

}

table.completeRow();

for (int i = 0; i < test.getRowCount(); i++) {

for (int j = 0; j < test.getColumnCount(); j++) {

String datum =  null;
if(test.getValueAt(i, j)== null){
datum = "      ";
}
else{
datum =test.getValueAt(i, j).toString();
}


PdfPCell cell = new PdfPCell();

cell.setPhrase(new Phrase(datum.toUpperCase(),

new Font(Font.HELVETICA, 10,

Font.NORMAL)));

table.addCell(cell);

}

table.completeRow();

}

document.addTitle("Table Demo");

document.add(table);
message = fileName + " created ";
} catch (DocumentException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

document.close();

}


return message;
}

/**

* @see 
* @param Bean
* @return String
* Description :TODO
* Date : Jan 9, 2012
* Coded by : +joseph james 
*/
public String createForm8(){

return null;
}
}


http://javabelazy.blogspot.in/

Facebook comments