com.icesoft.faces.component.dataexporter.PDFOutputHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of icefaces-compat Show documentation
Show all versions of icefaces-compat Show documentation
${icefaces.product.name} Compat Component Library
The newest version!
/*
* Copyright 2004-2013 ICEsoft Technologies Canada Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package com.icesoft.faces.component.dataexporter;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class PDFOutputHandler extends OutputTypeHandler {
protected String title;
protected ArrayList headers; // Index is column
protected ArrayList footers; // Index is column
protected ArrayList rowsOfData; // Index is row, element is ArrayList of column data
protected String orientation; // landscape, portrait (default)
protected String pageSize; // default: A4
protected String headerFont; // Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique,
// Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique,
// Symbol, Times, Times-Roman (default), Times-Bold, Times-Italic, Times-BoldItalic
protected String cellFont; // default: Times-Roman
protected int headerFontSize; // default: 9
protected int cellFontSize; // default: 8
public PDFOutputHandler(String path, String title) {
super(path);
this.title = title;
this.mimeType = "application/pdf";
this.headers = new ArrayList();
this.footers = new ArrayList();
this.rowsOfData = new ArrayList();
this.orientation = "portrait";
this.pageSize = "A4";
this.headerFont = "Times-Roman";
this.cellFont = "Times-Roman";
this.headerFontSize = 9;
this.cellFontSize = 8;
if (!REFLECTION_LOADED) {
throw new IllegalStateException("iText library not found, can not export dataTable to PDF");
}
}
/**
* The row indexing is zero based, from the perspective of the row data,
* ignoring how many rows were used for the header
*/
public void writeCell(Object output, int col, int row) {
rowsOfData.ensureCapacity(row);
while (row >= rowsOfData.size()) {
rowsOfData.add(new ArrayList());
}
ArrayList rowData = (ArrayList) rowsOfData.get(row);
rowData.ensureCapacity(col);
while (col >= rowData.size()) {
rowData.add(null);
}
rowData.set(col, output);
}
public void writeHeaderCell(String text, int col) {
headers.ensureCapacity(col);
while (col >= headers.size()) {
headers.add(null);
}
headers.set(col, text);
}
/**
* The row indexing is zero based, from the perspective of the row data,
* ignoring how many rows were used for the header
*/
public void writeFooterCell(Object output, int col, int row) {
footers.ensureCapacity(col);
while (col >= footers.size()) {
footers.add(null);
}
footers.set(col, output);
}
public void flushFile() {
try{
FileOutputStream fos = new FileOutputStream(this.getFile());
BufferedOutputStream bos = new BufferedOutputStream(fos,64*1014);
printPDF(bos);
bos.flush();
fos.flush();
bos.close();
fos.close();
}
catch(Exception e){
e.printStackTrace();
}
}
protected void printPDF(OutputStream osPDF)
throws Exception { // DocumentException, IOException
//Initialize document
Object document = new_Document();
//Apply Document Properties
applyDocumentProperties(document);
Object writer = PdfWriter_getInstance(document, osPDF);
//we open the document
Document_open(document);
PdfWriter_setOpenAction(writer);
//Generate Contents
//Add Header If provided
printHeaderTitle(document,this.title);
//Print Table Contents
printTableData(document);
Document_close(document);
}
protected void applyDocumentProperties(Object document) throws Exception { // DocumentException
//Find Document Size and Orientation
Object page = PageSize_getRectangle(this.pageSize);
// Check for Page Orientation
if(nvl(this.orientation).equalsIgnoreCase("LANDSCAPE")){
Document_setPageSize(document, Rectangle_rotate(page));
}else{
Document_setPageSize(document, page);
}
}
protected void printHeaderTitle(Object document, String headerText) throws Exception { // DocumentException
if(this.title!=null && this.title.length()>0){
Object font = FontFactory_getFont_BOLD(com_lowagie_text_FontFactory_TIMES_ROMAN, 14.0f);
Object ck = new_Chunk(headerText, font);
Object phrase = new_Phrase(ck);
Object paragraph = new_Paragraph(phrase);
Paragraph_setAlignment_ALIGN_CENTER(paragraph);
Document_add(document, paragraph);
}
}
protected void printTableData(Object document) throws Exception { // DocumentException
try{
//Find the number of Columns
int length = calculateColumnCount();
//Set table widths
float[] tWidth = new float[length];
for(int wi=0;wi 0;
}
/**
* @param table
* @return Number of rows of data added to the table
*/
protected int printTableContents(Object table, boolean wasHeader) throws Exception { // DocumentException
// Check Header Font and Font Size
String cellFontProp = nvl(this.cellFont);
//Check for Font Size
float fCellSize = this.cellFontSize;
Object font = FontFactory_getFont(cellFontProp, fCellSize);
java.awt.Color backGroundColor = java.awt.Color.white;
int headerRowOffset = wasHeader ? 1 : 0;
//Add Table Data
int numRows = this.rowsOfData.size();
for(int j=0;j