Last active
August 4, 2017 02:42
-
-
Save JunqiangYang/cd82d11d5043a354f0cbb76cd80d93b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.OutputStream; | |
import java.util.Map; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import com.itextpdf.text.Document; | |
import com.itextpdf.text.Image; | |
import com.itextpdf.text.PageSize; | |
import com.itextpdf.text.pdf.BaseFont; | |
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 com.itextpdf.text.pdf.PdfWriter; | |
import com.itextpdf.tool.xml.XMLWorkerHelper; | |
import com.nyonline.util.AppConfig; | |
import com.nyonline.util.BizLogger; | |
/** | |
* 需要提供水印logo,放在app.config.contract_logo位置 | |
* @author jake | |
* | |
*/ | |
public class HtmlToPDF { | |
private static String pdf_template = ""; | |
static{ | |
try{ | |
FileInputStream fis = new FileInputStream(HtmlToPDF.class.getResource("/pdf_template.html").getPath()); | |
String xml = ""; | |
byte[] b = new byte[1024*1024]; | |
while (fis.read(b,0,b.length)!=-1) { | |
xml += new String(b); | |
} | |
pdf_template = xml; | |
fis.close(); | |
}catch(Throwable t){ | |
BizLogger.error(t, "HtmlToPDF.load PDF_template"); | |
} | |
} | |
public static void generatePDF(HttpServletRequest req, HttpServletResponse res, Map<String,Object> dataMap) { | |
try { | |
Document document = new Document(PageSize.LETTER); | |
String pdfFile = getFileOrPath("contract_filepath", "d:\\")+"nyonline_temp"+System.currentTimeMillis()+".pdf"; | |
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile)); | |
document.open(); | |
document.addAuthor("xiaohuolicai"); | |
document.addCreator("www.nyonline.cn"); | |
document.addSubject("小诺理财"); | |
document.addCreationDate(); | |
document.addTitle("小诺理财"); | |
// pdf_template = formatPdf(pdf_template,"136***0945",56789,"2015 年 10 月 22 日"); | |
XMLWorkerHelper worker = XMLWorkerHelper.getInstance(); | |
worker.parseXHtml(pdfWriter, document, new ByteArrayInputStream(pdf_template.getBytes()), null, new AsianFontProvider()); | |
// String str = getHtmlTemplate(); | |
// worker.parseXHtml(pdfWriter, document, new ByteArrayInputStream(str.getBytes()), null, new AsianFontProvider()); | |
document.close(); | |
waterMark(pdfFile,HtmlToPDF.class.getResource("/logo.jpg").getPath(), res.getOutputStream(), "小诺理财"); // 添加水印 | |
} catch (Exception e) { | |
BizLogger.error(e, "HtmlToPDF.generatePDF"); | |
} | |
} | |
private static String getFileOrPath(String configName,String defaultPath){ | |
String p = AppConfig.get("contract_logo", defaultPath); | |
if(System.getProperties().getProperty("os.name").indexOf("Windows")>-1){ | |
return defaultPath; | |
}else{ | |
return p; | |
} | |
} | |
/** 水印 */ | |
public static void waterMark(String inputpdfFile, String imageFile, | |
OutputStream os, String waterMarkName) { | |
try { | |
PdfReader reader = new PdfReader(inputpdfFile); | |
PdfStamper stamper = new PdfStamper(reader, os); | |
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true); // TODO 需要查下为什么这么设置 | |
int total = reader.getNumberOfPages() + 1; | |
Image image = Image.getInstance(imageFile); | |
// 图片位置 | |
image.setAbsolutePosition(10, 480); | |
image.setRotation(-20);//旋转 弧度 | |
image.setRotationDegrees(-45);//旋转 角度 | |
image.scalePercent(50);//依照比例缩放 | |
PdfContentByte under; | |
PdfGState gs = new PdfGState(); | |
gs.setFillOpacity(0.2f);// 设置透明度为0.1 | |
int j = waterMarkName.length(); | |
char c = 0; | |
int rise = 0; | |
for (int i = 1; i < total; i++) { | |
rise = 400; | |
under = stamper.getUnderContent(i); | |
under.setGState(gs); | |
under.beginText(); | |
under.setFontAndSize(base, 24); | |
if (j >= 15) { | |
under.setTextMatrix(200, 120); | |
for (int k = 0; k < j; k++) { | |
under.setTextRise(rise); | |
c = waterMarkName.charAt(k); | |
under.showText(c + ""); | |
} | |
} else { | |
under.setTextMatrix(240, 100); | |
for (int k = 0; k < j; k++) { | |
under.setTextRise(rise); | |
c = waterMarkName.charAt(k); | |
under.showText(c + ""); | |
rise -= 18; | |
} | |
} | |
// 添加水印文字 | |
under.endText(); | |
// 添加水印图片 | |
under.addImage(image); | |
image.setAbsolutePosition(100, 480); | |
under.addImage(image); | |
image.setAbsolutePosition(200, 480); | |
under.addImage(image); | |
image.setAbsolutePosition(300, 480); | |
under.addImage(image); | |
// 画个圈 | |
// under.ellipse(250, 450, 350, 550); | |
// under.setLineWidth(1f); | |
// under.stroke(); | |
} | |
stamper.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@SuppressWarnings("unused") | |
private static String getHtmlTemplate(){ | |
String str = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" | |
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">" | |
+ "<head>" | |
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" | |
+ "<title>HTML 2 PDF</title>" | |
+ "<style type=\"text/css\">" | |
+ "<!--" | |
+ "body {" | |
+ " margin: 20px;" | |
+ "}" | |
+ "-->" | |
+ "</style>" | |
+ "</head>" | |
+ "<body>" | |
+ "<div style=\"width:90%; height:160px;padding-left:30px;\">多情浪漫的人,其实内心的情感是非常脆弱的,感情的末梢,有那么一点儿敏感,还有那么一点儿想入非非。所以和浪漫多情的人在一起,言语一定要斯文,说话不能像火炮,态度一定要温柔、语气也要婉转。遇到对方的一个眼神,也许有些人并没发现什么端倪,但是放在懂得浪漫的人身上,就会体会出万种滋味,百般柔情来。" | |
+ "</div>" | |
+ "<hr/>" | |
+ "<table cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%; border:1px;\">" | |
+ " <tr>" | |
+ " <td style=\"width:30%\">table中的中文显示及换行" | |
+ " </td>" | |
+ " <td>多情浪漫的人,其实内心的情感是非常脆弱的,感情的末梢,有那么一点儿敏感,还有那么一点儿想入非非。所以和浪漫多情的人在一起,言语一定要斯文,说话不能像火炮,态度一定要温柔、语气也要婉转。遇到对方的一个眼神,也许有些人并没发现什么端倪,但是放在懂得浪漫的人身上,就会体会出万种滋味,百般柔情来。" | |
+ " </td>" | |
+ " </tr>" | |
+ " <tr>" | |
+ " <td style=\"width:30%\">table中的中文显示及换行" | |
+ " </td>" | |
+ " <td>多情浪漫的人,其实内心的情感是非常脆弱的,感情的末梢,有那么一点儿敏感,还有那么一点儿想入非非。所以和浪漫多情的人在一起,言语一定要斯文,说话不能像火炮,态度一定要温柔、语气也要婉转。遇到对方的一个眼神,也许有些人并没发现什么端倪,但是放在懂得浪漫的人身上,就会体会出万种滋味,百般柔情来。" | |
+ " </td>" | |
+ " </tr>" | |
+ " <tr>" | |
+ " <td colspan=\"2\">" | |
+ " <img src=\"0.jpg\" />" | |
+ " </td>" | |
+ " </tr>" | |
+ "</table>" + "</body>" + "</html>"; | |
return str; | |
} | |
public static void main(String[] a){ | |
waterMark("d://testpdf.pdf", "d://c.jpg", "d://f.pdf", "小诺理财", 0); | |
} | |
public static void waterMark(String inputFile, String imageFile, | |
String outputFile, String waterMarkName, int permission) { | |
try { | |
PdfReader reader = new PdfReader(inputFile); | |
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( | |
outputFile)); | |
BaseFont base = BaseFont.createFont( | |
"C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// 使用系统字体 | |
int total = reader.getNumberOfPages() + 1; | |
Image image = Image.getInstance(imageFile); | |
// 图片位置 | |
image.setAbsolutePosition(400, 480); | |
PdfContentByte under; | |
int j = waterMarkName.length(); | |
char c = 0; | |
int rise = 0; | |
for (int i = 1; i < total; i++) { | |
rise = 400; | |
under = stamper.getUnderContent(i); | |
under.beginText(); | |
under.setFontAndSize(base, 30); | |
if (j >= 15) { | |
under.setTextMatrix(200, 120); | |
for (int k = 0; k < j; k++) { | |
under.setTextRise(rise); | |
c = waterMarkName.charAt(k); | |
under.showText(c + ""); | |
} | |
} else { | |
under.setTextMatrix(240, 100); | |
for (int k = 0; k < j; k++) { | |
under.setTextRise(rise); | |
c = waterMarkName.charAt(k); | |
under.showText(c + ""); | |
rise -= 18; | |
} | |
} | |
// 添加水印文字 | |
under.endText(); | |
// 添加水印图片 | |
under.addImage(image); | |
// 画个圈 | |
under.ellipse(250, 450, 350, 550); | |
under.setLineWidth(1f); | |
under.stroke(); | |
} | |
stamper.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 数字转换为汉语中人民币的大写<br> | |
* jake | |
*/ | |
public class MoneyUtil { | |
/** 汉语中数字大写 */ | |
private static final String[] CN_UPPER_NUMBER = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; | |
/** 汉语中货币单位大写,这样的设计类似于占位符 */ | |
private static final String[] CN_UPPER_MONETRAY_UNIT = {"分","角","元","拾","佰","仟","万","拾","佰","仟","亿", | |
"拾","佰", "仟", "兆", "拾","佰", "仟" }; | |
/** 特殊字符:整 */ | |
private static final String CN_FULL = ""; | |
/** 特殊字符:零元整 */ | |
private static final String CN_ZEOR_FULL = "零元" + CN_FULL; | |
/** | |
* 把输入的金额转换为汉语中人民币的大写 | |
* @param numberOfMoney 输入的金额 | |
* @return 对应的汉语大写 | |
*/ | |
public static String formatMoneyCN(long amount) { | |
StringBuffer sb = new StringBuffer(); | |
// 零元整的情况 | |
if (amount == 0) { | |
return CN_ZEOR_FULL; | |
} | |
// 得到小数点后两位值 | |
long scale = amount % 100; | |
int numUnit = 0; | |
int numIndex = 0; | |
boolean getZero = false; | |
// 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11 | |
if (!(scale > 0)) { | |
numIndex = 2; | |
amount = amount / 100; | |
getZero = true; | |
} | |
if ((scale > 0) && (!(scale % 10 > 0))) { | |
numIndex = 1; | |
amount = amount / 10; | |
getZero = true; | |
} | |
int zeroSize = 0; | |
while (true) { | |
if (amount <= 0) { | |
break; | |
} | |
// 每次获取到最后一个数 | |
numUnit = (int) (amount % 10); | |
if (numUnit > 0) { | |
if ((numIndex == 9) && (zeroSize >= 3)) { | |
sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]); | |
} | |
if ((numIndex == 13) && (zeroSize >= 3)) { | |
sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]); | |
} | |
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]); | |
sb.insert(0, CN_UPPER_NUMBER[numUnit]); | |
getZero = false; | |
zeroSize = 0; | |
} else { | |
++zeroSize; | |
if (!(getZero)) { | |
sb.insert(0, CN_UPPER_NUMBER[numUnit]); | |
} | |
if (numIndex == 2) { | |
if (amount > 0) { | |
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]); | |
} | |
} else if (((numIndex - 2) % 4 == 0) && (amount % 1000 > 0)) { | |
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]); | |
} | |
getZero = true; | |
} | |
// 让number每次都去掉最后一个数 | |
amount = amount / 10; | |
++numIndex; | |
} | |
// 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整 | |
if (!(scale > 0)) { | |
sb.append(CN_FULL); | |
} | |
return sb.toString(); | |
} | |
public static void main(String[] args) { | |
String s = MoneyUtil.formatMoneyCN(6980203004L); | |
System.out.println("你输入的金额为:【"+ s +"】 #--# [" +s.toString()+"]"); | |
String s1 = MoneyUtil.formatMoneyCN(52120411); | |
System.out.println("你输入的金额为:【"+ s1 +"】 #--# [" +s1.toString()+"]"); | |
} | |
// 获取各位置数值 | |
public static String getBitInt(String amount,int bit,int length){ | |
return amount.substring(length-bit, length-bit+1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment