public List<String> getmapstr(List<PointHelp> list){
List<String> mapstr = new ArrayList<String>();
StringBuffer markers = new StringBuffer();
StringBuffer markerStyles = new StringBuffer();
StringBuffer markersdes = new StringBuffer();
for(int i =0;i <list.size();i++){
PointInfo point = list.get(i).getPoint();
markers.append(point.getLon()+","+point.getLat());
if(i!=list.size()-1)
markers.append("|");
int nexi = i+1;
markerStyles.append("l,"+nexi);
if(i!=list.size()-1)
markerStyles.append("|");
markersdes.append(nexi+":"+point.getName()+";");
}
mapstr.add(markers.toString());
mapstr.add(markerStyles.toString());
mapstr.add(markersdes.toString());
return mapstr;
}
/**
* pdf 下载
* @param rc
* @throws Exception
*/
public void pdfdownload(RequestContext rc) throws Exception{
PlanInfo plan =pls.findById(rc.param("pid", "")); //计划 实体
Map<String,List<PointHelp>> map = pls.findByIdMap(rc.param("pid", ""));// map
List<PointHelp> helplist = plan.getPointList(); //计划详细
DateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");//格式转换-年月日
DateFormat hm = new SimpleDateFormat("HH:mm");//格式转换-时分
//使用iTextAsian.jar中的字体
//BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); //中文字体
//使用Windows系统字体(TrueType) --微软雅黑简体
BaseFont bfChinese = BaseFont.createFont("E:/msyh.ttf", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font f9 =new Font(bfChinese, 9, Font.NORMAL);
f9.setColor(136, 136, 136);
rc.response().setContentType("application/x-msdownload;charset=UTF-8");
rc.response().setHeader("Content-Disposition","attachment;filename=tourplan.pdf");
Rectangle psize = new Rectangle(PageSize.A4);
//psize.setLeft(30f);
//psize.setRight(30f);
//psize.setBackgroundColor(new BaseColor(247,247,247));
//Document document = new Document(PageSize.A4, 140, 140, 50, 50); , 120, 120, 30, 30
//Document document = new Document(psize,70, 70, 30, 30);
Document document = new Document(psize,100, 100, 30, 30);
//document.setPageSize(psize);
PdfWriter writer = PdfWriter.getInstance(document, rc.response().getOutputStream());
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.open();
// 书签 根节点
PdfOutline root = writer.getRootOutline();
PdfOutline movieBookmark = new PdfOutline(root, new PdfDestination(PdfDestination.FITH, writer.getVerticalPosition(true)), plan.getName(), true);
//计划名称 595×842
Font ft =new Font(bfChinese, 20, Font.NORMAL);
ft.setColor(83, 85, 100);
Paragraph name =new Paragraph(plan.getName(),ft);
name.setSpacingBefore(50);
name.setAlignment(Element.ALIGN_CENTER);
document.add(name);
//作者 --- ** 临时 由海南陆客替代 "+plan.getUser().getName()+" "+df.format(plan.getCreateDate())+" 207, 127, 34
Font fa =new Font(bfChinese, 11, Font.NORMAL);
fa.setColor(206, 126, 34);
Paragraph author =new Paragraph("by 海南陆客(2013.3.1)",fa);
author.setAlignment(Element.ALIGN_RIGHT);
author.setSpacingAfter(30);
document.add(author);
//预算表格
float[] widths = {0.1f, 0.7f, 0.2f};
PdfPTable table = new PdfPTable(widths);
BaseColor gc = new BaseColor(202, 202, 202);
table.getDefaultCell().setUseAscender(true);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setMinimumHeight(20);
table.getDefaultCell().setBorderColor(gc);
//table.getDefaultCell().setLeading(2, 2);
/*PdfPCell t1 = new PdfPCell(new Paragraph("行程",new Font(bfChinese, 10, Font.NORMAL)));
t1.setHorizontalAlignment(Element.ALIGN_CENTER);
t1.setBorderColor(gc);
t1.setBorderWidthLeft(2);
t1.setBorderWidthTop(2);*/
table.addCell(new Paragraph("行程",f9));
/*PdfPCell t2 = new PdfPCell(new Paragraph("名称",new Font(bfChinese, 10, Font.NORMAL)));
t2.setHorizontalAlignment(Element.ALIGN_CENTER);
t2.setBorderColor(gc);
t2.setBorderWidthTop(2);*/
table.addCell(new Paragraph("名称",f9));
/*PdfPCell t3 = new PdfPCell(new Paragraph("",new Font(bfChinese, 10, Font.NORMAL)));
t3.setHorizontalAlignment(Element.ALIGN_CENTER);
t3.setBorderColor(gc);
t3.setBorderWidthRight(2);
t1.setBorderWidthTop(2);*/
table.addCell(new Paragraph("价格",f9));
int price=0;//行程预算
for(int i = 1; i <= plan.getDays();i++){
List<String> mapstr = getmapstr(map.get(String.valueOf(i)));
//地图
Image img = Image.getInstance("http://api.map.baidu.com/staticimage?width=720&height=500&markers=" +mapstr.get(0)+"&markerStyles="+mapstr.get(1));
//img.scaleToFit(400, 250);
img.scaleToFit(400, 370);
img.setAlignment(Element.ALIGN_MIDDLE);
document.add(img);
//地图 -标点描述
Paragraph markersp = new Paragraph(mapstr.get(2),f9);
markersp.setLeading(2,2);
markersp.setSpacingAfter(10);
markersp.setSpacingBefore(10);
document.add(markersp);
Image imgca = Image.getInstance("F:\\tour\\calendar.jpg");
//imgca.setSpacingBefore(50);
imgca.setAlignment(Image.DEFAULT|Image.UNDEFINED);
imgca.scaleToFit(38, 34);
Chunk dw = new Chunk("第"+i+"天").setLocalDestination("d"+i);
Font f18 =new Font(bfChinese, 18, Font.NORMAL);
f18.setColor(83, 85, 100);
dw.setBackground(new BaseColor(247,247,247), 20, 20, 20, 20);
dw.setFont(f18);
/*Paragraph dy = new Paragraph();
dy.setAlignment(Element.ALIGN_CENTER);
dy.add(imgca);
dy.add(dw);*/
//document.add(imgca);
//document.add(dw);
//document.add(Chunk.NEWLINE);
//document.add(Chunk.NEWLINE);
//第 n 天
PdfOutline day1 =new PdfOutline(movieBookmark,PdfAction.gotoLocalPage("d"+i, false), "第"+i+"天");
Chunk dd = new Chunk("第"+i+"天").setLocalDestination("d"+i);
Font fd =new Font(bfChinese, 18, Font.NORMAL);
fd.setColor(83, 85, 100);
dd.setFont(fd);
Paragraph diyss = new Paragraph(dd);
diyss.setSpacingAfter(20);
diyss.setAlignment(Element.ALIGN_CENTER);
document.add(diyss);
//景点
for(int j =0;j <map.get(String.valueOf(i)).size();j++){
Paragraph tp = new Paragraph();
PointInfo point = map.get(String.valueOf(i)).get(j).getPoint();
new PdfOutline(day1,PdfAction.gotoLocalPage(""+i+j, false),point.getName());
Chunk tourc1 = new Chunk(point.getName()).setLocalDestination(""+i+j);
Font fj =new Font(bfChinese, 14, Font.NORMAL);
fj.setColor(83, 85, 100);
tourc1.setFont(fj);
Paragraph tourp1 = new Paragraph(tourc1);
tourp1.setAlignment(Element.ALIGN_CENTER);
tourp1.setSpacingAfter(10);
//tp.add(tourp1);
document.add(tourp1);
//景点 图片
Image tourimg1 = null;
if(point.getCover32() !=null && !point.getCover32().equals("")){
tourimg1 = Image.getInstance("http://cmstest.hainanluke.com/upload/image/"+point.getCover32());
tourimg1.scaleToFit(314, 193);
tourimg1.setAlignment(Element.ALIGN_MIDDLE);
tp.add(tourimg1);
document.add(tourimg1);
}
//景点描述 1-景点 2-住宿 3-美食 4-娱乐
StringBuffer tourdes = new StringBuffer();
tourdes.append("描述:"+point.getDescribe());
tourdes.append("\n地址:"+point.getAddress());
if(point.getType().equals("1")){ // 景点
tourdes.append("\n门票价格:"+point.getPriceStr());
tourdes.append("\n开放时间:"+point.getOpenTime());
tourdes.append("\n建议游玩时间:"+point.getHourStr());
tourdes.append("\n最佳旅游月份:"+point.getBestMonth());
}else if(point.getType().equals("2")){ //住宿
tourdes.append("\n价格:"+point.getPriceStr());
tourdes.append("\n级别:"+point.getRank());
}else if(point.getType().equals("3")){ //美食
tourdes.append("\n营业时间:"+point.getOpenTime());
tourdes.append("\n人均消费:"+point.getPriceStr());
tourdes.append("\n推荐菜:"+point.getRecommendDish());
}else if(point.getType().equals("4")){ //娱乐
tourdes.append("\n营业时间:"+point.getOpenTime());
tourdes.append("\n价格:"+point.getPriceStr());
tourdes.append("\n类型:"+point.getStyle());
}
if(point.getTelephone()!=null && !point.getTelephone().equals("")){
tourdes.append("\n电话:"+point.getTelephone());
}
if(point.getOfficialURL()!=null && !point.getOfficialURL().equals(""))
tourdes.append("\n官网:"+point.getOfficialURL());
String recommendation = "";
for(int k=1;k<=point.getRecommendation();k++){recommendation+="★";}
tourdes.append("\n陆客推荐度:"+recommendation+"\n");
if( j != map.get(String.valueOf(i)).size()-1){
tourdes.append("\n------------------------- "+point.getCarTyep()+","+point.getDistance()+","+point.getCreateTime()+"-------------------------");
}
Chunk sc = new Chunk(tourdes.toString(),f9);
//sc.setBackground(new BaseColor(247,247,247), 50, 50, 50, 50);
sc.setLineHeight(18);
PdfDiv pdiv = new PdfDiv();
pdiv.setBackgroundColor(new BaseColor(247,247,247));
pdiv.setWidth(400f);
pdiv.addElement(Chunk.NEWLINE);
pdiv.addElement(tourp1);
pdiv.addElement(tourimg1);
pdiv.addElement(sc);
//pdiv.setFloatType(FloatType.NONE);
PdfDiv pdiv2 = new PdfDiv();
pdiv2.setPaddingLeft(25f);
pdiv2.setPaddingRight(25f);
//pdiv2.setPaddingBottom(25f);
pdiv2.setBottom(10f);
pdiv2.setBackgroundColor(new BaseColor(247,247,247));
pdiv2.addElement(pdiv);
//pdiv2.setSpacingAfter(50f);
//document.add(pdiv2);
if( j != map.get(String.valueOf(i)).size()-1){
Paragraph ch = new Paragraph("-----------"+point.getCarTyep()+","+point.getDistance()+","+point.getCreateTime()+"-----------",f9);
ch.setSpacingAfter(50);
ch.setSpacingBefore(50);
//document.add(ch);
}
//document.add(Chunk.NEWLINE);
//document.add(Chunk.NEWLINE);
Paragraph tourdesp = new Paragraph(sc);
tourdesp.setLeading(2,2);//行距
tourdesp.setSpacingBefore(10);
tourdesp.setSpacingAfter(15);
document.add(tourdesp);
//setBackground(new BaseColor(247,247,247), 20, 20, 20, 20)
price += point.getPriceInt(); //行程预算
//预算表格
PdfPCell c0 = new PdfPCell();
c0.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(new Paragraph("D"+i,f9));
PdfPCell c1 = new PdfPCell();
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(new Paragraph(point.getName(),f9));
PdfPCell c2 = new PdfPCell();
c2.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(new Paragraph("¥"+String.valueOf(point.getPriceInt()),f9));
}
}
new PdfOutline(movieBookmark,
new PdfAction("http://www.hainanluke.com"),
"海南陆客旅行计划官网");
new PdfOutline(movieBookmark,
new PdfAction("http://www.hainanluke.com"),
"海南陆客官网");
new PdfOutline(movieBookmark,
new PdfAction("http://hnlkly.tmall.com/?spm=a1z10.1.15231555-17849994558.2.zp0cMr"),
"海南陆客淘宝店");
//直线
LineSeparator ls = new LineSeparator();
ls.setLineColor(new BaseColor(151, 151, 151));
//document.add(new Chunk(ls));
//设置书签
new PdfOutline(movieBookmark,PdfAction.gotoLocalPage("end", false), "价目清单");
//行程预算
Chunk yvsuanc = new Chunk("价目清单").setLocalDestination("end");
Font fb =new Font(bfChinese, 18, Font.NORMAL);
fb.setColor(83, 85, 100);
yvsuanc.setFont(fb);
Paragraph yvsuanmp = new Paragraph(yvsuanc);
yvsuanmp.setSpacingBefore(20);
yvsuanmp.setSpacingAfter(20);
yvsuanmp.setAlignment(Element.ALIGN_CENTER);
document.add(yvsuanmp);
PdfPCell end = new PdfPCell( new Paragraph("天数:"+plan.getDays()+" 预计价格:¥"+price,f9));
end.setLeading(2, 2);
end.setUseAscender(true);
end.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
end.setHorizontalAlignment(Element.ALIGN_RIGHT); //水平居右
end.setMinimumHeight(20);
end.setColspan(3);
end.setBorderColor(gc);
table.addCell(end);
table.setWidthPercentage(100);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
document.add(table);
/*
//添加空白行
document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);
*/
//日历图片
/*Image imgca = Image.getInstance("F:\\tour\\calendar.jpg");
imgca.setSpacingBefore(50);
imgca.scaleToFit(38, 34);
imgca.setAlignment(Element.ALIGN_MIDDLE);
document.add(imgca);*/
/*
*/
document.close();
}