2014年2月28日 星期五

PDFView.java 20140228

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class PDFView extends javax.servlet.http.HttpServlet {
static String db = "";
static String viewFile = "";
// The MIME type associated with the kind of media to be served.// For more info see
// http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html

//static String CONTENT_TYPE = "application/pdf";
static String CONTENT_TYPE = "";
public PDFView() {
}

public void doGet(HttpServletRequest request, HttpServletResponse response) {
processRequest(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
}

public void processRequest(HttpServletRequest request,
HttpServletResponse response) {

try {
String ContentType="";
db=request.getParameter("db");
viewFile=request.getParameter("viewFile");
HttpSession session = request.getSession();

ContentType=request.getParameter("ContentType");
if (ContentType.indexOf("ms-excel")>=0){
ContentType="application/vnd.ms-excel";
}
CONTENT_TYPE = ContentType;
response.setContentType(this.CONTENT_TYPE);
response.setHeader("Content-Disposition",
"inline; filename="+viewFile);
if (this.CONTENT_TYPE.indexOf("excel")>=0){}

if (session.isNew() || session.getAttribute("Login") == null) {
//response.sendRedirect("index.jsp");
}
File file1 = new File("/erp/"+db+"/"+viewFile);
FileInputStream fs = new FileInputStream(file1);
byte[] byteArray = new byte[(int) file1.length()];
fs.read(byteArray);
fs.close();
response.getOutputStream().write(byteArray);
} catch (Exception e) {
this.outErrorMessage(response,
" Some exception occurred: " + e.getMessage());
return;
}
}

private void outErrorMessage(HttpServletResponse response, String errorMsg) {
response.setContentType("text/plain");
try {
PrintWriter out = response.getWriter();
out.println(errorMsg);
} catch (Exception e) {
}
}

}

poGenPDF.java 20140228

// init.variable
// load.data
// body.sql.Statement
// load.data.body

// it cannot run stand alone. It must call by PDFCreateAndView.java
// e.g. PDFCreateAndView?db=erp&create=po&file=po1234&recordid=po14001
import com.erp.utils.CompanyProfile;
import java.io.FileOutputStream;
import java.io.IOException;
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 java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar; 
import java.text.*;
import com.itextpdf.text.pdf.*;// PdfPCell,PdfPTable,PdfWriter,PdfContentByte, PdfImportedPage,PdfReader,PdfWriter,PdfPCell,PdfPTable,PdfWriter, AcroFields,BaseFont,PdfReader,PdfStamper,PdfWriter,TextField,BaseFont,FontSelector,PdfWriter,
import com.itextpdf.text.*; //Font,FontFactory,PageSize,Paragraph,Phrase,Document,DocumentException
//Anchor,BadElementException,BaseColor,Chapter,Image,Element,List,ListItem,Paragraph,Phrase,Chunk,Section
//Rectangle;
//import com.itextpdf.text.pdf.iTextAsian;

public class poGenPDF {
CompanyProfile CompanyProfile=new CompanyProfile();
static String mCompanyName="";
static String mAddress1="";
static String[][] dRaws=null;
static String[][] dResults=null;
static String db = "";
static String fileName = "";
static String recordID = "";
public static final String RESULT = "/erp/"+db+"/"+fileName+".pdf";
public static void main(String[] args)
throws DocumentException, IOException {
createPdf();new poGenPDF().createPdf(RESULT);
}
public static void run() throws DocumentException, IOException {
String xRESULT = "/erp/"+db+"/"+fileName+".pdf";
createPdf();new poGenPDF().createPdf(xRESULT);
}
static public void createPdf(){
}
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
 paragraph.add(new Paragraph(" "));
}
}

private static void addTableCell(PdfPTable table,String cellValue,Font font,String align,String border) {
PdfPCell cell = null;
cell = new PdfPCell(new Phrase(cellValue,font));
cell.setBorder(0);
if (border.equals("border:box")){
cell.setBorder(Rectangle.BOX);
} else {
cell.setBorder(Rectangle.NO_BORDER);
}
if (align.equals("text-align:right")){
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
} else if (align.equals("text-align:center")) {
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
} else {
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
}
table.addCell(cell); //row 1 right
}


private static Font font7b = new Font(Font.FontFamily.TIMES_ROMAN, 7,Font.BOLD);
private static Font font8 = new Font(Font.FontFamily.TIMES_ROMAN, 8);
private static Font font9 = new Font(Font.FontFamily.TIMES_ROMAN, 9);
private static Font font11 = new Font(Font.FontFamily.TIMES_ROMAN, 11);
private static Font font11b = new Font(Font.FontFamily.TIMES_ROMAN, 11,Font.BOLD);
private static Font font12 = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL);
private static Font font12b = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);
private static Font font12R = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL, BaseColor.RED);  
private static Font font14b = new Font(Font.FontFamily.TIMES_ROMAN, 14,Font.BOLD);
private static Font font25b = new Font(Font.FontFamily.HELVETICA  , 25, Font.BOLD);
private static Font font18il = new Font(Font.FontFamily.COURIER    , 18, Font.ITALIC | Font.UNDERLINE);
private static Font font18b = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
private static Font font27 = new Font(Font.FontFamily.TIMES_ROMAN, 27);  

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


try{
// init.variable
DecimalFormat currencyFormat = new DecimalFormat("#0.00");
DecimalFormat integerFormat = new DecimalFormat("#0");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
String COMPANY_NAME="Top Level Corporation Limited";
String COMPANY_ADDRESS0="Flat 1602, Fook Hong Industrial Building,";
String COMPANY_ADDRESS1="19 Sheung Yuet Road, Kowloon Bay, Kowloon, HK";
String COMPANY_TEL="852-8102 2620";
String COMPANY_FAX="852-8102 2623";
String COMPANY_LOGO="/erp/images/Database-bottomlogo.jpg";
String COMPANY_AUTHORIZED="Alvin Chan";
String DOCUMENT_NAME="PURCHASE ORDER";
String PARTY=null;
String dbID="root";
String dbPassword="root";
String paymentTerm=null;
String poRemark=null;
String poNo=null;
String poSourceRef=null;
String poVer=null;
String poSupplier=null;
String poDate=null;
String poPaymentTerms=null;
String poContactName=null;
String shipToCompany=null;
String shipToContactName=null;
String shipToAddress=null;
String shipToTel=null;
String result="";
String supplierRef=null;
String poAddress=null;
String poTel=null;
String poTotalAmount=null;


// load.data///////////////// read data from mysql ///////////////////////////////////////

ResultSet rs = null;
ResultSet rsB = null;
Connection con = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/"+"BR201312111529"+"?useUnicode=true&characterEncoding=UTF8",dbID, dbPassword);
stmt = con.createStatement();
String documentHeaderSqlString="select A.PURCHASE_ORDER,A.version,A.OUR_REF_NO,A.date,A.YOUR_REF_NO,A.PAYMENT_TERMS ,B.NAME,A.COMPANY_NAME,A.CONTACT_NAME,A.SHIP_TO_CONTACT, A.CONTACT_ADDRESS,A.SHIP_TO_ADDRESS ,A.CONTACT_TEL,A.SHIP_TO_TEL,C.PO_AMOUNT,A.REMARK from PURCHASE_HEADER A,VENDOR B,POTATAMT C where A.SUPPLIER=B.RECORDID AND A.RECORDID=C.PARENT_recordid  and A.recordID="+recordID+";";
rs = stmt.executeQuery(documentHeaderSqlString);
// body.sql.Statement
String documentLineSqlString="SELECT ITEM, MODEL , DESCRIPTION ,QTY,UNIT, PRICE, round(QTY*PRICE,2) FROM PO WHERE PARENT_RECORDID="+recordID+";";
String documentLineFormat="S,S,S,D,S,C,C"; // add column 1/5 step
String formats[]=documentLineFormat.split(","); 
if (rs.next()){
poNo=rs.getString(1);
poVer=rs.getString(2);
poSourceRef=rs.getString(3);
poDate=rs.getString(4);
supplierRef=rs.getString(5);
paymentTerm=rs.getString(6);
poSupplier=rs.getString(7);
shipToCompany=rs.getString(8);
poContactName=rs.getString(9);
shipToContactName=rs.getString(10);
poAddress=rs.getString(11);
shipToAddress=rs.getString(12);
poTel=rs.getString(13);
shipToTel=rs.getString(14);
poTotalAmount=(String) currencyFormat.format(rs.getDouble(15));
poRemark=rs.getString(16);;
}
PARTY=poSupplier;

rs = stmt.executeQuery(documentLineSqlString);
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
// load.data.body
while (rs.next()){
dRaws[iRecno] = new String [40];  //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")){
if ((rs.getDouble(j)%1)==0){
result=result+fD+(String) integerFormat.format(rs.getDouble(j));
dRaws[iRecno][j-1]=(String) integerFormat.format(rs.getDouble(j));
} else {
result=result+fD+Double.toString(rs.getDouble(j));
dRaws[iRecno][j-1]=Double.toString(rs.getDouble(j));
}
}
if (formats[j-1].equals("C")){
result=result+fD+Double.toString(rs.getDouble(j));
dRaws[iRecno][j-1]=(String) currencyFormat.format(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;
}

/////////////////////////////////////////////////////////////////////////


/////////////////////////////////// Begin Print Data ////////////////////////////////
Paragraph paragraph = new Paragraph(); 
PdfPTable table=null;
PdfPCell cell = null;
    Document document = new Document(PageSize.A4, 5, 20, 20, 5);
OutputStream file = new FileOutputStream(new File(filename));
PdfWriter.getInstance(document, file);
document.open();
Image img = Image.getInstance(COMPANY_LOGO);
img.scaleToFit(180, 100);
table = new PdfPTable(3);
table.setWidths(new int[]{200,400,700});
cell = new PdfPCell(img);
cell.setColspan(2);
cell.setRowspan(4);
cell.setBorder(0);
table.addCell(cell);


addTableCell(table,COMPANY_NAME,font14b,"text-align:left","");
addTableCell(table,COMPANY_ADDRESS0,font11,"text-align:left","");
addTableCell(table,COMPANY_ADDRESS1,font11,"text-align:left","");
addTableCell(table,"Tel: "+COMPANY_TEL+"Fax:"+COMPANY_FAX,font11,"text-align:left","");
document.add(table);


paragraph = new Paragraph("______________________________________________________________________________________", font11); 
paragraph.setAlignment(Element.ALIGN_CENTER); 
document.add(paragraph);

paragraph = new Paragraph(DOCUMENT_NAME+" NO. "+poNo+poVer, font14b); 
paragraph.setAlignment(Element.ALIGN_CENTER); 
paragraph.add(new Paragraph(" "));
paragraph.add(new Paragraph(" "));
document.add(paragraph);

//////////////////////////////////// po_header ////////////////////////////////////
table = new PdfPTable(4);  
table.getDefaultCell().setBorder(1);
table.setWidthPercentage(90);
table.setWidths(new int[]{200,400,200,400});
addTableCell(table,"OurRef.No.:",font11b,"text-align:right","border:box");
addTableCell(table,poSourceRef,font11,"text-align:left","border:box");
addTableCell(table,"P.O. Date:",font11b,"text-align:right","border:box");
addTableCell(table,poDate,font11,"text-align:left","border:box");
addTableCell(table,"Your Ref.No.:",font11b,"text-align:right","border:box");
addTableCell(table,supplierRef,font11,"text-align:left","border:box");
addTableCell(table,"Terms:",font11b,"text-align:right","border:box");
addTableCell(table,paymentTerm,font11,"text-align:left","border:box");
document.add(table);

paragraph = new Paragraph("", font14b);
paragraph.add(new Paragraph(" "));
document.add(paragraph);

table = new PdfPTable(4);  
table.getDefaultCell().setBorder(1);
table.setWidthPercentage(90);
table.setWidths(new int[]{200,400,200,400});
//////////////////////////////// More po_Header ///////////////
addTableCell(table,"Supplier:",font11b,"text-align:right","border:box");
addTableCell(table,PARTY,font11,"text-align:left","border:box");
addTableCell(table,"Ship To:",font11b,"text-align:right","border:box");
addTableCell(table,shipToCompany,font11,"text-align:left","border:box");
addTableCell(table,"Name:",font11b,"text-align:right","border:box");
addTableCell(table,poContactName,font11,"text-align:left","border:box");
addTableCell(table,"Name:",font11b,"text-align:right","border:box");
addTableCell(table,shipToContactName,font11,"text-align:left","border:box");
addTableCell(table,"Address:",font11b,"text-align:right","border:box");
addTableCell(table,poAddress,font11,"text-align:left","border:box");
addTableCell(table,"Address:",font11b,"text-align:right","border:box");
addTableCell(table,shipToAddress,font11,"text-align:left","border:box");
addTableCell(table,"Telephone:",font11b,"text-align:right","border:box");
addTableCell(table,poTel,font11,"text-align:left","border:box");
addTableCell(table,"Telephone:",font11b,"text-align:right","border:box");
addTableCell(table,shipToTel,font11,"text-align:left","border:box");
document.add(table);

paragraph = new Paragraph("", font14b);
paragraph.add(new Paragraph(" "));
document.add(paragraph);

table = new PdfPTable(7);  
table.getDefaultCell().setBorder(1);
table.setWidthPercentage(90);
table.setWidths(new int[]{100,150,450,100,100,150,150});
//////////////////////////////// More Detail 
addTableCell(table,"Item",font11b,"text-align:left","border:box");
addTableCell(table,"Product Number",font11b,"text-align:left","border:box");
addTableCell(table,"Description",font11b,"text-align:left","border:box");
addTableCell(table,"Qty",font11b,"text-align:right","border:box");
addTableCell(table,"Unit",font11b,"text-align:left","border:box");
addTableCell(table,"Unit Price (HK)",font11b,"text-align:right","border:box");
addTableCell(table,"Amount (HK)",font11b,"text-align:right","border:box");
for(int i=0;i<dRaws.length; i++){
addTableCell(table,dRaws[i][0],font11,"text-align:left","border:box");
addTableCell(table,dRaws[i][1],font11,"text-align:left","border:box");
addTableCell(table,dRaws[i][2],font11,"text-align:left","border:box");
addTableCell(table,dRaws[i][3],font11,"text-align:right","border:box");
addTableCell(table,dRaws[i][4],font11,"text-align:left","border:box");
addTableCell(table,dRaws[i][5],font11,"text-align:right","border:box");
addTableCell(table,dRaws[i][6],font11,"text-align:right","border:box");
}
document.add(table);

///////////////////////// total /////////////////////////////////////////
table = new PdfPTable(7);  
table.getDefaultCell().setBorder(1);
table.setWidthPercentage(90);
table.setWidths(new int[]{100,150,450,100,100,150,150});
table.addCell("");table.addCell("");table.addCell("");table.addCell("");
cell = new PdfPCell(new Phrase("Net Due (HKD)", font11b));
cell.setColspan(2);
cell.setRowspan(1);
cell.setBorder(1);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
addTableCell(table,poTotalAmount,font11,"text-align:right","border:box");
document.add(table);

table = new PdfPTable(1);  
table.getDefaultCell().setBorder(0);
table.setWidthPercentage(90);
table.setWidths(new int[]{1200});
cell = new PdfPCell(new Phrase("Remarks :", font14b));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase(poRemark, font12));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //1

document.add(table);
paragraph.add(new Paragraph(" "));
document.add(paragraph);
/////////////////////////////////////// BODY end

///////////////////////// Footer Signature ////////////////////////////////////////////
table = new PdfPTable(4);  
table.getDefaultCell().setBorder(0);
table.setWidthPercentage(90);
table.setWidths(new int[]{50,550,50,550});

addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,"For and on behalf of",font12b,"text-align:left","");
addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,"Confirm and accepted by",font12b,"text-align:left","");
addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,COMPANY_NAME,font12b,"text-align:left","");
addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,PARTY,font12b,"text-align:left","");

cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
for (int i = 0; i < 4*6; i++) {table.addCell(cell);}

addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,"__________________________",font12,"text-align:left","");
addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,"__________________________",font12,"text-align:left","");
addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,"Authorized Signature(s)",font12,"text-align:left","");
addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,"Authorized Signature(s)",font12,"text-align:left","");
addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,"Name: "+COMPANY_AUTHORIZED,font12,"text-align:left","");
addTableCell(table,"",font12,"text-align:left","");
addTableCell(table,"Date:",font12,"text-align:left","");

document.add(table);
document.close();
file.close();
} catch (Exception e) {e.printStackTrace();}
  }
}

2014年2月27日 星期四

mysql-roundup

mysql round

select round(0.12345,4);   >> 0.1235
select round(0.12344,4);   >> 0.1234

java-translate 1234.1231345 -> 1234.12

java-translate
java roundup
java-numeric format

  DecimalFormat formatter = new DecimalFormat("#0.00");
    double d = 4.0;
    System.out.println(formatter.format(d));

import java.text.*;


DecimalFormat currencyFormat = new DecimalFormat("#0.00");   


poTotalAmount=(String) currencyFormat.format(rs.getDouble(15));

asp-session

session("a")=nothing
session("a")="newValue"

asp-select asp-case


dim str1
select case day(date)
case 1,2,3,4,5,6,7,8,9,10
str1="month first"
case 11,12,13,14,15,16,17,18,19,20
str1="month middle"
case else
str1="month tail"
End Select

2014年2月26日 星期三

PDFCreatePOTemplate.java 20140227

// it cannot run stand alone. It must call by PDFCreateAndView.java
// e.g. PDFCreateAndView?db=erp&create=po&file=po1234&recordid=po14001
import com.erp.utils.CompanyProfile;
import java.io.FileOutputStream;
import java.io.IOException;
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 java.text.DateFormat;
import java.text.SimpleDateFormat;
import com.itextpdf.text.pdf.*;// PdfPCell,PdfPTable,PdfWriter,PdfContentByte, PdfImportedPage,PdfReader,PdfWriter,PdfPCell,PdfPTable,PdfWriter, AcroFields,BaseFont,PdfReader,PdfStamper,PdfWriter,TextField,BaseFont,FontSelector,PdfWriter,
import com.itextpdf.text.*; //Font,FontFactory,PageSize,Paragraph,Phrase,Document,DocumentException
//Anchor,BadElementException,BaseColor,Chapter,Image,Element,List,ListItem,Paragraph,Phrase,Chunk,Section
//Rectangle;
//import com.itextpdf.text.pdf.iTextAsian;

import java.util.Date;
import java.util.Calendar; 
public class PDFCreatePOTemplate {
CompanyProfile CompanyProfile=new CompanyProfile();
static String mCompanyName="";
static String mAddress1="";
static String[][] dRaws=null;
static String[][] dResults=null;
static String db = "";
static String fileName = "";
static String recordID = "";
public static final String RESULT = "/erp/"+db+"/"+fileName+".pdf";
public static void main(String[] args)
throws DocumentException, IOException {
createPdf();new poGenPDF().createPdf(RESULT);
}
public static void run() throws DocumentException, IOException {
String xRESULT = "/erp/"+db+"/"+fileName+".pdf";
createPdf();new poGenPDF().createPdf(xRESULT);
}
static public void createPdf(){
}
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
 paragraph.add(new Paragraph(" "));
}
}

private static Font font7b = new Font(Font.FontFamily.TIMES_ROMAN, 7,Font.BOLD);
private static Font font8 = new Font(Font.FontFamily.TIMES_ROMAN, 8);
private static Font font9 = new Font(Font.FontFamily.TIMES_ROMAN, 9);
private static Font font11 = new Font(Font.FontFamily.TIMES_ROMAN, 11);
private static Font font11b = new Font(Font.FontFamily.TIMES_ROMAN, 11,Font.BOLD);
private static Font font12b = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL);
private static Font font12R = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL, BaseColor.RED);  
private static Font font14b = new Font(Font.FontFamily.TIMES_ROMAN, 14,Font.BOLD);
private static Font font25b = new Font(Font.FontFamily.HELVETICA  , 25, Font.BOLD);
private static Font font18il = new Font(Font.FontFamily.COURIER    , 18, Font.ITALIC | Font.UNDERLINE);
private static Font font18b = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
private static Font font27 = new Font(Font.FontFamily.TIMES_ROMAN, 27);  

  public void createPdf(String filename)
    throws DocumentException, IOException {
String COMPANY_NAME="Company  Corporation Limited";
String COMPANY_ADDRESS0="Industrial Building,";
String COMPANY_ADDRESS1=" Kowloon, HK";
String COMPANY_TEL="852-1234 5678 ";
String COMPANY_FAX="852-8765 4321";
String COMPANY_LOGO="/erp/images/logoFile.jpg";
String COMPANY_AUTHORIZED="Authorized Name";
String DOCUMENT_NAME="PURCHASE ORDER";
String PARTY="Supplier Name  Limited";

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
Paragraph paragraph = new Paragraph(); 
PdfPTable table=null;
PdfPCell cell = null;
    Document document = new Document(PageSize.A4, 5, 20, 20, 5);
OutputStream file = new FileOutputStream(new File(filename));
PdfWriter.getInstance(document, file);
document.open();
Image img = Image.getInstance(COMPANY_LOGO);
img.scaleToFit(180, 100);
table = new PdfPTable(3);
table.setWidths(new int[]{200,400,700});
cell = new PdfPCell(img);
cell.setColspan(2);
cell.setRowspan(4);
cell.setBorder(0);
table.addCell(cell);
cell = new PdfPCell(new Phrase(COMPANY_NAME, font14b));
cell.setBorder(0);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //row 1 right
cell = new PdfPCell(new Phrase(COMPANY_ADDRESS0, font11));
cell.setBorder(0);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //row 2 right
cell = new PdfPCell(new Phrase(COMPANY_ADDRESS1, font11));
cell.setBorder(0);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //row 3 right
cell = new PdfPCell(new Phrase("Tel: "+COMPANY_TEL+"Fax:"+COMPANY_FAX, font11));
cell.setBorder(0);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //row 4 right
document.add(table);


paragraph = new Paragraph("______________________________________________________________________________________", font11); 
paragraph.setAlignment(Element.ALIGN_CENTER); 
document.add(paragraph);

paragraph = new Paragraph(DOCUMENT_NAME+" NO. "+recordID, font14b); 
paragraph.setAlignment(Element.ALIGN_CENTER); 
paragraph.add(new Paragraph(" "));
paragraph.add(new Paragraph(" "));
document.add(paragraph);


//////////////////////////////////// document header ////////////////////////////////////
table = new PdfPTable(4);  
table.getDefaultCell().setBorder(1);
table.setWidthPercentage(90);
table.setWidths(new int[]{200,400,200,400});
/////////////////////////////////////////////////////////////////////////////////////////
cell = new PdfPCell(new Phrase("OurRef.No.:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("OurRef.No.: value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //2
cell = new PdfPCell(new Phrase("P.O. Date:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //3
cell = new PdfPCell(new Phrase("P.O. Data: Value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //4
/////////////////////////////////////////////////////////////////////////////////////////////
cell = new PdfPCell(new Phrase("Your Ref.No.:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Your Ref.No.: value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //2
cell = new PdfPCell(new Phrase("Terms:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //3
cell = new PdfPCell(new Phrase("TERMS: Value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //4
document.add(table);
paragraph = new Paragraph("", font14b);
paragraph.add(new Paragraph(" "));
document.add(paragraph);
table = new PdfPTable(4);  
table.getDefaultCell().setBorder(1);
table.setWidthPercentage(90);
table.setWidths(new int[]{200,400,200,400});
//////////////////////////////// More Detail 
/////////////////////////////////////////////////////////////////////////////////////////////
cell = new PdfPCell(new Phrase("Supplier:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase(PARTY, font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //2
cell = new PdfPCell(new Phrase("Ship To:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //3
cell = new PdfPCell(new Phrase("Ship To: Value", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //4
cell = new PdfPCell(new Phrase("Name:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Name: value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //2
cell = new PdfPCell(new Phrase("Name:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //3
cell = new PdfPCell(new Phrase("Name: Value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //4
/////////////////////////////////////////////////////////////////////////////////////////////
cell = new PdfPCell(new Phrase("Address:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Address: value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //2
cell = new PdfPCell(new Phrase("Address:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //3
cell = new PdfPCell(new Phrase("Address: Value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //4
/////////////////////////////////////////////////////////////////////////////////////////////
cell = new PdfPCell(new Phrase("Telephone :", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Telephone: value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //2
cell = new PdfPCell(new Phrase("Telephone:", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //3
cell = new PdfPCell(new Phrase("Telephone: Value", font11));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //4
/////////////////////////////////////////////////////////////////////////////////////////////
document.add(table);

/////////////////////////////////////// BODY 
paragraph = new Paragraph("", font14b);
paragraph.add(new Paragraph(" "));
document.add(paragraph);
table = new PdfPTable(7);  
table.getDefaultCell().setBorder(1);
table.setWidthPercentage(90);
table.setWidths(new int[]{100,150,450,100,100,150,150});

//////////////////////////////// More Detail 
/////////////////////////////////////////////////////////////////////////////////////////////
cell = new PdfPCell(new Phrase("Item", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Product Number", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Description", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Qty", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Unit", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Unit Price (HKD)", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
cell = new PdfPCell(new Phrase("Amount (HKD)", font11b));
cell.setBorder(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell); //1
document.add(table);

///////////////////////// total /////////////////////////////////////////
table = new PdfPTable(7);  
table.getDefaultCell().setBorder(1);
table.setWidthPercentage(90);
table.setWidths(new int[]{100,150,450,100,100,150,150});
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
cell = new PdfPCell(new Phrase("Net Due (HKD)", font11b));
cell.setColspan(2);
cell.setRowspan(1);
cell.setBorder(1);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("7,568.00", font11));
cell.setColspan(2);
cell.setRowspan(1);
cell.setBorder(1);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
document.add(table);

table = new PdfPTable(1);  
table.getDefaultCell().setBorder(0);
table.setWidthPercentage(90);
table.setWidths(new int[]{1200});
cell = new PdfPCell(new Phrase("Remark", font14b));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell); //1
document.add(table);
/////////////////////////////////////// BODY 


table = new PdfPTable(4);  
table.getDefaultCell().setBorder(0);
table.setWidthPercentage(90);
table.setWidths(new int[]{50,550,50,550});

//////////////////////////////// More Detail 
/////////////////////////////////////////////////////////////////////////////////////////////
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell); 
cell = new PdfPCell(new Phrase("For and on behalf of", font12b));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell); 
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell); 
cell = new PdfPCell(new Phrase("Confirm and accepted by", font12b));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell); 
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell); 
cell = new PdfPCell(new Phrase(COMPANY_NAME, font12b));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell); 
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell); 
cell = new PdfPCell(new Phrase(PARTY, font12b));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell); 

cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);

for (int i = 0; i < 4*6; i++) {
table.addCell(cell); 
}
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("__________________________"));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("__________________________"));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);

cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Authorized Signature(s)"));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Authorized Signature(s)"));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Name: "+COMPANY_AUTHORIZED));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Date"));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);

document.add(table);


document.close();
file.close();
    }
}