环境:jdk1.6.0_17
开发工具:eclipse
依赖Jar包:iText-2.1.7.jar & iTextAsian.jar
代码:
package com.design.pdf;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Vector;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
/**
* depends on iText-2.1.7.jar & iTextAsian.jar
*
* @since 2013-12-31
*
*/
public class PdfUtils {
/**
* generate PDF document
*
* @param title
* the title in document
* @param colNames
* the columns of table in document
* @param data
* the data of table in document
* @param storeLoc
* the location of PDF document
* @return
*/
public static boolean generatePdf(String title, Vector<String> colNames,
Vector<Vector<String>> data, String storeLoc) {
Document document = generateDoc(title, storeLoc);
return insertData(document, colNames, data);
}
/**
* generate PDF document
*
* @param title
* the title in document
* @param colNames
* the columns of table in document
* @param data
* the data of table in document
* @param storeLoc
* the location of PDF document
* @return
*/
public static boolean generatePdf(String title, String[] colNames,
String[][] data, String storeLoc) {
Document document = generateDoc(title, storeLoc);
return insertData(document, colNames, data);
}
/**
* insert data to document.<br>
* tips:the document will be closed after insert data.
*
* @param document
* PDF document
* @param colNames
* the columns of table in document
* @param data
* the data of table in document
* @return
*/
private static boolean insertData(Document document,
Vector<String> colNames, Vector<Vector<String>> data) {
if (!validate(colNames, data)) {
return false;
}
// font
BaseFont bfChinese = null;
Font fontChinese = null;
try {
bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
false);
} catch (Exception e1) {
e1.printStackTrace();
}
fontChinese = new Font(bfChinese, 14, Font.COURIER);
int cols = colNames.size();
int rows = data.size();
// the table in document
PdfPTable pdfTable = new PdfPTable(cols);
// set before and after spacing
pdfTable.setSpacingBefore(25);
pdfTable.setSpacingAfter(25);
PdfPCell pdfCell = null;
// generate table columns name
for (int c = 0; c < cols; c++) {
String colName = colNames.elementAt(c);
pdfCell = new PdfPCell(new Phrase(colName,fontChinese));
pdfTable.addCell(pdfCell);
}
// insert data
Vector<String> rowData = null;
String cellData = null;
for (int r = 0; r < rows; r++) {
rowData = data.elementAt(r);
for (int c = 0; c < cols; c++) {
cellData = rowData.elementAt(c);
pdfCell = new PdfPCell(new Phrase(cellData,fontChinese));
pdfTable.addCell(pdfCell);
}
}
try {
// add table data to document
document.add(pdfTable);
} catch (DocumentException e) {
e.printStackTrace();
return false;
} finally {
if (document.isOpen()) {
document.close();
}
}
return true;
}
/**
* insert data to document.tips:the document will be closed after insert
* data.
*
* @param document
* PDF document
* @param colNames
* the columns of table in document
* @param data
* the data of table in document
* @return
*/
private static boolean insertData(Document document, String[] colNames,
String[][] data) {
if (!validate(colNames, data)) {
return false;
}
// font
BaseFont bfChinese = null;
Font fontChinese = null;
try {
bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
false);
} catch (Exception e1) {
e1.printStackTrace();
}
fontChinese = new Font(bfChinese, 14, Font.COURIER);
// the columns in table
int cols = colNames.length;
// the table in document
PdfPTable pdfTable = new PdfPTable(cols);
// set before and after spacing
pdfTable.setSpacingBefore(25);
pdfTable.setSpacingAfter(25);
PdfPCell pdfCell = null;
// generate table columns name
for (int i = 0; i < cols; ++i) {
String colName = colNames[i];
pdfCell = new PdfPCell(new Phrase(colName, fontChinese));
pdfTable.addCell(pdfCell);
}
// insert data
int rows = data.length;
String[] rowData = null;
String cellData = null;
for (int r = 0; r < rows; r++) {
rowData = data[r];
for (int c = 0; c < cols; c++) {
cellData = rowData[c];
pdfCell = new PdfPCell(new Phrase(cellData, fontChinese));
pdfTable.addCell(pdfCell);
}
}
try {
// add table data to document
document.add(pdfTable);
} catch (DocumentException e) {
e.printStackTrace();
return false;
} finally {
if (document.isOpen()) {
document.close();
}
}
return true;
}
/**
* generate PDF document by title and location only
*
* @param title
* @param storeLoc
* @return
*/
private static Document generateDoc(String title, String storeLoc) {
BaseFont bfChinese = null;
Font fontChinese = null;
try {
bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
false);
} catch (Exception e1) {
e1.printStackTrace();
}
fontChinese = new Font(bfChinese, 14, Font.BOLD);
// the absolute path of document
File storeFile = new File(storeLoc);
// document object
Document document = new Document(PageSize.A3, 0, 0, 0, 0);
// the title in document
Paragraph pdfTitle = new Paragraph(title, fontChinese);
// set alignment to be center
pdfTitle.setAlignment(Paragraph.ALIGN_CENTER);
try {
// get PDF writer
PdfWriter.getInstance(document, new FileOutputStream(storeFile));
document.open();
// add PDF title to document
document.add(pdfTitle);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return document;
}
/**
* validate columns,table data and whether columns's length equals to the
* length of row data or not.
*
* @param colNames
* @param data
* @return
*/
public static boolean validate(Vector<String> colNames,
Vector<Vector<String>> data) {
if (colNames == null || colNames.isEmpty() || data == null
|| data.isEmpty()) {
return false;
}
if (colNames.size() != data.elementAt(0).size()) {
return false;
}
return true;
}
/**
* validate columns,table data and whether columns's length equals to the
* length of row data or not.
*
* @param colNames
* @param data
* @return
*/
public static boolean validate(String[] colNames, String[][] data) {
if (colNames == null || colNames.length == 0 || data == null
|| data.length == 0) {
return false;
}
if (colNames.length != data[0].length) {
return false;
}
return true;
}
public static void main(String[] args) throws Exception {
String title = "XXX表格";
int cols = 4;
int rows = 10;
String[] colNames = new String[cols];
String[][] data = new String[rows][cols];
Vector<String> colNamesVec = new Vector<String>(cols);
Vector<Vector<String>> dataVec = new Vector<Vector<String>>();
String userDir = System.getProperty("user.dir");
String storeLoc = userDir+"//install//temp//Arr" + ".pdf";
String storeLocVec = userDir+"//install//temp//Vec"+ ".pdf";
for (int c = 0; c < cols; c++) {
String content = ((char) ('A' + c)) + "列";
colNames[c] = new String(content);
colNamesVec.add(content);
}
Vector<String> rowDataVec = null;
for (int r = 0; r < rows; r++) {
rowDataVec = new Vector<String>(cols);
for (int c = 0; c < cols; c++) {
String content = "值 " + "";
data[r][c] = content;
rowDataVec.add(content);
}
dataVec.add(rowDataVec);
}
PdfUtils.generatePdf(title, colNames, data, storeLoc);
PdfUtils.generatePdf(title, colNamesVec, dataVec, storeLocVec);
}
}