package com.jeeplus.modules.esignature.service.docdeal;

import com.alibaba.fastjson.JSONObject;
import com.jeeplus.modules.esignature.kinggridserver.KingGridServer;
import com.jeeplus.modules.esignature.utils.HttpRequest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 文档处理接口,添加二维码，文字水印，图片水印
 * @author Administrator
 *
 */
public class DealPdfService {
	
	/**
	 * logo 水印图片
	 */
	//public static final String LOGO_IMAGE = "[encoded data, 11064 characters]";

	public static final String LOGO_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAAJ8AAAA6CAIAAAAhqBzqAAACvElEQVR42u2cUW7EMAhEuf+lU1WVqqpqHAwzA05B+7XatR2ewXjixK6x95qNC4bu2NAdG7pjQ3ds6I4N3aE7NnRvm7HPT/drNfwvqxpUx+4X4M6Mv0f4OEj4tRQ5xwp814HuepD+X5KcA+raaoKjJAkHYteszDOIAVhNcAhi9AWZOT0Mo3i2kO5iAOfSjY7kKLq/coPnc3pVlfOnxRMd6jIC7cd6DNPNk4bQ3f87mm54NoTdx6YLyUZN6QaSYeaDdVyTESZTvYJu+R40NsKf37dadyV7X+OmTUEJJlh34XQD4kZoHexBV9MIm25hwr9pwQA5k5FRD43dxQIBDxLHFDEA2sxMVyaAtvtdWtFn2SsMJxm4hHsu3Yt1i9CQ+aFWemRLXeUVxtD9F3Td7VgLtNgdkWd565aZOa01oIuVgZzSVRO6znkZrXmtC1qIVrVuraFWtf4mXcYaqzQn7etfcAfwcf7h4sEUG4wLemxDfwdQojx4+03RJaVlXml9Vs38uOuFykcSutTzVs3p7jbonwEsun0Ct21mDk+Xw+iyD0p2o/snUdQgNz3Jv73T5zYile46WI+h2ypwlXQzexWUx3R0OwTuJTwTmVk1UU7bdKaJApdnyjORzl54IUGkmxGVxHRr1TQS3X1/pk/YgtZ/fOy2ehoRS9ffLVhgi2Uzauw6w2voRmQgpeOSal+T05/t6OY1GmCPmWqgXC59zEZb3Yro8pTbcFgoK4b8kcJiundeQ2HOz5gOb1bQtmBctFjGmb8zolxPd2sbBj4TuRsTvOCDJFsgYOojhNvPIlDdnQfM09fygNPicKTO334WgTqpZXU19kms/BkBbFp+6sLUXJVqJfxBtGR+8hSbUK20osTQCJaF7xJzTl++1GPFmfM65LV9+TWowqzVaGAk+jRb6sx5g++bbegO3bEz7QP/rsdwun1HBAAAAABJRU5ErkJggg==";
	
	public static void main(String[] args) throws IOException {
		pdf_add_watermarkimage("C:\\Users\\admin\\Desktop\\kinggrid:/test.pdf");
	}
	
	/**
	 * 添加图片水印
	 * @param saveFile
	 * @throws IOException
	 */
	public static void pdf_add_watermarkimage(String saveFile) throws IOException{
		HttpRequest request = KingGridServer.createHttpRequest("/api/cnt/dealPDF");
		InputStream is = null;
		try{
			is = DealPdfService.class.getResourceAsStream("/kinggrid/demo.pdf");
			request.addInputStream("pdf_file", "/kinggrid/demo.pdf", is);
			
			JSONObject watermarkimageJson = new JSONObject(true);
			watermarkimageJson.put("right", "1%");
			watermarkimageJson.put("top", "99%");
			watermarkimageJson.put("metaname", "文字水印");
			watermarkimageJson.put("imgData", LOGO_IMAGE);
			System.out.println("watermarkimage JSON:"+watermarkimageJson.toJSONString());
			
			request.addParam("watermarkimage",watermarkimageJson.toJSONString());
			request.send();
			int code = request.getResponseCode();
			System.out.println("状态码："+code);
			if(code==200){
				System.out.println("生成成功");
				FileOutputStream fos = null;
				try{
					File file  = new File(saveFile);
					file.getParentFile().mkdirs();
					fos = new FileOutputStream(file);
					System.out.println("保存文件至："+saveFile);
					request.result(fos);
				}finally{
					if(fos!=null){
						fos.close();
					}
				}
			}else{
				System.out.println("错误信息："+request.resultToString());
			}
			
		}finally{
			if(is!=null){
				is.close();
			}
		}
	}
	
	/**
	 * pdf添加文本水印
	 * @throws IOException 
	 */
	public static void pdf_add_watermarktext(String saveFile) throws IOException{
		HttpRequest request = KingGridServer.createHttpRequest("/api/cnt/dealPDF");
		InputStream is = null;
		try{
			is = DealPdfService.class.getResourceAsStream("/kinggrid/demo.pdf");
			request.addInputStream("pdf_file", "/kinggrid/demo.pdf", is);
			
			JSONObject watermarktextJson = new JSONObject();
			watermarktextJson.put("x", "50%");
			watermarktextJson.put("y", "50%");
			watermarktextJson.put("fontsize", "40");
			watermarktextJson.put("basefont", "STXINGKA.TTF");
			watermarktextJson.put("color", "#1873c9");//16进制RGB颜色类型，如00ff00。（默认000000，纯黑）
			watermarktextJson.put("textval", "信签事业部");
			watermarktextJson.put("metaname", "文字水印");
			System.out.println("watermarktext JSON:"+watermarktextJson.toJSONString());
			
			request.addParam("watermarktext",watermarktextJson.toJSONString());
			request.send();
			int code = request.getResponseCode();
			System.out.println("状态码："+code);
			if(code==200){
				System.out.println("生成成功");
				FileOutputStream fos = null;
				try{
					File file  = new File(saveFile);
					file.getParentFile().mkdirs();
					fos = new FileOutputStream(file);
					System.out.println("保存文件至："+saveFile);
					request.result(fos);
				}finally{
					if(fos!=null){
						fos.close();
					}
				}
			}else{
				System.out.println("错误信息："+request.resultToString());
			}
			
		}finally{
			if(is!=null){
				is.close();
			}
		}
	}
	
	/**
	 * pdf添加二维码
	 * @throws IOException 
	 */
	public static void pdf_add_qrcode(String saveFile) throws IOException{
		HttpRequest request = KingGridServer.createHttpRequest("/api/cnt/dealPDF");
		InputStream is = null;
		try{
			is = DealPdfService.class.getResourceAsStream("/demo.pdf");
			request.addInputStream("pdf_file", "demo.pdf", is);
			
			JSONObject qrcodeJson = new JSONObject();
			qrcodeJson.put("metaname", "二维码名称");
			qrcodeJson.put("right", "1%");
			qrcodeJson.put("top", "1%");
			qrcodeJson.put("textval", "www.tosign.cn");
			
			System.out.println("qrcode JSON:"+qrcodeJson.toJSONString());
			
			request.addParam("qrcode",qrcodeJson.toJSONString());
			request.send();
			int code = request.getResponseCode();
			System.out.println("状态码："+code);
			if(code==200){
				System.out.println("生成成功");
				FileOutputStream fos = null;
				try{
					File file  = new File(saveFile);
					file.getParentFile().mkdirs();
					fos = new FileOutputStream(file);
					System.out.println("保存文件至："+saveFile);
					request.result(fos);
				}finally{
					if(fos!=null){
						fos.close();
					}
				}
			}else{
				System.out.println("错误信息："+request.resultToString());
			}
			
		}finally{
			if(is!=null){
				is.close();
			}
		}
	}
	
}
