概述
今天想生成一个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文件学习的全部内容,希望文章能够帮你解决使用ITXT生成PDF文件学习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复