如何利用java 利用itext生成PDF并下载

zr_dixuexiongying 2018-02-28 09:16:17
这是我在网上找的一个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
...全文
1163 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
济南大飞哥 2018-04-12
  • 打赏
  • 举报
回复
初学一个东西,你找的例子应该是hello world而不是复杂的例子。itext生成pdf跟html做页面差不多。另外还有基于模板的。
不胖不换ID 2018-04-10
  • 打赏
  • 举报
回复
itext官网上有demo。自己可以去看下 。https://developers.itextpdf.com/
诺丽果 2018-03-20
  • 打赏
  • 举报
回复
testpdf2( request, response);中的这两个参数去掉,参照我上面写的代码
诺丽果 2018-03-01
  • 打赏
  • 举报
回复
肯定空指针啊,你在main方法里调用testpdf2( request, response);的时候,HttpServletRequest request = null; HttpServletResponse response = null;这两个都是空啊,你传的参数是空的。
zr_dixuexiongying 2018-03-01
  • 打赏
  • 举报
回复

那你说这段程序应该怎么改啊!望赐教
诺丽果 2018-03-01
  • 打赏
  • 举报
回复
package com.daoben.robot.controller.web; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.AcroFields; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; /** * @author zhaoxl * @date ����ʱ�䣺2018��3��1�� ����11:17:37 * @version 1.0 * @parameter * @since * @return */ public class TestPdf { public static void main(String[] args) throws Exception { test(); System.out.println("success"); } public static void test() throws IOException, DocumentException { String fileName = "F:/学习/test.pdf"; // pdf模板 PdfReader reader = new PdfReader(fileName); ByteArrayOutputStream bos = new ByteArrayOutputStream(); /* 将要生成的目标PDF文件名称 */ PdfStamper ps = new PdfStamper(reader, bos); PdfContentByte under = ps.getUnderContent(1); /* 使用中文字体 */ BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); ArrayList<BaseFont> fontList = new ArrayList<BaseFont>(); fontList.add(bf); /* 取出报表模板中的所有字段 */ AcroFields fields = ps.getAcroFields(); fields.setSubstitutionFonts(fontList); fillData(fields, data()); /* 必须要调用这个,否则文档不会生成的 */ ps.setFormFlattening(true); ps.close(); OutputStream fos = new FileOutputStream("F:/学习/testResult.pdf"); fos.write(bos.toByteArray()); fos.flush(); fos.close(); bos.close(); } public static void fillData(AcroFields fields, Map<String, String> data) throws IOException, DocumentException { for (String key : data.keySet()) { String value = data.get(key); fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的 } } public static Map<String, String> data() { Map<String, String> data = new HashMap<String, String>(); data.put("name", "test:"); data.put("bianhao", "xx第10000001号"); data.put("amount", "1000"); data.put("date", "2015年7月7日"); return data; } } 需要下载iText-2.1.5.jar和iTextAsian.jar

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧