使用ITXT生成PDF文件学习

今天想生成一个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());
  }

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

使用ITXT生成PDF文件学习

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭