java itext下载_Java 使用itext生成pdf以及下载

该博客展示了如何利用Java的iText库创建PDF文件,包括设置字体、创建表格和填充内容,用于生成舆情报告,包含舆情事件、发生时间和地区、概述、相关数据、结论等关键信息。
摘要由CSDN通过智能技术生成

packageutils;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importcom.itextpdf.text.BaseColor;importcom.itextpdf.text.Document;importcom.itextpdf.text.DocumentException;importcom.itextpdf.text.Element;importcom.itextpdf.text.Font;importcom.itextpdf.text.PageSize;importcom.itextpdf.text.Phrase;importcom.itextpdf.text.pdf.BaseFont;importcom.itextpdf.text.pdf.PdfPCell;importcom.itextpdf.text.pdf.PdfPTable;importcom.itextpdf.text.pdf.PdfWriter;public classDoPDF {public voidgetPDF(String filePath,String title,String time,String area,

String summary,String data,String result,String advise)throwsIOException{

Document document= newDocument(PageSize.A4);try{try{

PdfWriter.getInstance(document,newFileOutputStream(filePath));

}catch(FileNotFoundException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

document.open();

BaseFont bfCN;

Font fontCN ;

bfCN= BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", false);

fontCN= new Font(bfCN, 12, Font.NORMAL, BaseColor.BLACK);

PdfPTable table= new PdfPTable(4);

table.setTotalWidth(new float[]{66,84,66,84});//设置字体大小

PdfPCell cell = new PdfPCell(new Phrase("舆情报告",fontCN));

cell.setColspan(4);

cell.setBorderWidth(0);//设置表格的边框宽度为0

cell.setMinimumHeight(30);

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

PdfPCell cell1= new PdfPCell(new Phrase("舆情事件",fontCN));

cell1.setColspan(1);

cell1.setBorderWidth(1);//设置表格的边框宽度为0

cell1.setMinimumHeight(30);

table.addCell(cell1);

PdfPCell cell2= new PdfPCell(newPhrase(title,fontCN));

cell2.setColspan(3);

cell2.setBorderWidth(1);//设置表格的边框宽度为0

cell2.setMinimumHeight(30);

table.addCell(cell2);

PdfPCell cell3= new PdfPCell(new Phrase("发生时间",fontCN));

cell3.setColspan(1);

cell3.setBorderWidth(1);//设置表格的边框宽度为0

cell3.setMinimumHeight(30);

table.addCell(cell3);

PdfPCell cell4= new PdfPCell(newPhrase(time,fontCN));

cell4.setColspan(1);

cell4.setBorderWidth(1);//设置表格的边框宽度为0

cell4.setMinimumHeight(30);

table.addCell(cell4);

PdfPCell cell5= new PdfPCell(new Phrase("发生地区",fontCN));

cell5.setColspan(1);

cell5.setBorderWidth(1);//设置表格的边框宽度为0

cell5.setMinimumHeight(30);

table.addCell(cell5);

PdfPCell cell6= new PdfPCell(newPhrase(area,fontCN));

cell6.setColspan(1);

cell6.setBorderWidth(1);//设置表格的边框宽度为0

cell6.setMinimumHeight(30);

table.addCell(cell6);

PdfPCell cell7= new PdfPCell(new Phrase("舆情概述",fontCN));

cell7.setColspan(4);

cell7.setBorderWidth(1);//设置表格的边框宽度为0

cell7.setMinimumHeight(30);

table.addCell(cell7);

PdfPCell cell8= new PdfPCell(newPhrase(summary,fontCN));

cell8.setColspan(4);

cell8.setBorderWidth(1);//设置表格的边框宽度为0

cell8.setMinimumHeight(90);

table.addCell(cell8);

PdfPCell cell9= new PdfPCell(new Phrase("相关数据",fontCN));

cell9.setColspan(4);

cell9.setBorderWidth(1);//设置表格的边框宽度为0

cell9.setMinimumHeight(30);

table.addCell(cell9);

PdfPCell cell10= new PdfPCell(newPhrase(data,fontCN));

cell10.setColspan(4);

cell10.setBorderWidth(1);//设置表格的边框宽度为0

cell10.setMinimumHeight(90);

table.addCell(cell10);

PdfPCell cellA= new PdfPCell(new Phrase("结论",fontCN));

cellA.setColspan(4);

cellA.setBorderWidth(1);//设置表格的边框宽度为0

cellA.setMinimumHeight(30);

table.addCell(cellA);

PdfPCell cellB= new PdfPCell(new Phrase("cell B",fontCN));

cellB.setColspan(4);

cellB.setBorderWidth(1);//设置表格的边框宽度为0

cellB.setMinimumHeight(90);

table.addCell(cellB);

PdfPCell cellC= new PdfPCell(newPhrase(advise,fontCN));

cellC.setColspan(4);

cellC.setBorderWidth(1);//设置表格的边框宽度为0

cellC.setMinimumHeight(30);

table.addCell(cellC);

PdfPCell cellD= new PdfPCell(new Phrase("cell D",fontCN));

cellD.setColspan(4);

cellD.setBorderWidth(1);//设置表格的边框宽度为0

cellD.setMinimumHeight(90);

table.addCell(cellD);

PdfPCell cellE= new PdfPCell(new Phrase("领导意见",fontCN));

cellE.setColspan(2);

cellE.setBorderWidth(1);//设置表格的边框宽度为0

cellE.setMinimumHeight(30);

table.addCell(cellE);

PdfPCell cellF= new PdfPCell(new Phrase("审批意见",fontCN));

cellF.setColspan(2);

cellF.setBorderWidth(1);//设置表格的边框宽度为0

cellF.setMinimumHeight(30);

table.addCell(cellF);

PdfPCell cellM= new PdfPCell(new Phrase("",fontCN));

cellM.setColspan(2);

cellM.setBorderWidth(1);//设置表格的边框宽度为0

cellM.setMinimumHeight(70);

table.addCell(cellM);

PdfPCell cellN= new PdfPCell(new Phrase("",fontCN));

cellN.setColspan(2);

cellN.setBorderWidth(1);//设置表格的边框宽度为0

cellN.setMinimumHeight(70);

table.addCell(cellN);

document.add(table);

}catch(DocumentException e) {

e.printStackTrace();

}finally{

document.close();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值