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();
    }
}

沒有留言:

張貼留言