java 利用itext生成PDF并下载

使用itext插件生成pdf并下载,懒得写了,直接代码

/**
	 * 测试生成pdf下载
	 * @author liqimo
	 * @param request
	 * @param response
	 * @return null;
	 */
	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("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);   
            BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/simhei.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);   
            com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);
            com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD); 
            com.itextpdf.text.Font FontChinese16 = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);
            com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);
            com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.BOLD);
            com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);
            com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL);

            document.open();
            //test
            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));
            
          //table7
            PdfPTable pt01 = new PdfPTable(11);
            BaseColor lightGrey01 = new BaseColor(0xCC,0xCC,0xCC);
            int widthpt01[] = {20,20,20,20,20,20,15,20,20,15,15};
            pt01.setWidths(widthpt01); 
            PdfPCell cell01 = new PdfPCell(new Paragraph("登记来源",FontChinese11Bold));
            BaseColor bc = new BaseColor(102, 204, 255);
            cell01.setBackgroundColor(bc);
            PdfPCell cell02 = new PdfPCell(new Paragraph("产权证号",FontChinese11Bold));
            cell02.setBackgroundColor(bc);
            PdfPCell cell03 = new PdfPCell(new Paragraph("房屋坐落",FontChinese11Bold));
            cell03.setBackgroundColor(bc);
            PdfPCell cell04 = new PdfPCell(new Paragraph("房产原值/土地面积",FontChinese11Bold));
            cell04.setBackgroundColor(bc);
            cell04.setNoWrap(false);
            PdfPCell  cell05= new PdfPCell(new Paragraph("应税房产原值/应税土地面积",FontChinese11Bold));
            cell05.setBackgroundColor(bc);
            PdfPCell cell06 = new PdfPCell(new Paragraph("应纳税额",FontChinese11Bold));
            cell06.setBackgroundColor(bc);
            PdfPCell cell07 = new PdfPCell(new Paragraph("面积",FontChinese11Bold));
            cell07.setBackgroundColor(bc);
            PdfPCell cell08 = new PdfPCell(new Paragraph("起时间",FontChinese11Bold));
            cell08.setBackgroundColor(bc);
            PdfPCell cell09 = new PdfPCell(new Paragraph("止时间",FontChinese11Bold));
            cell09.setBackgroundColor(bc);
            PdfPCell cell10 = new PdfPCell(new Paragraph("年租金",FontChinese11Bold));
            cell10.setBackgroundColor(bc);
            PdfPCell cell_11 = new PdfPCell(new Paragraph("承租人/出租人",FontChinese11Bold));
            cell_11.setBackgroundColor(bc);
            
            									
            //表格高度
            //cell01.setFixedHeight(25);
           
            //水平居中
            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);
      
            //边框颜色
            cell01.setBorderColor(lightGrey01);
            cell02.setBorderColor(lightGrey01);
            cell03.setBorderColor(lightGrey01);
            cell04.setBorderColor(lightGrey01);
            cell05.setBorderColor(lightGrey01);
            cell06.setBorderColor(lightGrey01);
            cell07.setBorderColor(lightGrey01);
            cell08.setBorderColor(lightGrey01);
            cell09.setBorderColor(lightGrey01);
            cell10.setBorderColor(lightGrey01);
            cell_11.setBorderColor(lightGrey01);
            //去掉左右边框
            /**
            cell71.disableBorderSide(8);
            **/
            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);
            BaseColor lightGrey02 = new BaseColor(0xCC,0xCC,0xCC);
            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);
            cell003.setHorizontalAlignment(Element.ALIGN_MIDDLE);
            cell004.setHorizontalAlignment(Element.ALIGN_MIDDLE);
            cell005.setHorizontalAlignment(Element.ALIGN_MIDDLE);
            cell006.setHorizontalAlignment(Element.ALIGN_MIDDLE);
            cell007.setHorizontalAlignment(Element.ALIGN_MIDDLE);
            cell008.setHorizontalAlignment(Element.ALIGN_MIDDLE);
            cell009.setHorizontalAlignment(Element.ALIGN_MIDDLE);
            cell100.setHorizontalAlignment(Element.ALIGN_MIDDLE);
            cell011.setHorizontalAlignment(Element.ALIGN_MIDDLE);
      
            //边框颜色
            cell001.setBorderColor(lightGrey02);
            cell002.setBorderColor(lightGrey02);
            cell003.setBorderColor(lightGrey02);
            cell004.setBorderColor(lightGrey02);
            cell005.setBorderColor(lightGrey02);
            cell006.setBorderColor(lightGrey02);
            cell007.setBorderColor(lightGrey02);
            cell008.setBorderColor(lightGrey02);
            cell009.setBorderColor(lightGrey02);
            cell100.setBorderColor(lightGrey02);
            cell011.setBorderColor(lightGrey02);
            pt02.addCell(cell001);
            pt02.addCell(cell002);
            pt02.addCell(cell003);
            pt02.addCell(cell004);
            pt02.addCell(cell005);
            pt02.addCell(cell006);
            pt02.addCell(cell007);
            pt02.addCell(cell008);
            pt02.addCell(cell009);
            pt02.addCell(cell100);
            pt02.addCell(cell011);
            document.add(pt02);
            
            

            document.close();
            os.flush();
            os.close();

        } catch (Exception ex) 
        {
          ex.printStackTrace();
        }
		return null;
	}

结果图:


猜你喜欢

转载自blog.csdn.net/liqimo1799/article/details/55049211