您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)使用java怎么給pdf加水印,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
引入依賴
<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' 字體 本機(jī)沒(méi)有的話, 可以百度下載
/** * pdf 加水印 * * @return */ public byte[] pdfAddWaterMark(byte[] byes) { String fileName = UUID.randomUUID().toString() + ".pdf"; String courseFile = ""; try { // 第二種:獲取項(xiàng)目路徑 D:\git\daotie\daotie //生成臨時(shí)文件 , 讀取完刪除 File directory = new File("");// 參數(shù)為空 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); //這里的字體設(shè)置比較關(guān)鍵,這個(gè)設(shè)置是支持中文的寫法 /*BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系統(tǒng)字體*/ /*//設(shè)置透明度 PdfGState gs = new PdfGState(); gs.setFillOpacity(1f); gs.setStrokeOpacity(1f);*/ PdfContentByte under; com.itextpdf.text.Rectangle pageRect = null; // 循環(huán)對(duì)每頁(yè)插入水印 for (int i = 1; i < total; i++) { pageRect = stamper.getReader().getPageSizeWithRotation(i); // 計(jì)算水印X,Y坐標(biāo) 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(); // 設(shè)置透明度為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, "圖片僅供預(yù)覽,不可用于商業(yè)用途", 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(); } }
看完上述內(nèi)容,你們對(duì)使用java怎么給pdf加水印有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。