itextpdf导出pdf流,写入表格 ,以及生成水印

导出PDF流

设置响应参数HttpServletResponse

     		response.setCharacterEncoding("utf-8");
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(tempName + ".pdf", "UTF-8"));

将数据写入到response.getOutputStream()输出流,以流的形式返回给前端

            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
            document.open();
            //设置字体 中文需要导入itext-asian包 否则中文将中不到对应的字体不显示
            BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font f1 = new Font(bfChinese);
            //通过反射去获取字段
            Field[] declaredFields = tempClass.getDeclaredFields();
            PdfPTable leftTable = new PdfPTable(declaredFields.length);
             //表头
            for (Field declaredField : declaredFields) {
            	//获取ExcelProperty注解中value值
                String name=declaredField.getAnnotation(ExcelProperty.class).value()[0];
                PdfPCell topCell = new PdfPCell(new Paragraph(name,f1));
                //设置水平垂直居中
                topCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                topCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                leftTable.addCell(topCell);
            }
           //表格内容
            for (Object datum : data) {
                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    Object o = declaredField.get(datum);
                    String text = Objects.isNull(o)?"":o.toString();
                    //特殊字段需要处理 可以去掉
                    if(StringUtil.equals("isCancel",declaredField.getName())){
                        text = Objects.equals(o, YesOrNoEnum.NO.getCode())?"有效":"已失效";
                    }
                    PdfPCell cell = new PdfPCell(new Paragraph(text,f1));
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    leftTable.addCell(cell);
                }
            }
            document.add(leftTable);
            //水印文字 实现换行
            List<String> watermarks= new ArrayList<>();
            watermarks.add(LocalDate.now().format(DatePattern.NORM_DATE_FORMATTER));
            watermarks.add(LocalDate.now().format(DatePattern.NORM_DATETIME_MINUTE_FORMATTER));
            watermarks.add(LocalDate.now().format(DatePattern.CHINESE_DATE_TIME_FORMATTER));
            Font watermarkFont = new Font(f1.getBaseFont(),100, Font.NORMAL,new BaseColor(223,223,223,120));
            Rectangle rectangle =writer.getPageSize();
            for (int i = 0; i < watermarks.size(); i++) {
                ColumnText.showTextAligned(writer.getDirectContentUnder(), Element.ALIGN_CENTER, new Phrase(watermarks.get(i), watermarkFont), rectangle.getRight()/2, rectangle.getTop()/2-(150*i)+200, 30);
            }
            //关闭流
            document.close();
            writer.close();

pom文件
注:itextpdf 和 asian的版本需对应,否则将会出现一系列报错 asian版本过低 将提示找不到cjk_registry.properties文件

    <dependency> 
      <groupId>com.itextpdf</groupId>  
      <artifactId>itextpdf</artifactId>  
      <version>5.5.9</version> 
    </dependency>  
    
    <dependency> 
      <groupId>com.itextpdf</groupId>  
      <artifactId>itext-asian</artifactId>  
      <version>5.2.0</version> 
    </dependency>  

UniGB-UCS2-H :为cjk_registry.properties 的Adobe_GB1中的一个值 可替换为其他值
STSong-Light :为cjk_registry.properties 的fonts中的一个值 可替换为其他值

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值