<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                pdf添加水印logo這種需求場景確實很少,有些時候一些銷售單據生成pdf添加一個水印logo,做一個簡單的防偽效果,雖然實際上并沒有太大作用,但是產品經理說要,巴拉巴拉……省略一萬字。 下面將源碼分享給猿友們,有用就looklook,沒用就轉移視線吧。 ### 一、效果展示 沒加水印的pdf: ![](https://box.kancloud.cn/2016-03-15_56e77db773081.jpg) 添加水印后的pdf: ![](https://box.kancloud.cn/2016-03-15_56e77db799316.jpg) 這里截圖效果可能不是很明顯,有需要的猿友可以直接下載下面的源碼壓縮包,里面有添加水印后的pdf文件。 ### 二、源碼下載 [http://download.csdn.net/detail/u013142781/9421432](http://download.csdn.net/detail/u013142781/9421432) 里面的pdf_project.zip是源碼,源碼環境eclipse+maven ### 三、實例說明 工程需要添加itextpdf包依賴,maven依賴為: ~~~ <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.3</version> </dependency> ~~~ 除了依賴以外,就一個類PDFAddWaterMark.java了,其代碼如下: ~~~ package com.luo.pdf; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfGState; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import java.io.File; import java.io.FileOutputStream; import java.util.Random; public class PDFAddWaterMark { static final float IAMGE_HEIGHT = 110f; // 限制水印圖片的的高度 static final float IAMGE_WIDTH = 110f; // 限制水印圖片的的寬度 /** * 給pdf文件添加水印 * @param file 要加水印的原pdf文件 * @throws Exception */ public static void addPdfMark(File file) throws Exception { // 如果是web項目,應該從web項目里面獲取logo // String markImagePath = // request.getSession().getServletContext().getRealPath("/") + // "resources/images/logo.jpg"; String markImagePath = "C:/Users/luoguohui/Desktop/logo.jpg"; // 水印圖片路徑 PdfReader reader = new PdfReader(file.getPath(), "PDF".getBytes()); // 如果是web項目,直接下載應該放到response的流里面 // PdfStamper stamp = new PdfStamper(reader, // response.getOutputStream()); // 添加水印之后的pdf文件 PdfStamper stamp = new PdfStamper(reader, new FileOutputStream( "C:/Users/luoguohui/Desktop/afterAddPdfMark.pdf")); int pageSize = reader.getNumberOfPages(); float pageHeight = reader.getPageSize(1).getHeight(); float pageWidth = reader.getPageSize(1).getWidth(); try { // 每兩行顯示兩個 左邊一個,右邊一個 int lineNum = (int) (pageHeight / IAMGE_HEIGHT); // 行數 int middleY = (int) pageWidth / 2; for (int i = 1; i <= pageSize; i++) { for (int j = 0, k = 0; j < lineNum; j = j + 2, k++) { Random random = new Random(); Image img = Image.getInstance(markImagePath);// 插入水印 img.scaleAbsolute(IAMGE_WIDTH, IAMGE_HEIGHT * 184 / 455); img.setAlignment(Image.UNDERLYING); // 在字下面 int trueY; while (true) { trueY = random.nextInt(middleY); if (trueY > IAMGE_WIDTH / 2 && trueY < (middleY - IAMGE_WIDTH)) { break; } } img.setAbsolutePosition(trueY, j * IAMGE_HEIGHT + (float) random.nextInt((int) IAMGE_HEIGHT) - (k % 2) * 10); // 水印的位置 img.setRotationDegrees(random.nextInt(360));// 旋轉 角度 PdfContentByte under = stamp.getUnderContent(i); PdfGState gs = new PdfGState(); gs.setFillOpacity(0.3f); // 設置透明度為0.3 under.setGState(gs); under.addImage(img); while (true) { trueY = random.nextInt(middleY) + middleY; if (trueY > middleY + IAMGE_WIDTH / 2 && trueY < (2 * middleY - IAMGE_WIDTH)) { break; } } img.setAbsolutePosition(trueY, j * IAMGE_HEIGHT + (float) random.nextInt((int) IAMGE_HEIGHT) - (k % 2) * 10); // 水印的位置 img.setRotationDegrees(random.nextInt(360));// 旋轉 角度 under.addImage(img); } } } catch (Exception e) { throw e; } finally { stamp.close();// 關閉 reader.close(); } } public static void main(String[] args) throws Exception { // 添加水印之前的pdf文件 File file = new File("C:/Users/luoguohui/Desktop/beforeAddPdfMark.pdf"); addPdfMark(file); } } ~~~ 這里有必要對上面的代碼說明的是:其實就是在pdf里面加入圖片logo,只是這些圖片logo不會遮擋住文字,而且有一定的透明度。另外logo隨機旋轉一定的角度。再另外,其出現的位置,根據當前頁的高度計算出一頁需要多少行logo,每行左邊一個右邊一個,然后其位置上下左右在一定范圍內隨機。代碼實現就這樣,如果還有什么不明白的可以留言。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看