2013年9月25日 星期三

java-itext-example

// generate payroll pdf file
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.net.*;import java.io.*;import java.util.HashMap;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;
import java.sql.SQLException;import java.sql.Statement;

import java.util.Date;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * First iText example: Hello World.
 */
public class genPLPDF {
static String[][] dRaws=null;
static String[][] dResults=null;
    /** Path to the resulting PDF file. */
    public static final String RESULT
        = "/hr/payrollSlip.pdf";

    /**
     * Creates a PDF file: hello.pdf
     * @param    args    no arguments needed
     */
    public static void main(String[] args)
        throws DocumentException, IOException {
createPdf();
        new genPLPDF().createPdf(RESULT);
    }

public static void run() throws DocumentException, IOException {
createPdf();
new genPLPDF().createPdf(RESULT);
}

static public void createPdf(){

try{
String result="";
ResultSet rs = null;
Connection con = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/hr?useUnicode=true&characterEncoding=UTF8","root", "root");
stmt = con.createStatement();
String format="S,S,S,D,S,I,S,S"; // add column 1/5 step
String sqlString="select banker,bankerno,surname, salary,contracttype, employeenumber, departmentName,mpfnumber from employee ;"; // add column 2/5 step
result=sqlString;
String formats[]=format.split(",");
rs = stmt.executeQuery(sqlString);
while (rs.next()){String id=rs.getString(1);}rs.last(); //go bottom
int rowCount=rs.getRow(); //go recno
dRaws = new String[rowCount][];
rs.beforeFirst(); //go top
int iRecno=0;
String rD=""; // RowDelimiter
while (rs.next()){
dRaws[iRecno] = new String [20];  //insert column
result=result+rD+"[";
String fD="";  // fD=fieldDelimiter
for (int j=1; j<=formats.length; j++){
if (formats[j-1].equals("I")){
result=result+fD+Integer.toString(rs.getInt(j));
if (rs.getString(j).equals("No Value") || rs.getString(j).equals("null")){dRaws[iRecno][j-1]="0";}
else {dRaws[iRecno][j-1]=Integer.toString(rs.getInt(j));}}
if (formats[j-1].equals("D")){
result=result+fD+Double.toString(rs.getDouble(j));
dRaws[iRecno][j-1]=Double.toString(rs.getDouble(j));
}
if (formats[j-1].equals("L")){
result=result+fD+Long.toString(rs.getLong(j));
dRaws[iRecno][j-1]=Long.toString(rs.getLong(j));
}
if (formats[j-1].equals("S")){
result=result+fD+rs.getString(j);
dRaws[iRecno][j-1]=rs.getString(j);
}
fD=",";
}
result=result+"]";
rD=",\n";
++iRecno;
}


} catch (Exception e) {e.printStackTrace();}
}
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException
     * @throws    IOException
     */

///////////////////////////////////////
   private static void addEmptyLine(Paragraph paragraph, int number) {
    for (int i = 0; i < number; i++) {
      paragraph.add(new Paragraph(" "));
    }
}
  private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
      Font.BOLD);
  private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
      Font.NORMAL, BaseColor.RED);
  private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 13,
      Font.BOLD);
  private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
      Font.BOLD);

  private static Font Font12 = new Font(Font.FontFamily.TIMES_ROMAN, 12,
      Font.NORMAL);

 private static Font font1 = new Font(Font.FontFamily.HELVETICA  , 25, Font.BOLD);
      private static Font font2 = new Font(Font.FontFamily.COURIER    , 18,
    Font.ITALIC | Font.UNDERLINE);
      private static Font font3 = new Font(Font.FontFamily.TIMES_ROMAN, 27);


////////////////////////////////////////////
    public void createPdf(String filename)
    throws DocumentException, IOException {

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();

        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
dResults = new String[dRaws.length][];
int fieldCount=20;
for(int i=0;i<dRaws.length; i++){
Anchor anchor = new Anchor("First Chapter", catFont);
anchor.setName("First Chapter");
Chapter catPart = new Chapter(new Paragraph(anchor), 1);
Paragraph subPara = new Paragraph("Subcategory 1", subFont);
Section subCatPart = catPart.addSection(subPara);
Paragraph paragraph = new Paragraph(); // new line newline

paragraph = new Paragraph("Payroll Slip", catFont);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);

Paragraph preface = new Paragraph();
addEmptyLine(preface, 3);


document.add(new Paragraph(" "));
document.add(new Paragraph(" "));


//-------------------------
PdfPTable table = new PdfPTable(4);  // number of column
table.setWidths(new int[]{560,300,600,300}); // column widths
table.setWidthPercentage(100); // tables width 100%
    // t.setBorderColor(BaseColor.GRAY);
    // t.setPadding(4);
    // t.setSpacing(4);
    // t.setBorderWidth(1);

table.getDefaultCell().setBorder(0); // set itext-table-border-width to 0

/*
    PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase("Table Header 2"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase("Table Header 3"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);
    table.setHeaderRows(1);
*/
PdfPCell c1 = new PdfPCell(new Phrase(""));

    c1 = new PdfPCell(new Phrase("Staff Name"));
c1.setBorder(0);
    c1.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(c1);


//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



    table.addCell(dRaws[i][2]);
 

c1 = new PdfPCell(new Phrase("Date"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
c1 = new PdfPCell(new Phrase(""+dateFormat.format(date)));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
 


    table.addCell("Offical name");
    table.addCell("Lee s l");
c1 = new PdfPCell(new Phrase("Period (From)"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
 
  c1 = new PdfPCell(new Phrase("17/7/2013"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
 

    table.addCell("Position");
    table.addCell("IT Engineer");
c1 = new PdfPCell(new Phrase("Period (to)"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
  c1 = new PdfPCell(new Phrase("31/7/2013"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
 

    table.addCell("");
    table.addCell("");
c1 = new PdfPCell(new Phrase("Total Working Hour"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
c1 = new PdfPCell(new Phrase("90"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
 

    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");

    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");

    table.addCell("Earnings");
c1 = new PdfPCell(new Phrase("Amount (HK$)"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
    table.addCell("Deducations");
c1 = new PdfPCell(new Phrase("Amount (HK$)"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
 
    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");

    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");

    table.addCell("Basic Monthly Salary");
c1 = new PdfPCell(new Phrase("3500.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
    table.addCell("Late Penalty");
c1 = new PdfPCell(new Phrase("0.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
 

    table.addCell("Salary pay back / Adjustment");
c1 = new PdfPCell(new Phrase("0.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
    table.addCell("Salary Adjustment");
c1 = new PdfPCell(new Phrase("0.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);

    table.addCell("Bonus/Incentive");
c1 = new PdfPCell(new Phrase("0.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
    table.addCell("Absent / Sick Leave");
  c1 = new PdfPCell(new Phrase("0.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);

    table.addCell("Others");
c1 = new PdfPCell(new Phrase("0.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
    table.addCell("Others");
c1 = new PdfPCell(new Phrase("0.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);


    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");

    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");


c1 = new PdfPCell(new Phrase("Gross Income <a>",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(c1);
c1 = new PdfPCell(new Phrase("0.00",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
c1 = new PdfPCell(new Phrase("Total Deductions <b>",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(c1);
c1 = new PdfPCell(new Phrase("0.00",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);

    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");

    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");



c1 = new PdfPCell(new Phrase("Relevant Income <c=a-b>",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(c1);
 
c1 = new PdfPCell(new Phrase("0.00",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);

c1 = new PdfPCell(new Phrase("Already Paid <d>",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(c1);
 
c1 = new PdfPCell(new Phrase("0.00",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);


c1 = new PdfPCell(new Phrase("Net pay <e=c-d-f>",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(c1);
 
c1 = new PdfPCell(new Phrase("0.00",subFont));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
    table.addCell("Employee MPF Contribution < 1");
  c1 = new PdfPCell(new Phrase("0.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);

    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");

    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");


table.addCell("Employer MPF Contribution");
  c1 = new PdfPCell(new Phrase("0.00"));
c1.setBorder(0);
c1.setPaddingRight(18);
    c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(c1);
    table.addCell("Commerence Date :");
    table.addCell("17 July 2013");

    table.addCell("Bank A/C#");
    table.addCell("BOC 12345678902");
    table.addCell("");
    table.addCell("");

    table.addCell("");
    table.addCell("");
    table.addCell("");
    table.addCell("");

    //subCatPart.add(table);
document.add(table);
preface.add(new Paragraph("This is an offical payroll slip. Neither signature nor acknowledgement is required.", smallBold));
preface.add(new Paragraph("***Please check this payroll slip carefully and refer any quiery to Admin Dept***", smallBold));
document.add(preface);
//-------------------------

//preface.add(new Paragraph("Payroll Slip", catFont));
//addEmptyLine(preface, 1);

// Will create: Report generated by: _name, _date

/*
preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
smallBold));
addEmptyLine(preface, 3);
preface.add(new Paragraph("This document describes something which is very important ",
smallBold));
addEmptyLine(preface, 8);
// RED Color   preface.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",redFont));
document.add(preface);
*/
// Start a new page


/* left
paragraph.setAlignment(Element.ALIGN_RIGHT);
document.add(paragraph);
*/

// Centered
paragraph = new Paragraph("");
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
// Left
paragraph = new Paragraph("");
paragraph.setAlignment(Element.ALIGN_LEFT);
document.add(paragraph);
// Left with indentation
paragraph = new Paragraph("");
paragraph.setAlignment(Element.ALIGN_LEFT);
paragraph.setIndentationLeft(50);
document.add(paragraph);
document.newPage();
//////////////////////
}
        // step 5
        document.close();
    }
}

沒有留言:

張貼留言