2019独角兽企业重金招聘Python工程师标准>>>
maven依赖
4.0.0
com.ttxit
artifact
1.0.0
war
javax.servlet
javax.servlet-api
3.1.0
org.jodd
jodd-madvoc
3.7
org.jodd
jodd-http
3.7
org.jodd
jodd-core
3.7
mysql
mysql-connector-java
5.1.38
org.mybatis
mybatis
3.3.1
com.google.code.gson
gson
2.6.2
com.itextpdf
itextpdf
5.5.9
com.itextpdf
itext-asian
5.2.0
log4j
log4j
1.2.17
org.mortbay.jetty
maven-jetty-plugin
6.1.10
UTF-8
10
foo
9999
start-jetty
pre-integration-test
run
0
true
stop-jetty
post-integration-test
stop
org.apache.maven.plugins
maven-war-plugin
2.0.2
pdfutils源码
package com.ttxit.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.log4j.Logger;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class PdfUtils {
private static final Logger logger = Logger.getLogger(PdfUtils.class);
static String PDF = "/Users/shicheng/Desktop/2_d.pdf";
public static final String NAME = "STSong-Light";
public static final String ENCODE = "UniGB-UCS2-H";
public static final int SIZE = 12;
/**
* 生成添加文字数据流
*
* @param text
* 输入信息
* @param width
* 宽度
* @param height
* 高度
* @return 数据流
*/
public static byte[] addText(String text, int width, int height) {
ByteArrayOutputStream byteArrayOutputStream = null;
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
PdfContentByte pdfContentByte = null;
try {
BaseFont baseFont = BaseFont.createFont(NAME, ENCODE, BaseFont.NOT_EMBEDDED);
InputStream inputStream = new FileInputStream(new File(PDF)); // 读取默认pdf文件
pdfReader = new PdfReader(inputStream); // 加载文件到pdf引擎
byteArrayOutputStream = new ByteArrayOutputStream();
pdfStamper = new PdfStamper(pdfReader, byteArrayOutputStream); // 加载模板
pdfContentByte = pdfStamper.getOverContent(1); // 获取顶部
pdfContentByte.beginText(); // 插入文字信息
pdfContentByte.setFontAndSize(baseFont, SIZE);
BaseColor baseColor = new BaseColor(0, 0, 0);
pdfContentByte.setColorFill(baseColor);
pdfContentByte.setTextMatrix(width, height); // 设置文字在页面中的坐标
pdfContentByte.showText(text);
pdfContentByte.endText();
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.error("addText(String, int, int) - Exception e=" + e); //$NON-NLS-1$
}
} finally {
try {
pdfStamper.close();
pdfReader.close();
byteArrayOutputStream.close();
} catch (Exception _e) {
if (logger.isDebugEnabled()) {
logger.error("addText(String, int, int) - Exception e=" + _e); //$NON-NLS-1$
}
}
}
return byteArrayOutputStream.toByteArray();
}
}
action源码
package com.ttxit.action;
import org.apache.log4j.Logger;
import com.ttxit.util.PdfUtils;
import jodd.madvoc.meta.Action;
import jodd.madvoc.meta.In;
import jodd.madvoc.meta.MadvocAction;
import jodd.madvoc.meta.Out;
import jodd.madvoc.result.RawData;
import jodd.util.MimeTypes;
@MadvocAction
public class ImageAction {
private static final Logger logger = Logger.getLogger(ImageAction.class);
@Out
String json = "hello";
@In
int width;
@In
int height;
@Action(value = "/image/brokenhearted_card", method = Action.GET)
public RawData hello() {
if (logger.isDebugEnabled()) {
logger.debug("hello() - start"); //$NON-NLS-1$
}
RawData returnRawData = new RawData(PdfUtils.addText("AAA", width, height), MimeTypes.lookupMimeType("pdf"));
if (logger.isDebugEnabled()) {
logger.debug("hello() - end"); //$NON-NLS-1$
}
return returnRawData;
}
}