溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用java怎么給pdf加水印

發布時間:2021-06-11 15:52:45 來源:億速云 閱讀:274 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關使用java怎么給pdf加水印,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

引入依賴

<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();
   }
  }

看完上述內容,你們對使用java怎么給pdf加水印有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女