posts - 8,  comments - 1,  trackbacks - 0

我的评论

re: Swing不丑系列:JComboBox[未登录] Charlie 2011-03-24 23:09  
支持楼主
re: 程序员小史记011[未登录] charlie 2008-10-15 08:17  
@青鸟
dennis
呵,仅仅是个公司化名,made in Singapore~~

-------------------- 是NCS吗?
re: 破解 Geneious Pro 3.0.6[未登录] charlie 2008-10-11 18:19  
我也要一份啊!非常感谢哈!!
Email:charlie.paradise@163.com
re: null和字符串的恩怨 charlie 2007-11-28 15:19  
//method for pdf

private void generatePDF(
HttpSession session, String fileName, DbHandler dh)
throws SQLException, ClassNotFoundException{

//得到pdf文件名


try{
//设置响应
HttpServletResponse response = getResponse();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);

//新建pdf文档 ,添加许可
Document document = new Document();
document.addLicense(GeneralConstants.PDF_LICENSE_KEY);

//调用方法得到页面宽度
int pageWidth = this.getPDFPageWidth(session);
//调用方法得到页面实例
Page page = this.getPDFPageInstance(pageWidth);


//设置相关作者 标题等信息
HashMap navBarAccessMap = (HashMap)session.getAttribute(UserCompanyWorker.NAV_ACCESS_BEAN);
String programName = "L5M Internal" + "\n";
programName += this.getPDFNavBarTitle(navBarAccessMap, CATEGORY_KEY) + " - ";
programName += this.getPDFNavBarTitle(navBarAccessMap, PROGRAM_KEY);
String userName = DataUtil.getUserFullName(dh, getUserBean().getUserId());
document.setAuthor(userName);
document.setTitle(programName);


//新建模板
Template template = new Template();
this.setupPDFDocumentTemplate(template, page);
document.setTemplate(template);

this.setupPDFHeader(session, page, programName, userName);

//是否设置了页脚 Footer
boolean hasSetupFooter = false;



Table table = this.getPDFTable(session, page);
page.getElements().add(table);
document.getPages().add(page);
Table tableOF = table.getOverflowRows();
while (tableOF != null){
Page pageOF = this.getPDFPageInstance(pageWidth);
pageOF.getElements().add(tableOF);
float offsetY = tableOF.getVisibleHeight() + 65; // last table +
// table start (used
// for footer)
document.getPages().add(pageOF);
tableOF = tableOF.getOverflowRows();
if (tableOF == null){
this.setupPDFFooter(session, pageOF, offsetY);
hasSetupFooter = true;
}
}

if (!hasSetupFooter){
float offsetY = table.getVisibleHeight() + 65; // last table + table
// start
this.setupPDFFooter(session, page, offsetY);
}
document.draw(response.getOutputStream());
}
catch (Exception e){
e.printStackTrace();
}
}

///ok bar title
private String getPDFNavBarTitle(HashMap navBarAccessMap, String code){
String title = "No Title";
if (navBarAccessMap.containsKey(code)){
NavigationItemBean itemBean = (NavigationItemBean)navBarAccessMap.get(code);
if (itemBean.getDisplayName() != null) title = itemBean.getDisplayName();
}
return title;
}


// ok 返回页面宽度
// normal ~50; large ~150
private int getPDFPageWidth(HttpSession session){
int pageWidth = 0;
DataTable dataTable = (DataTable)session.getAttribute("dataTable");
if (dataTable == null) return 0;

DataNode[][] headerNodes = dataTable.getHeaderNodes();

pageWidth += 150;
pageWidth += 20;
pageWidth += 50;

int levelCount = headerNodes.length;
int lastLevelIndex = levelCount - 1;
for (int i = 0; i < headerNodes[lastLevelIndex].length; i++){
pageWidth += 50;
}

return pageWidth + 100;
}

// ok 返回page
private Page getPDFPageInstance(int pageWidth){
Page page = new Page(PageSize.LETTER, PageOrientation.LANDSCAPE);
if (pageWidth > page.getDimensions().getWidth()){
page.getDimensions().setWidth(pageWidth);
}
return page;
}
///ok 新建模板
private void setupPDFDocumentTemplate(Template template, Page page)
throws FileNotFoundException{
float x, y, w, h;

String token = "%%CP(1)%% of %%TP(1)%%";
x = page.getDimensions().body.getWidth() - 100;
y = page.getDimensions().body.getHeight() - 25;
w = 100;
h = 25;
PageNumberingLabel labelPN = new PageNumberingLabel(
token, x, y, w, h,
com.cete.dynamicpdf.Font.getHelvetica(), 12, TextAlign.RIGHT);
template.getElements().add(labelPN);
}


///ok 设置pdf 头
private void setupPDFHeader(HttpSession session, Page page, String programName, String userName)
throws FileNotFoundException{

Label labelProgram = new Label(
programName, 0, 0, page.getDimensions().body.getWidth() / 2, 100,
com.cete.dynamicpdf.Font.getHelveticaBold(), 12, TextAlign.LEFT);

String imgPath = getAppHomeDir() + "/image/";
Image imageLogo = new Image(
imgPath + "Inter-ViewButton_01.PNG",
page.getDimensions().body.getWidth(), 0);
imageLogo.setAlign(Align.RIGHT);

Calendar cal = Calendar.getInstance();
java.util.Date date = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy");

String strRunningInfo = this.getRunningTimeInfo(session);

String strUserDate = " " + userName + "\n " + sdf.format(date) + "\t\t\t" + strRunningInfo;
Label labelUserDate = new Label(
strUserDate, 0, 28, page.getDimensions().body.getWidth(), 100,
com.cete.dynamicpdf.Font.getHelvetica(), 10, TextAlign.LEFT);

page.getElements().add(labelProgram);
page.getElements().add(imageLogo);
page.getElements().add(labelUserDate);
}


// ok 设置页脚
private void setupPDFFooter(HttpSession session, Page page, float offsetY){

String strDB = "DB Provider: DB106 Equipment";
Label labelDB = new Label(
strDB, 0, offsetY, 400, 100,
com.cete.dynamicpdf.Font.getHelvetica(), 10, TextAlign.LEFT);


page.getElements().add(labelDB);

}

private Table getPDFTable(HttpSession session, Page page){
Table table = new Table(
0, 55, page.getDimensions().body.getWidth(),
page.getDimensions().body.getHeight() - 80,
com.cete.dynamicpdf.Font.getHelvetica(), 12);
table.setBorderWidth(1);
table.setRepeatColumnHeaderCount(4);

this.setupPDFTableColumns(session, table);
this.setupPDFTableHeader(session, table);
this.setupPDFTableBody(session, table);
return table;
}

private void setupPDFTableColumns(HttpSession session, Table table){
boolean[] isDisplayingHeader = (boolean[])session.getAttribute("invoiceHeaderColumnControl");
if (isDisplayingHeader == null) return;

ColumnList colList = table.getColumns();
for (int i = 0; i < isDisplayingHeader.length; i++){
if (isDisplayingHeader[i]){
if (i == 2) colList.add(80);
else if (i == 4) colList.add(65);
else if (i == 14) colList.add(65);
else if (i == 15) colList.add(65);
else colList.add(65);
}
}
}

private void setupPDFTableHeader(HttpSession session, Table table){
boolean[] isDisplayingHeader = (boolean[])session.getAttribute("invoiceHeaderColumnControl");

RowList rowList = table.getRows();
Row row1 = rowList.add(com.cete.dynamicpdf.Font.getHelveticaBold(), PDF_FONT_SIZE);
CellList cellList1 = row1.getCellList();

for(int i=0; i<INVOICE_HEADER_COLUMNS.length; i++){
if (isDisplayingHeader[i]) cellList1.add(INVOICE_HEADER_COLUMNS[i]);
}

for (int i = 0; i < cellList1.getCount(); i++){
Cell cell = cellList1.getCell(i);
cell.setAlign(CellAlign.CENTER);
cell.setVAlign(CellVAlign.CENTER);
}
}

private void setupPDFTableBody(HttpSession session, Table table){
boolean[] isDisplayingHeader = (boolean[])session.getAttribute("invoiceHeaderColumnControl");
List resultList = (List)session.getAttribute("displayList");
if (isDisplayingHeader == null) return;
if (resultList == null) return;

RowList rowList = table.getRows();
for (int i = 0; i < resultList.size(); i++){
DataGroupBean bean = (DataGroupBean)resultList.get(i);

Row row = rowList.add(com.cete.dynamicpdf.Font.getHelvetica(), PDF_FONT_SIZE);
CellList cellList = row.getCellList();
RgbColor rowBackground = null;
if ((i % 2) == 0) rowBackground = new RgbColor(245, 245, 220);
else rowBackground = new RgbColor(255, 255, 255);
row.setBackgroundcolor(rowBackground);

for (int j = 0; j < bean.getNumFields(); j++){
if (isDisplayingHeader[j]){
Object obj = bean.getField(j);
String strValue = (obj != null) ? (bean.getField(j)).toString() : "";
Cell cell = cellList.add(strValue);
cell.setFontSize(PDF_FONT_SIZE);
}
}

for (int k = 0; k < cellList.getCount(); k++){
Cell cell = cellList.getCell(k);
cell.setAlign(CellAlign.CENTER);
cell.setVAlign(CellVAlign.CENTER);
}
}
}

private String getRunningTimeInfo(HttpSession session){
Long longStart = (Long)session.getAttribute(CountInfoUtil.RUNNING_START_TIME);
long valueStart = (longStart != null) ? longStart.longValue() : 0;
Long longEnd = (Long)session.getAttribute(CountInfoUtil.RUNNING_END_TIME);
long valueEnd = (longEnd != null) ? longEnd.longValue() : 0;

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(valueStart);
SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss a");
long timeDiff = (valueEnd - valueStart) / 1000;
long hour = timeDiff / 3600;
long minute = timeDiff % 3600 / 60;
long second = timeDiff % 60;

StringBuffer sb = new StringBuffer();
sb.append("Running Starts at ").append(formatter2.format(cal.getTime()));
sb.append(" Duration: ");
sb.append(hour).append("h:");
sb.append(minute).append("m:");
sb.append(second).append("s");
return sb.toString();
}

}// class
re: Chain of responsibility charlie 2007-11-22 11:03  
今年的软考,软件设计师java大题就是这个
请给我发一份,zqweb@tom.com,谢谢!
re: 一个OpenSource的评价图 Charlie 2006-11-25 09:52  
谢谢您的图,顶一下。。。
re: 给每一个女孩儿(转) charlie 2005-11-29 21:02  
一些发自内心的话语,值得深思
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(1)

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜