javaitext循环导出数据到模板
背景:⼀个pdf模板,⼀个模板⾥能只能记录三条数据,但是导出时记录条数不确定,这是就需要合并多个模板了,然后把数据导出到合并后的模板
⼀ ⾸先我们是项⽬,引⼊需要⽤到的包:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
由于不⽀持中⽂,还需要导⼊中⽂包(注意:语⾔包和itext包是对应的,版本不兼容会报错)
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
⼆ 贴代码
Controller层
@RequestMapping("/downLoadElecOrderDetails")
public R downLoadElecOrderDetails(@RequestParam Map<String,Object> map,HttpServletResponse response)throws Exception{
OrderinfoEntity entity = orderinfoService.queryElecOrderDetails(map);
try{
if(entity!=null){
ServletOutputStream out;
out = OutputStream();
response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName="+"order_detail("+ DateUtils.format(new Date())+").pdf");
map.put("callId",Id());
List<CallDetailEntity> callDetailEntityList = callDetailService.queryList(map);
entity.setCallDetailEntityList(callDetailEntityList);
exportOrderDetailPdf(entity,out);//导出⽂件
out.flush();
out.close();
}else{
("⼯单编号输⼊错误!");
}
}catch (IOException e){
e.printStackTrace();
}
return R.ok();
}
先根据数据条数,⽣成对应页数的pdf模板⽂件,然后⽤模板⽂件贴上对应的数据
第⼀个模板,需要依次移动每个字段到对应位置,后⾯的直接循环搞定
//导出核⼼代码,
private boolean exportOrderDetailPdf(OrderinfoEntity entity, OutputStream out)throws Exception{
if(entity ==null){
return false;
}
// String tempFile = "D:\\upload\\serviceOrderDetailTemp.pdf";
String tempFile =filePath+"serviceOrderDetailTemp.pdf";//临时⽂件路径
int pageSize = (il(Double.CallDetailEntityList().size())/3.0);//向上取整
checkTempPdf(pageSize,tempFile);//⽣成对应⼤⼩的模板
PdfReader reader = new PdfReader(tempFile);
PdfStamper stamper = new PdfStamper(reader, out);
BaseFont font = ateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
try{
long CallDetailEntityList().get(0).getReachTime().getTime();
long CallDetailEntityList().get(0).getLeaveTime().getTime();
for(int k=0;k<pageSize;k++){
PdfContentByte overContent = OverContent(k+1);
overContent.beginText();
overContent.setFontAndSize(font, 12);
overContent.setTextMatrix(300, 200);
overContent.showTextAligned(Element.ALIGN_Ordernumber(),445,755,0);//⼯单编号
overContent.showTextAligned(Element.ALIGN_ServiceCompanyName(),95,733,0);//客户名称
overContent.showTextAligned(Element.ALIGN_ServiceAddress(),320,733,0);//服务地点
overContent.showTextAligned(Element.ALIGN_DealTypeName(),95,685,0);//服务⽅式
//⼦单
for(int i=k*3;i&CallDetailEntityList().size()&&i<(k+1)*3;i++){
CallDetailEntity callDetailEntity = CallDetailEntityList().get(i);
overContent.showTextAligned(Element.ALIGN_CustomerPersonName(),85,660-(192*(i%3)),0);//⽤户overContent.showTextAligned(Element.ALIGN_EquipmentName(),172,660-(192*(i%3)),0);//设备
overContent.showTextAligned(Element.ALIGN_LEFT,DateUtils.StartTime(),DateUtils.DATE_TIME_PATTER N),290,660-(192*(i%3)),0);//开始时间
overContent.showTextAligned(Element.ALIGN_LEFT,DateUtils.FinishTime(),DateUtils.DATE_TIME_PATTE RN),445,660-(192*(i%3)),0);//结束时间
//循环换⾏
for(int m=0;m&FaultDesc().length();m=m+38){
String lineDescContent="";
if(m+38&FaultDesc().length()){
lineDescContent= FaultDesc().substring(m,m+38);
}else{
FaultDesc().substring(m);
}
overContent.showTextAligned(Element.ALIGN_LEFT,lineDescContent,95,645-(192*(i%3))-(m/38)*14,0);//故障现象}
//循环换⾏
for(int n=0;n&FaultSolve().length();n=n+38){
String lineSolveContent = "";
if(n+38&FaultSolve().length()){
FaultSolve().substring(n,n+38);
}else{
FaultSolve().substring(n);
}
overContent.showTextAligned(Element.ALIGN_LEFT,lineSolveContent,95,602-(192*(i%3))-(n/38)*14,0);//故障处理过程}
overContent.showTextAligned(Element.ALIGN_FinishStateName(),95,518-(192*(i%3)),0);//处理结果overContent.showTextAligned(Element.ALIGN_Trace(),213,518-(192*(i%3)),0);//后续跟进
overContent.showTextAligned(Element.ALIGN_Score()==null?"-
":String.Score()),148,493-(192*(i%3)),0);//评分
":String.Score()),148,493-(192*(i%3)),0);//评分
//签名
PdfDictionary pdfDictionary = PageN(1);
PdfObject pdfObject = (new PdfName("MediaBox"));
PdfArray pdfArray = (PdfArray) pdfObject;
String signPath = SignPath();
if(signPath!=null&&!"".im())){
// String realPath = "/usr/project/upload/"+signPath;
// String realPath = "D:\\upload\\"+signPath;
String realPath = filePath+signPath;
Image image = Instance(realPath);
image.scalePercent(15);
service fault/
/ image.setAbsolutePosition(370,80); 第三个⼦单
// image.setAbsolutePosition(370,280);第⼆个⼦单
image.setAbsolutePosition(370,480-((i%3)*192));
overContent.addImage(image);
}
if( ReachTime().getTime()<minReachTime){
minReachTime =ReachTime().getTime();
}
LeaveTime().getTime()>maxLeaveTime){
maxLeaveTime= LeaveTime().getTime();
}
}
overContent.showTextAligned(Element.ALIGN_LEFT,DateUtils.format(new Date(minReachTime),DateUtils.DATE_TIME_PATTERN),95,700,0);//⼯单到达时间overContent.showTextAligned(Element.ALIGN_LEFT,DateUtils.format(new Date(maxLeaveTime),DateUtils.DATE_TIME_PATTERN),260,700,0);//⼯单离开时间dText();
}
}catch (Exception e){
e.printStackTrace();
}finally {
stamper.close();
reader.close();
File tempEnd = new File(tempFile);//删除⽣成的临时模板
ists()){
tempEnd.delete();
}
return true;
}
}
//⽣成对应页数的pdf临时⽂件
private void checkTempPdf(int n,String tempFile) throws Exception{
File isExistTempFile = new File(tempFile);
ists()){
isExistTempFile.delete();
}
Document document = new Document();
try{
PdfWriter pdfWriter = Instance(document,new FileOutputStream(tempFile));
pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
document.open();
PdfReader reader = new PdfReader(filePath+"serviceOrderDetail.pdf");
PdfImportedPage page;
page = ImportedPage(reader,1);
Image imagePage = Instance(page);
imagePage.setAbsolutePosition(0,0);
for(int l=0;l<n;l++){
document.add(imagePage);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(document!=null){
document.close();
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论