pdf文件加水印输出图片

MAVEN依赖

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

代码片段

/**pdffile-文件,targetPath-生成图片保存位置,index文件的页数,water是否加水印*/

public static void pdfFileToImage(File pdffile, String targetPath, int index, boolean water) {
  try {
    FileInputStream instream = new FileInputStream(pdffile);
    InputStream byteInputStream = null;
    try {
      PdfReader reader = new PdfReader(instream);
      int pageCount = reader.getNumberOfPages();
      ByteArrayInputStream swapStream = null;
      if (pageCount > 0) {
        if (water) {
          BaseFont base =
              BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
          PdfContentByte content;
          PdfGState gs = new PdfGState();
          // 在内容下方加水印
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          PdfStamper stamper = new PdfStamper(reader, bos);
          // 水印加在内容上方
          content = stamper.getOverContent(index + 1);
          // 设置透明度
          gs.setFillOpacity(0.4f);
          content.beginText();
          content.setFontAndSize(base, 50);
          content.setTextMatrix(70, 200);
          content.setColorFill(BaseColor.GRAY);
          content.showTextAligned(Element.ALIGN_CENTER, "这里是水印文字", 300, 250, 55);
          content.showTextAligned(Element.ALIGN_CENTER, "这里是水印文字", 300, 600, 55);
          content.endText();

          stamper.setFormFlattening(true);
          stamper.close();

          Document doc = new Document();
          ByteArrayOutputStream bout = new ByteArrayOutputStream();
          PdfCopy copy = new PdfCopy(doc, bout);
          doc.open();
          // 输出到doc
          for (int j = 1; j <= pageCount; j++) {
            copy.addPage(copy.getImportedPage(new PdfReader(bos.toByteArray()), j));
          }
          // copy.addPage(copy.getImportedPage(new PdfReader(bos.toByteArray()), index+1));
          doc.close();
          swapStream = new ByteArrayInputStream(bout.toByteArray());
          // index = 0;
        }
        PDDocument doc;
        if (swapStream == null) {
          // instream = new FileInputStream(pdffile);
          doc = PDDocument.load(pdffile);
        } else {
          doc = PDDocument.load(swapStream);
        }

        PDFRenderer renderer = new PDFRenderer(doc);
        BufferedImage image = renderer.renderImage(index, 2.0f);
        image.flush();
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imOut;
        imOut = ImageIO.createImageOutputStream(bs);
        ImageIO.write(image, "png", imOut);
        byteInputStream = new ByteArrayInputStream(bs.toByteArray());
      }
      reader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    File uploadFile = new File(targetPath);
    FileOutputStream fops;
    fops = new FileOutputStream(uploadFile);
    fops.write(readInputStream(byteInputStream));
    fops.flush();
    fops.close();
    byteInputStream.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值