java 生成pdf (iText)

public String ExportPdf{

Calendar calendar = Calendar.getInstance();
  String filename = String.valueOf(calendar.getTimeInMillis()) + ".pdf";// 文件名字
  String path = "\\" + filename;
  filename = filedir + path;// 生成的pdf文件保存路径
  System.out.println("fileame123===" + filename);

  //写入前把文件都删除 
  System.out.println("fileame===" + filedir+"file");
  //WritePdf.deleteFileInFolder(filedir+"file");
  
  //导出pdf
  Document document = new Document();
  
  //获取图片的路径    image为jfree生成的图片
  try {
   String imagePath =ServletActionContext.getServletContext().getRealPath(imageDir);
   File imagePath1 = new File(imagePath);
   // 如果这个目录不存在,则创建它。
   if (!imagePath1.exists())
    imagePath1.mkdir();
   
   double[] d1 =(double[])this.getRequest().getSession().getAttribute("dou");//预测数据
   getColumnPath(imagePath,d1);//把图片生成到指定的图片中
   Image  jpg = Image.getInstance(imagePath+"\\fruit4.jpeg");
   //Image  jpg = Image.getInstance(imagePath);

  Table table=new Table(2);
  table.setBorderWidth(1); //将边框宽度设为0
  table.setBorderColor(new Color(255, 255, 255)); //将边框的颜色设置为白色
  //将图片加载在表格中
  table.addCell(new Cell(jpg));
  table.addCell(new PDFParagraph("acf图"));
  
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  PdfWriter.getInstance(document, new FileOutputStream(filename));//pdf路径和文档内容绑定
  document.open();
  //通过表格进行输出图片的内容
  document.add(table);
  document.close();
  
  } catch (BadElementException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  //在页面显示
  //String path1 = path.replace("\\", "\\\\");
 // request.setAttribute("path", path1);
  //response.sendRedirect(path);
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值