ireport给pdf加图片水印

纳尼,要给pdf加图片水印,ireport无法满足需求???
最总发现使用 itext 满足了需求
父pom引入

 <!-- ireport报表-->
    <dependency>
         <groupId>net.sf.jasperreports</groupId>
         <artifactId>jasperreports</artifactId>
         <version>6.0.0</version>
     </dependency>
     <dependency>
         <groupId>com.itextpdf</groupId>
         <artifactId>itext-asian</artifactId>
         <version>5.2.0</version>
     </dependency>
     <dependency>
         <groupId>com.itextpdf</groupId>
         <artifactId>itext-asiancmaps</artifactId>
         <version>5.1.1</version>
     </dependency>
@Data
@ApiModel("获取文件DTO")
public class DownloadFileDTO implements Serializable {

    private static final long serialVersionUID = 2839509383419821357L;

    private byte[] fileByte;
}

 WrapperResponse<DownloadFileDTO> response = signatureHttpReqService.signPost(sealDocReqDTO);
		//流的方式输入输出文件
		HttpServletResponse httpres = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
        httpres.setContentType("application/pdf");
        httpres.setCharacterEncoding("UTF-8");
        httpres.setHeader("Content-Disposition", "inline;");
        InputStream in = new ByteArrayInputStream(response.getData().getByteFile());
        OutputStream out =null;
        try{
            out = httpres.getOutputStream();
            setWatermark(in,out);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(in != null){
                in.close();
            }
 
            if(out != null){
                out.close();
            }
        }
	/**
    *添加水印的方法
    **/
public static void setWatermark(InputStream in, OutputStream bos) throws IOException {
        PdfReader reader = null;
        PdfStamper stamper = null;
        ClassPathResource resource1 = new ClassPathResource("mark" + File.separator + "printMark.jpg");
        InputStream picture = resource1.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] rs = new byte[1024];
        int len = 0;
        while ((len = picture.read(rs)) >= 0) {
            byteArrayOutputStream.write(rs, 0, len);
        }
        byte[] rs1 = byteArrayOutputStream.toByteArray();
        try {
            reader = new PdfReader(in);
            stamper = new PdfStamper(reader, bos);
            int total = reader.getNumberOfPages() + 1;
            PdfContentByte content;
            PdfGState gs = new PdfGState();
            for (int i = 1; i < total; i++) {
                int io = reader.getPageRotation(i);
                content = stamper.getOverContent(i);// 在内容上方加水印
//            content = stamper.getUnderContent(i);//在内容下方加水印
                gs.setFillOpacity(1.0f); // 透明度
                content.setGState(gs);
                Image image = Image.getInstance(rs1);
                image.setAlignment(Image.TEXTWRAP);
                // 设置边框属性
//            image.setBorder(Image.BOX);
//            image.setBorderWidth(10);
//            image.setBorderColor(Color.WHITE);
//            image.setRotationDegrees(30);//旋转
                Rectangle rectangle = reader.getPageSize(i);
                image.scaleToFit(350, 200);//大小
                if (io > 0) {
                    image.setAbsolutePosition((rectangle.getHeight() / 2) - 175, (rectangle.getWidth() / 2) - 100);
                } else {
                    image.setAbsolutePosition((rectangle.getWidth() / 2) - 175, (rectangle.getHeight() / 2) - 100);
                }
//            image.setAbsolutePosition(200, 206);
                content.addImage(image);
                bos.flush();
            }
        } catch (IOException e) {
            System.out.println("添加水印失败!错误信息为: " + e);
            e.printStackTrace();
        } catch (DocumentException e) {
            System.out.println("添加水印失败!错误信息为: " + e);
            e.printStackTrace();
        } finally {
            //关闭流
            if (stamper != null) {
                try {
                    stamper.close();
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                reader.close();
            }
        }
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值