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


2020年4月10日 星期五

Maven create PDF file By WebGet Html content

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>

public static void createPdf(String url, String id, String outPath) throws Exception {
  //Pagesize, marginLeft, marginRight, marginTop, marginBottom
Document document = new Document(PageSize.A4, 20, 45, 20, 45);
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(outPath + id + ".pdf"));
document.open();
document.addAuthor("***");
document.addTitle("***");
document.addCreator("***");
document.addHeader("Domain", "www.***.gov.tw");
document.addKeywords("**");
document.addSubject("**");
                
                //Referenct by https://seed0111.blogspot.com/2020/04/maven-get-html-content-use-httppost.html
String _html = getHtmlContent(url, caseno);
try {
//InputStream cssInput =new URL("http://xxx/xx.css").openConnection().getInputStream();
InputStream cssInput = null;
//HTML To PDF
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document,new ByteArrayInputStream(_html.getBytes("UTF-8")),cssInput,Charset.forName("UTF-8"), new AsianFontProvider());
}catch (IOException e) {
                        log.error("createPdf has exception " + e.getMessage(), e);
               }finally {
                        document.close();
               }
}
※ 部份環境中文字會有亂碼產生,故_html.getBytes() =>  _html.getBytes("UTF-8") 排除

public class AsianFontProvider extends XMLWorkerFontProvider {
   @Override
    public Font getFont(final String fontname, final String encoding,
            final boolean embedded, final float size, final int style,
            final BaseColor color) {
            BaseFont bf = null;
        try {
        /*
        // 設定中文字體
String chMingliu0FontPath = "c:\\Windows\\Fonts\\mingliu.ttc,0";//windows內建的細明體                           
String chMingliu1FontPath = "c:\\Windows\\Fonts\\mingliu.ttc,1";//windows內建的新細明體                         
        */
        String chKaiuFontPath = "c:\\Windows\\Fonts\\KAIU.TTF";//windows內建的標楷體
bf = BaseFont.createFont(chKaiuFontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
           e.printStackTrace();
        }
        Font font = new Font(bf, size, style, color);
        font.setColor(color);
        return font;
    }

}
※ 部份環境無法讀取TTC 選TTF字型檔可以排除

// PDF設定縮放
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, document.getPageSize().getHeight(), 0.8f);
PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, pdfWriter);
pdfWriter.setOpenAction(action);

Maven Get Html Content use HttpPost

//url: target url address
//id: post include params 
private static String getHtmlContent(String url, String id) throws IOException {
System.setProperty("javax.net.debug","ssl,handshake");
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation","true");
String content = "";
try {   
                   //if use https or call https url addr than show handshake failed return, can add in pass check
SSLContext sslcontext = SSLContexts.createDefault();
SSLConnectionSocketFactory sslsf = new 
              SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1.2"}, null, new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        
        try {        
    //Use HttpGet method 
    //HttpGet webGet = new HttpGet(url);
                            //Use HttpPost method 
    HttpPost webGet = new HttpPost(url); 
    ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>( );
    params.add(new BasicNameValuePair("id" ,id )) ;
    webGet.setEntity(new UrlEncodedFormEntity(params));
    
    CloseableHttpResponse response = httpClient.execute(webGet);
    HttpEntity entity = response.getEntity();
    content = EntityUtils.toString(entity, "UTF-8");
    content = content.replace("&emsp;", "<font color='white'>_</font>");
    //System.out.println(content);
    EntityUtils.consume(entity);
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
} finally {
httpClient.close();
}         
} catch (Exception ex) {
log.error("createIgnoreVerifySSL has exception " + ex.getMessage(), ex);
}
    return content;
}