2020年6月18日 星期四

XMLWorkerHelper加PDF浮水印



File file = new File(outFilePath + "filename.pdf");
String DEST = outFilePath + "filename_water.pdf";
FileInputStream input1 = new FileInputStream(file);
FileOutputStream output2 = new FileOutputStream(new File(DEST));
stringWaterMark(input1, output2, "浮水印測試", 2, 3, 0.15f,45, 84,BaseColor.BLACK);

/* 斜角排列、多個重複的花式文字浮水印
     * @param input             需要加水印的PDF讀取輸入流
     * @param output            輸出生成PDF的輸出流
     * @param waterMarkString   浮水印字
     * @param xAmout            x軸重複數量
     * @param yAmout            y軸重複數量
     * @param opacity           水印透明度
     * @param rotation          水印文字旋轉角度,一般為45度角
     * @param waterMarkFontSize 水印字型大小
     * @param color             水印字型顏色
     */
public static void stringWaterMark(InputStream input, OutputStream output, String waterMarkString, int xAmout, int yAmout, float opacity, float rotation, int waterMarkFontSize, BaseColor color) {
        try {

            PdfReader reader = new PdfReader(input);
            PdfStamper stamper = new PdfStamper(reader, output);

            // 新增中文字型
            String chMingliu1FontPath = "c:\\Windows\\Fonts\\mingliu.ttc,0";//windows內建的新細明體         
            BaseFont baseFont = BaseFont.createFont(chMingliu1FontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

            int total = reader.getNumberOfPages() + 1;

            PdfContentByte over;
            // 給每一頁面加浮水印
            for (int i = 1; i < total; i++) {
                Rectangle pageRect = stamper.getReader().getPageSizeWithRotation(i);
                // 計算水印每個單位距離X,Y
                float x = pageRect.getWidth() / xAmout;
                float y = pageRect.getHeight() / yAmout;

                over = stamper.getOverContent(i);
                PdfGState gs = new PdfGState();
                // 設定透明度為
                gs.setFillOpacity(opacity);

                over.setGState(gs);
                over.saveState();

                over.beginText();
                over.setColorFill(color);
                over.setFontAndSize(baseFont, waterMarkFontSize);

                for (int n = 0; n < xAmout + 1; n++) {
                    for (int m = 0; m < yAmout + 1; m++) {
                        over.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x * n, y * m, rotation);
                    }
                }

                over.endText();
            }
            stamper.close();
        } catch (Exception e) {
        System.out.println("PDF error "+e.getMessage());
        }
}

2020年6月15日 星期一

GRADLE 安裝備忘

最近碰到Spring Batch的專案需求,編譯環境的設定記錄下...以後可以參考用。

1. 下載: https://gradle.org/releases/

2. 解壓縮後,將bin path設定加入到系統環境變數的path裡

3. 自命令提示字元中(終端機) 輸入 Gradle –v 進行測試,若設定OK會回版本號回來

4. gradle build 進行專案編譯


使用Enter鍵來移動焦點至下個輸入元素

用AJAX做功能習慣了,碰到習慣submit的使用者,對於封鎖在輸入框按Enter做提交的設計,給了許多的負評,沒辦法改變人那就改變寫法吧,把ENTER的封鎖改成切換焦點方式,當切到按鈕處在執行提交,那頁面功能就不會異常了,寫法如下~~

 var allInputSelector = ":input:visible:enabled"; 
 $("body").on("keydown", allInputSelector + ":not(textarea, :button)", function(event){
    //排除 focus 在 textarea 或 button 時觸發 
    if (event.keyCode == 13) { 
         $(allInputSelector + ":eq(" + ($(allInputSelector).index($(this)) + 1) + ")").focus();
         event.preventDefault(); 
     } 
 });