关闭

使用ITXT生成PDF文件学习

664人阅读 评论(0) 收藏 举报

今天想生成一个PDF文件,带中文的,从网络上找了好多资料,最中中文还是有问题,最后参照官方的文档才将这个给解决了,

代码 :

 response.setContentType("application/pdf");
  String filename="card"+Datetime.Time_Article()+".pdf";
  Document document=new Document(PageSize.A4);
  BaseFont bfChinese = BaseFont.createFont("c://winnt//fonts//msgothic.ttc,0",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
  //ServletOutputStream yeout=response.getOutputStream();
  try{
    PdfWriter.getInstance(document,new FileOutputStream("D://Java//tomcat5.5//webapps//card//cardpdf//"+filename));
   // PdfWriter.getInstance(document,new FileOutputStream("//"+filename));
     String[][] yemzi=(String[][])session.getAttribute("yemzi");
     //document.
     String cardmoney=(String)session.getAttribute("cardmoney");
     int number=Integer.parseInt((String)session.getAttribute("number"));
     document.open();
     Table td=new Table(3);
     td.setBorder(1);
     td.setBorderColor(new Color(255,255,255));
     td.setPadding(2);
     td.setSpacing(2);
     Cell yecell=new Cell();
     //String str=new String("?????".getBytes("ISO-8859-1"),"GB2312");
     document.add(new Paragraph("/u5361/u53f7/u548c/u5bc6/u7801/u5217/u8868",FontChinese));
     yecell=new Cell(new Paragraph("/u5361/u53f7",FontChinese));
     yecell.setHorizontalAlignment("center");
     td.addCell(yecell);
     yecell=new Cell(new Paragraph("/u5bc6/u7801",FontChinese));
     yecell.setHorizontalAlignment("center");
     td.addCell(yecell);
     yecell=new Cell(new Paragraph("/u91d1/u989d",FontChinese));
     yecell.setHorizontalAlignment("center");
     td.addCell(yecell);
     for(int i=0;i<number;i++)
     {
       yecell=new Cell(yemzi[i][0]);
       yecell.setHorizontalAlignment("center");
       td.addCell(yecell);
       yecell=new Cell(yemzi[i][1]);
       yecell.setHorizontalAlignment("center");
       td.addCell(yecell);
       yecell=new Cell(cardmoney);
       yecell.setHorizontalAlignment("center");
       td.addCell(yecell);
     }
     document.add(td);
     document.close();
  }catch(FileNotFoundException  ex)
  {
    ex.printStackTrace();
    out.println("??PDF??????"+ex.getMessage());
  }catch(DocumentException ye)
  {
    ye.printStackTrace();
    out.println("??PDF??????1"+ye.getMessage());
  }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18668次
    • 积分:379
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类