iText导出pdf
程序员文章站
2024-03-19 12:57:16
...
需要的依赖
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.6</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>
调用
@RequestMapping(value = "/xxx{id}")
void downpdfkeng(Model model,HttpServletResponse response,@PathVariable("id") String id ,HttpServletRequest req) {
response.setHeader("X-Frame-Options", "SAMEORIGIN");
获取数据 tbldievdto( 需要导出的数据对象 )
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
File rfile = new File("e:/pdf");
if(!rfile.exists()){
rfile.mkdirs();
}
String pdfPath = "e:/pdf/0-6岁疑似残疾儿童诊断评估书.pdf";
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(pdfPath));
document.open();
BaseFont hfHei = BaseFont.createFont("c:\\Windows\\fonts\\SIMHEI.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font font = new Font(hfHei,22);
Font font1 = new Font(hfHei,14);
String title = "xxxxx";
//设置标题
Paragraph paragraph = new Paragraph(title,font);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
//拼接内容
PdfPTable table1=pdfpjie(tbldievdto);
document.add(table1);
document.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File("e:/pdf/xxxxx.pdf");
byte[] buf = new byte[1024 * 1024 * 10];
int len = 0;
ServletOutputStream ut = null;
BufferedInputStream br = null;
response.reset();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "must-revalidate, no-transform");
response.setDateHeader("Expires", 0L);
String userAgent = req.getHeader("User-Agent");
// boolean ie =userAgent.toLowerCase().indexOf("msie") != -1;
boolean ie = (userAgent.toLowerCase().indexOf("msie") > -1 || userAgent.toLowerCase().indexOf("rv:11") > -1);
boolean isIE = (userAgent != null) && ie;
String displayFilename = file.getName();
response.setContentType("application/x-download");
if (isIE) {
try {
displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
} else {
try {
displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
}
try {
br = new BufferedInputStream(new FileInputStream(file));
ut = response.getOutputStream();
while ((len = br.read(buf)) != -1)
ut.write(buf, 0, len);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public PdfPTable pdfpjie(TblDiagnosticEvalDto tbldievdto) throws DocumentException, IOException{
BaseFont hfHei = BaseFont.createFont("c:\\Windows\\fonts\\SIMHEI.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font font = new Font(hfHei,22);
Font font1 = new Font(hfHei,12);
// 3列的表.
PdfPTable table = new PdfPTable(7);
table.setWidthPercentage(100); // 宽度100%填充
table.setSpacingBefore(10f); // 前间距
table.setSpacingAfter(10f); // 后间距
List<PdfPRow> listRow = table.getRows();
//设置列宽
float[] columnWidths = { 1f, 1f, 1f, 1f, 1f, 1f ,1f };
table.setWidths(columnWidths);
//行1
PdfPCell cells1[]= new PdfPCell[7];
PdfPRow row1 = new PdfPRow(cells1);
//单元格
cells1[0] = new PdfPCell(new Paragraph("姓名",font1));//单元格内容
cells1[1] = new PdfPCell(new Paragraph(tbldievdto.getChildName(),font1));
cells1[1].setColspan(2);//合并横向单元格
cells1[3] = new PdfPCell(new Paragraph("xxx",font1));
cells1[4] = new PdfPCell(new Paragraph(tbldievdto.getChildIdno()));
cells1[4].setColspan(3);
//行2
PdfPCell cells2[]= new PdfPCell[7];
PdfPRow row2 = new PdfPRow(cells2);
cells2[0] = new PdfPCell(new Paragraph("xx",font1));//单元格内容
cells2[1] = new PdfPCell(new Paragraph(Sex,font1));
cells2[1].setColspan(2);
cells2[3] = new PdfPCell(new Paragraph("xx",font1));
cells2[4] = new PdfPCell(new Paragraph(DateToStr(tbldievdto.getChildBirthday())));
cells2[4].setColspan(3);
//行3
PdfPCell cells3[]= new PdfPCell[7];
PdfPRow row3 = new PdfPRow(cells3);
cells3[0] = new PdfPCell(new Paragraph("xx",font1));//单元格内容
cells3[1] = new PdfPCell(new Paragraph(tbldievdto.getEvalutionMethod(),font1));
cells3[1].setColspan(6);
//行4
PdfPCell cells4[]= new PdfPCell[7];
PdfPRow row4 = new PdfPRow(cells4);
cells4[0] = new PdfPCell(new Paragraph("xxx",font1));//单元格内容
cells4[0].setRowspan(2); //合并纵向单元格
cells4[1] = new PdfPCell(new Paragraph(tbldievdto.getEvalutionResult(),font1));
cells4[1].setColspan(6);
//行5
PdfPCell cells5[]= new PdfPCell[7];
PdfPRow row5 = new PdfPRow(cells5);
cells5[1] = new PdfPCell(new Paragraph("xxx",font1));//单元格内容
cells5[1].setColspan(2);
cells5[3] = new PdfPCell(new Paragraph(Type,font1));
cells5[3].setColspan(4);
//行6
PdfPCell cells6[]= new PdfPCell[7];
PdfPRow row6 = new PdfPRow(cells6);
cells6[0] = new PdfPCell(new Paragraph("xxx",font1));//单元格内容
cells6[1] = new PdfPCell(new Paragraph(tbldievdto.get(),font1));
cells6[1].setColspan(6);
//行7
PdfPCell cells7[]= new PdfPCell[7];
PdfPRow row7 = new PdfPRow(cells7);
cells7[0] = new PdfPCell(new Paragraph("xxx",font1));
cells7[1] = new PdfPCell(new Paragraph(truntrainingProgram,font1));
cells7[1].setColspan(6);
//行8
PdfPCell cells8[]= new PdfPCell[7];
PdfPRow row8 = new PdfPRow(cells8);
cells8[0] = new PdfPCell(new Paragraph("xxx",font1));
cells8[1] = new PdfPCell(new Paragraph(tbldievdto.get(),font1));
cells8[1].setColspan(2);
cells8[3] = new PdfPCell(new Paragraph("评估单位",font1));
cells8[4] = new PdfPCell(new Paragraph(tbldievdto.get(),font1));
cells8[4].setColspan(3);
//行9
PdfPCell cells9[]= new PdfPCell[7];
PdfPRow row9 = new PdfPRow(cells9);
cells9[0] = new PdfPCell(new Paragraph("xxx",font1));
cells9[1] = new PdfPCell(new Paragraph(DateToStr(tbldievdto.getUpdatetime()),font1));
cells9[1].setColspan(6);
//把第一行添加到集合
listRow.add(row1);
listRow.add(row2);
listRow.add(row3);
listRow.add(row4);
listRow.add(row5);
listRow.add(row6);
listRow.add(row7);
listRow.add(row8);
listRow.add(row9);
return table;
}
上一篇: web安全入门(第八章-2)越权漏洞
下一篇: md5 sha256 加密