java pdf 文字_Java给pdf文件添加文字等信息

2019独角兽企业重金招聘Python工程师标准>>>

6936d1565ae689371725cdb5fc51c415.png

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;

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值