亲宝软件园·资讯

展开

java pdf加水印的方法

人气:1

引入依赖

<dependency>
 <groupId>com.itextpdf.tool</groupId>
 <artifactId>xmlworker</artifactId>
 <version>5.5.10</version>
</dependency>
<dependency>
 <groupId>com.itextpdf</groupId>
 <artifactId>itextpdf</artifactId>
 <version>5.5.10</version>
</dependency>

‘/static/fonts/SIMYOU.TTF' 字体 本机没有的话, 可以百度下载

/**
  * pdf 加水印
  *
  * @return
  */
 public byte[] pdfAddWaterMark(byte[] byes) {

  String fileName = UUID.randomUUID().toString() + ".pdf";
  String courseFile = "";
  try {
   // 第二种:获取项目路径 D:\git\daotie\daotie
   //生成临时文件 , 读取完删除
   File directory = new File("");// 参数为空
   courseFile = directory.getCanonicalPath() + "/";
  } catch (IOException e) {
   e.printStackTrace();
  }
  byte[] returnBytes = null;
  // 待加水印的文件
  PdfReader reader = null;
  PdfStamper stamper = null;
//  ByteArrayOutputStream baos = null;
  FileOutputStream os = null;
  try {
   reader = new PdfReader(byes);
   // 加完水印的文件
//   baos = new ByteArrayOutputStream();
//   stamper = new PdfStamper(reader, baos);
   // 加完水印的文件
   os = new FileOutputStream(courseFile + fileName);
   stamper = new PdfStamper(reader, os);

   int total = reader.getNumberOfPages() + 1;
   PdfContentByte content;
   // BaseFont font = BaseFont.createFont();
   BaseFont basefont = BaseFont.createFont("/static/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
   //这里的字体设置比较关键,这个设置是支持中文的写法
   /*BaseFont base = BaseFont.createFont("STSong-Light",
     "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体*/

   /*//设置透明度
   PdfGState gs = new PdfGState();
   gs.setFillOpacity(1f);
   gs.setStrokeOpacity(1f);*/

   PdfContentByte under;
   com.itextpdf.text.Rectangle pageRect = null;

   // 循环对每页插入水印
   for (int i = 1; i < total; i++) {
    pageRect = stamper.getReader().getPageSizeWithRotation(i);
    // 计算水印X,Y坐标
    float x = (float) (pageRect.getWidth() / 1.98);
    float y = (float) (pageRect.getHeight() / 2.8);
    // 获得PDF最顶层
    under = stamper.getOverContent(i);
    under.saveState();
    // set Transparency
    PdfGState gs = new PdfGState();
    // 设置透明度为0.2
    gs.setFillOpacity(1.f);
    under.setGState(gs);
    under.restoreState();
    under.beginText();
    under.setFontAndSize(basefont, pageRect.getHeight() / 17);
    under.setColorFill(BaseColor.RED);

    // 水印文字成45度角倾斜
    System.out.println("width" + pageRect.getWidth());
    System.out.println("height" + pageRect.getHeight());
    System.out.println("x" + x);
    System.out.println("y" + y);
    under.showTextAligned(Element.ALIGN_CENTER, "图片仅供预览,不可用于商业用途", x, y, 45);
    // 添加水印文字
    under.endText();
    under.setLineWidth(1f);
    under.stroke();
   }
//   returnBytes = baos.toByteArray();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (DocumentException e) {
   e.printStackTrace();
  } finally {
   try {
    stamper.close();
    if (os != null) {
     os.close();
    }
    if (reader != null) {
     reader.close();
    }
   } catch (DocumentException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论