如何利用java 利用itext生成PDF并下载
这是我在网上找的一个Demo,但是报空指针,有谁可以提供一个完整的代码啊!
public class TestPdf {
public static void main(String[] args){
HttpServletRequest request = null;
HttpServletResponse response = null;
testpdf2( request, response);
}
public static String testpdf2(HttpServletRequest request,HttpServletResponse response){
try
{
OutputStream os = response.getOutputStream();
response.setContentType("application/pdf");
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, os);
//设置字体
BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/simhei.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font FontChinese24 = new Font(bfChinese, 24, Font.BOLD);
Font FontChinese18 = new Font(bfChinese, 18, Font.BOLD);
Font FontChinese16 = new Font(bfChinese, 16, Font.BOLD);
Font FontChinese12 = new Font(bfChinese, 12, Font.NORMAL);
Font FontChinese11Bold = new Font(bfChinese, 11, Font.BOLD);
Font FontChinese11 = new Font(bfChinese, 11, Font.ITALIC);
Font FontChinese11Normal = new Font(bfChinese, 11, Font.NORMAL);
document.open();
Paragraph pg_bt = new Paragraph("纳税人风险明细数据查询报告",FontChinese18);
pg_bt.setAlignment(Element.ALIGN_CENTER);
document.add(pg_bt);
document.add(new Paragraph("报告使用说明:",FontChinese18));
document.add(new Chunk("1、报告使用注意保密,不得将报告直接转发给被应对纳税人,以及其他无关单位和人员;", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Chunk("2、报告中的外部交换数据,以及部分涉及发票、契税等数据,是以纳税名称为条件的查询,可能不完整;", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Chunk("3、报告中使用的外部数据本身可能存在错误、遗漏;", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Chunk("4、报告中的税款统计口径使用入库期,如标明为所属期,对跨年税款,按所跨越年度均等拆分。", FontChinese11Normal));
Paragraph pg_xbt01 = new Paragraph("第一章 企业资产税收风险分析",FontChinese18);
pg_xbt01.setAlignment(Element.ALIGN_CENTER);
document.add(pg_xbt01);
Paragraph pg_xbt02 = new Paragraph("第一节 企业房产、土地税收风险分析",FontChinese18);
pg_xbt02.setAlignment(Element.ALIGN_CENTER);
document.add(pg_xbt02);
document.add(new Paragraph("一、分析项目:本项分析主要涉及纳税人的以下经济行为:",FontChinese18));
document.add(new Chunk("(一)使用自有房产或无租使用他人房产", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Chunk("(二)使用自有土地或免税土地产", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Chunk("(三)自建或委托建设房产", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Chunk("(四)购置房产", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Chunk("(五)购置土地", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Chunk("(六)使用集体土地", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Chunk("(七)出租房产、土地", FontChinese11Normal));
document.add(Chunk.NEWLINE);
document.add(new Paragraph("二、数据查询",FontChinese18));
document.add(new Paragraph("(一)税源登记信息",FontChinese18));
document.add(new Paragraph("1、房土税源登记表",FontChinese18));
PdfPTable pt01 = new PdfPTable(11);
int widthpt01[] = {20,20,20,20,20,20,15,20,20,15,15};
pt01.setWidths(widthpt01);
PdfPCell cell01 = new PdfPCell(new Paragraph("登记来源",FontChinese11Bold));
PdfPCell cell02 = new PdfPCell(new Paragraph("产权证号",FontChinese11Bold));
PdfPCell cell03 = new PdfPCell(new Paragraph("房屋坐落",FontChinese11Bold));
PdfPCell cell04 = new PdfPCell(new Paragraph("房产原值/土地面积",FontChinese11Bold));
cell04.setNoWrap(false);
PdfPCell cell05= new PdfPCell(new Paragraph("应税房产原值/应税土地面积",FontChinese11Bold));
PdfPCell cell06 = new PdfPCell(new Paragraph("应纳税额",FontChinese11Bold));
PdfPCell cell07 = new PdfPCell(new Paragraph("面积",FontChinese11Bold));
PdfPCell cell08 = new PdfPCell(new Paragraph("起时间",FontChinese11Bold));
PdfPCell cell09 = new PdfPCell(new Paragraph("止时间",FontChinese11Bold));
PdfPCell cell10 = new PdfPCell(new Paragraph("年租金",FontChinese11Bold));
PdfPCell cell_11 = new PdfPCell(new Paragraph("承租人/出租人",FontChinese11Bold));
//水平居中
cell01.setHorizontalAlignment(Element.ALIGN_CENTER);
cell02.setHorizontalAlignment(Element.ALIGN_CENTER);
cell03.setHorizontalAlignment(Element.ALIGN_CENTER);
cell04.setHorizontalAlignment(Element.ALIGN_CENTER);
cell05.setHorizontalAlignment(Element.ALIGN_CENTER);
cell06.setHorizontalAlignment(Element.ALIGN_CENTER);
cell07.setHorizontalAlignment(Element.ALIGN_CENTER);
cell08.setHorizontalAlignment(Element.ALIGN_CENTER);
cell09.setHorizontalAlignment(Element.ALIGN_CENTER);
cell10.setHorizontalAlignment(Element.ALIGN_CENTER);
cell_11.setHorizontalAlignment(Element.ALIGN_CENTER);
//垂直居中
cell01.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell02.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell03.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell04.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell05.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell06.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell07.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell08.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell09.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell10.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell_11.setHorizontalAlignment(Element.ALIGN_MIDDLE);
//去掉左右边框
pt01.addCell(cell01);
pt01.addCell(cell02);
pt01.addCell(cell03);
pt01.addCell(cell04);
pt01.addCell(cell05);
pt01.addCell(cell06);
pt01.addCell(cell07);
pt01.addCell(cell08);
pt01.addCell(cell09);
pt01.addCell(cell10);
pt01.addCell(cell_11);
document.add(pt01);
PdfPTable pt02 = new PdfPTable(11);
int widthpt02[] ={20,20,20,20,20,20,15,20,20,15,15};
pt02.setWidths(widthpt02);
PdfPCell cell001 = new PdfPCell(new Paragraph("自用房屋登记",FontChinese11Bold));
PdfPCell cell002 = new PdfPCell(new Paragraph("无产权证",FontChinese11Bold));
PdfPCell cell003 = new PdfPCell(new Paragraph("秦淮路19号",FontChinese11Bold));
PdfPCell cell004 = new PdfPCell(new Paragraph("7464583.31",FontChinese11Bold));
PdfPCell cell005 = new PdfPCell(new Paragraph("7464583.31",FontChinese11Bold));
PdfPCell cell006 = new PdfPCell(new Paragraph("62702.5",FontChinese11Bold));
PdfPCell cell007 = new PdfPCell(new Paragraph("1000",FontChinese11Bold));
PdfPCell cell008 = new PdfPCell(new Paragraph("2005-01-01",FontChinese11Bold));
PdfPCell cell009 = new PdfPCell(new Paragraph("",FontChinese11Bold));
PdfPCell cell100 = new PdfPCell(new Paragraph("",FontChinese11Bold));
PdfPCell cell011 = new PdfPCell(new Paragraph("",FontChinese11Bold));
//表格高度
cell001.setFixedHeight(25);
cell002.setFixedHeight(25);
cell003.setFixedHeight(25);
cell004.setFixedHeight(25);
cell005.setFixedHeight(25);
cell006.setFixedHeight(25);
cell007.setFixedHeight(25);
cell008.setFixedHeight(25);
cell009.setFixedHeight(25);
cell100.setFixedHeight(25);
cell011.setFixedHeight(25);
//水平居中
cell001.setHorizontalAlignment(Element.ALIGN_CENTER);
cell002.setHorizontalAlignment(Element.ALIGN_CENTER);
cell003.setHorizontalAlignment(Element.ALIGN_CENTER);
cell004.setHorizontalAlignment(Element.ALIGN_CENTER);
cell005.setHorizontalAlignment(Element.ALIGN_CENTER);
cell006.setHorizontalAlignment(Element.ALIGN_CENTER);
cell007.setHorizontalAlignment(Element.ALIGN_CENTER);
cell008.setHorizontalAlignment(Element.ALIGN_CENTER);
cell009.setHorizontalAlignment(Element.ALIGN_CENTER);
cell100.setHorizontalAlignment(Element.ALIGN_CENTER);
cell011.setHorizontalAlignment(Element.ALIGN_CENTER);
//垂直居中
cell001.setHorizontalAlignment(Element.ALIGN_MIDDLE);
cell002.setHorizontalAlignment(Element.ALIGN_MIDDLE);
...........
pt02.addCell(cell001);
.....................
document.add(pt02);
document.close();
os.flush();
os.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
}
报错信息:
java.lang.NullPointerException
at com.netbank.corbank.action.TestPdf.testpdf2(TestPdf.java:37)
at com.netbank.corbank.action.TestPdf.main(TestPdf.java:24)
参考:http://blog.csdn.net/liqimo1799/article/details/55049211