使用itext直接替换PDF中的文本
直接说问题,itext没有直接提供替换PDF中文本的接口(查看资料得到的结论是PDF不支持这种操作),不过存在解决思路:在需要替换的文本上覆盖新的文本。按照这个思路我们需要解决以下几个问题:
- itext怎样增加白色底的覆盖层
- 找到覆盖层的位置(左顶点的位置)和高度与宽带
1、itext怎样增加覆盖层?
- /*
- * This example was written in answer to the question:
- * http://stackoverflow.com/questions/33952183
- */
- package sandbox.stamper;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.pdf.PdfContentByte;
- import com.itextpdf.text.pdf.PdfReader;
- import com.itextpdf.text.pdf.PdfStamper;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /**
- *
- * @author Bruno Lowagie (iText Software)
- */
- public class HighLightByAddingContent {
- public static final String SRC = "resources/pdfs/hello.pdf";
- public static final String DEST = "results/stamper/hello_highlighted.pdf";
- public static void main(String[] args) throws IOException, DocumentException {
- File file = new File(DEST);
- file.getParentFile().mkdirs();
- new HighLightByAddingContent().manipulatePdf(SRC, DEST);
- }
- public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
- PdfReader reader = new PdfReader(src);
- PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
- PdfContentByte canvas = stamper.getUnderContent(1);
- canvas.saveState();
- canvas.setColorFill(BaseColor.YELLOW);
- canvas.rectangle(36, 786, 66, 16);
- canvas.fill();
- canvas.restoreState();
- stamper.close();
- reader.close();
- }
- }
这里可以在任意位置产生一个层,符合我们的“遮盖层”的要求,不过,通过测试发现此段代码存在一个问题点,它无法遮挡住文字,只是添加了一个背景层。为了达到我们的要求,我们只需要修改一处地方:
- PdfContentByte canvas = stamper.getUnderContent(1); //变成 PdfContentByte canvas = stamper.getOverContent(1);
到目前为止,我们的遮盖层已添加,后面我们还需要的就是在新的遮盖层上写上自己的文字,代码如下:
- /**********************************************************************
- * <pre>
- * FILE : HighLightByAddingContent.java
- * CLASS : HighLightByAddingContent
- *
- *
- * FUNCTION : TODO
- *
- *
- *======================================================================
- * CHANGE HISTORY LOG
- *----------------------------------------------------------------------
- * MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
- *----------------------------------------------------------------------
- *
- * DESCRIPTION:
- * </pre>
- ***********************************************************************/
- package com.cx.itext;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.net.URLDecoder;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Font;
- 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 HighLightByAddingContent {
- @SuppressWarnings("deprecation")
- public static final String SRC = URLDecoder.decode(HighLightByAddingContent.class.getResource("ticket.pdf").getFile());
- public static final String DEST = "I://ticket.pdf";
- public static void main(String[] args) throws IOException, DocumentException {
- File file = new File(DEST);
- file.getParentFile().mkdirs();
- new HighLightByAddingContent().manipulatePdf(SRC, DEST);
- }
- public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
- PdfReader reader = new PdfReader(src);
- PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
- PdfContentByte canvas = stamper.getOverContent(1);
- float height=595;
- System.out.println(canvas.getHorizontalScaling());
- float x,y;
- x= 216;
- y = height -49.09F;
- canvas.saveState();
- canvas.setColorFill(BaseColor.WHITE);
- canvas.rectangle(x, y-5, 43, 15);
- canvas.fill();
- canvas.restoreState();
- //开始写入文本
- canvas.beginText();
- //BaseFont bf = BaseFont.createFont(URLDecoder.decode(CutAndPaste.class.getResource("/AdobeSongStd-Light.otf").getFile()), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
- Font font = new Font(bf,10,Font.BOLD);
- //设置字体和大小
- canvas.setFontAndSize(font.getBaseFont(), 10);
- //设置字体的输出位置
- canvas.setTextMatrix(x, y);
- //要输出的text
- canvas.showText("多退少补" );
- //设置字体的输出位置
- canvas.setFontAndSize(font.getBaseFont(), 20);
- canvas.setTextMatrix(x, y-90);
- //要输出的text
- canvas.showText("多退少补" );
- canvas.endText();
- stamper.close();
- reader.close();
- System.out.println("complete");
- }
- }
2、找到覆盖层的位置(左顶点的位置)和高度与宽带
- /**********************************************************************
- * <pre>
- * FILE : Demo.java
- * CLASS : Demo
- *
- * AUTHOR : caoxu-yiyang@qq.com
- *
- * FUNCTION : TODO
- *
- *
- *======================================================================
- * CHANGE HISTORY LOG
- *----------------------------------------------------------------------
- * MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
- *----------------------------------------------------------------------
- * |2016年11月9日|caoxu-yiyang@qq.com| Created |
- * DESCRIPTION:
- * </pre>
- ***********************************************************************/
- package com.cx.itext;
- import java.io.IOException;
- import com.itextpdf.awt.geom.Rectangle2D.Float;
- import com.itextpdf.text.pdf.PdfReader;
- import com.itextpdf.text.pdf.parser.ImageRenderInfo;
- import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
- import com.itextpdf.text.pdf.parser.RenderListener;
- import com.itextpdf.text.pdf.parser.TextRenderInfo;
- public class Demo
- {
- // 定义关键字
- private static String KEY_WORD = "结算区分";
- // 定义返回值
- private static float[] resu = null;
- // 定义返回页码
- private static int i = 0;
- public static void main(String[] args) {
- float[] point = getKeyWords("I://ticket_in.pdf");
- }
- /*
- * 返回关键字所在的坐标和页数 float[0] >> X float[1] >> Y float[2] >> page
- */
- private static float[] getKeyWords(String filePath)
- {
- try
- {
- PdfReader pdfReader = new PdfReader(filePath);
- int pageNum = pdfReader.getNumberOfPages();
- PdfReaderContentParser pdfReaderContentParser = new PdfReaderContentParser(
- pdfReader);
- // 下标从1开始
- for (i = 1; i <= pageNum; i++)
- {
- pdfReaderContentParser.processContent(i, new RenderListener()
- {
- @Override
- public void renderText(TextRenderInfo textRenderInfo)
- {
- String text = textRenderInfo.getText();
- if (null != text && text.contains(KEY_WORD))
- {
- Float boundingRectange = textRenderInfo
- .getBaseline().getBoundingRectange();
- resu = new float[3];
- System.out.println("======="+text);
- System.out.println("h:"+boundingRectange.getHeight());
- System.out.println("w:"+boundingRectange.width);
- System.out.println("centerX:"+boundingRectange.getCenterX());
- System.out.println("centerY:"+boundingRectange.getCenterY());
- System.out.println("x:"+boundingRectange.getX());
- System.out.println("y:"+boundingRectange.getY());
- System.out.println("maxX:"+boundingRectange.getMaxX());
- System.out.println("maxY:"+boundingRectange.getMaxY());
- System.out.println("minX:"+boundingRectange.getMinX());
- System.out.println("minY:"+boundingRectange.getMinY());
- resu[0] = boundingRectange.x;
- resu[1] = boundingRectange.y;
- resu[2] = i;
- }
- }
- @Override
- public void renderImage(ImageRenderInfo arg0)
- {
- }
- @Override
- public void endTextBlock()
- {
- }
- @Override
- public void beginTextBlock()
- {
- }
- });
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- return resu;
- }
- }
结合以上的,我们就可以写一个自动替换PDF文本的类,具体使用如下:
- public static void main(String[] args) throws IOException, DocumentException {
- PdfReplacer textReplacer = new PdfReplacer("I://test.pdf");
- textReplacer.replaceText("陈坤", "小白");
- textReplacer.replaceText("本科", "社会大学");
- textReplacer.replaceText("0755-29493863", "15112345678");
- textReplacer.toPdf("I://ticket_out.pdf");
- }
原始PDF:
补上相关代码(还在完善中),总共4个类
代码中有几个地方要说明下:
1、由于自动计算得到的高度都是0,所有我这边默认的都是12,大家要根据实际情况来设
2、除了可以让代码自己计算位置之外,也可以通过replaceText的重载方法强制指定替换区域。
- /**********************************************************************
- * <pre>
- * FILE : PdfTextReplacer.java
- * CLASS : PdfTextReplacer
- *
- * AUTHOR : caoxu-yiyang@qq.com
- *
- * FUNCTION : TODO
- *
- *
- *======================================================================
- * CHANGE HISTORY LOG
- *----------------------------------------------------------------------
- * MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
- *----------------------------------------------------------------------
- * |2016年11月8日|caoxu-yiyang@qq.com| Created |
- * DESCRIPTION:
- * </pre>
- ***********************************************************************/
- package com.cx.itext;
- import java.io.ByteArrayOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.log.Logger;
- import com.itextpdf.text.log.LoggerFactory;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfContentByte;
- import com.itextpdf.text.pdf.PdfReader;
- import com.itextpdf.text.pdf.PdfStamper;
- /**
- * 替换PDF文件某个区域内的文本
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月8日
- */
- public class PdfReplacer {
- private static final Logger logger = LoggerFactory.getLogger(PdfReplacer.class);
- private int fontSize;
- private Map<String, ReplaceRegion> replaceRegionMap = new HashMap<String, ReplaceRegion>();
- private Map<String, Object> replaceTextMap =new HashMap<String, Object>();
- private ByteArrayOutputStream output;
- private PdfReader reader;
- private PdfStamper stamper;
- private PdfContentByte canvas;
- private Font font;
- public PdfReplacer(byte[] pdfBytes) throws DocumentException, IOException{
- init(pdfBytes);
- }
- public PdfReplacer(String fileName) throws IOException, DocumentException{
- FileInputStream in = null;
- try{
- in =new FileInputStream(fileName);
- byte[] pdfBytes = new byte[in.available()];
- in.read(pdfBytes);
- init(pdfBytes);
- }finally{
- in.close();
- }
- }
- private void init(byte[] pdfBytes) throws DocumentException, IOException{
- logger.info("初始化开始");
- reader = new PdfReader(pdfBytes);
- output = new ByteArrayOutputStream();
- stamper = new PdfStamper(reader, output);
- canvas = stamper.getOverContent(1);
- setFont(10);
- logger.info("初始化成功");
- }
- private void close() throws DocumentException, IOException{
- if(reader != null){
- reader.close();
- }
- if(output != null){
- output.close();
- }
- output=null;
- replaceRegionMap=null;
- replaceTextMap=null;
- }
- public void replaceText(float x, float y, float w,float h, String text){
- ReplaceRegion region = new ReplaceRegion(text); //用文本作为别名
- region.setH(h);
- region.setW(w);
- region.setX(x);
- region.setY(y);
- addReplaceRegion(region);
- this.replaceText(text, text);
- }
- public void replaceText(String name, String text){
- this.replaceTextMap.put(name, text);
- }
- /**
- * 替换文本
- * @throws IOException
- * @throws DocumentException
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- */
- private void process() throws DocumentException, IOException{
- try{
- parseReplaceText();
- canvas.saveState();
- Set<Entry<String, ReplaceRegion>> entrys = replaceRegionMap.entrySet();
- for (Entry<String, ReplaceRegion> entry : entrys) {
- ReplaceRegion value = entry.getValue();
- canvas.setColorFill(BaseColor.RED);
- canvas.rectangle(value.getX(),value.getY(),value.getW(),value.getH());
- }
- canvas.fill();
- canvas.restoreState();
- //开始写入文本
- canvas.beginText();
- for (Entry<String, ReplaceRegion> entry : entrys) {
- ReplaceRegion value = entry.getValue();
- //设置字体
- canvas.setFontAndSize(font.getBaseFont(), getFontSize());
- canvas.setTextMatrix(value.getX(),value.getY()+2/*修正背景与文本的相对位置*/);
- canvas.showText((String) replaceTextMap.get(value.getAliasName()));
- }
- canvas.endText();
- }finally{
- if(stamper != null){
- stamper.close();
- }
- }
- }
- /**
- * 未指定具体的替换位置时,系统自动查找位置
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- */
- private void parseReplaceText() {
- PdfPositionParse parse = new PdfPositionParse(reader);
- Set<Entry<String, Object>> entrys = this.replaceTextMap.entrySet();
- for (Entry<String, Object> entry : entrys) {
- if(this.replaceRegionMap.get(entry.getKey()) == null){
- parse.addFindText(entry.getKey());
- }
- }
- try {
- Map<String, ReplaceRegion> parseResult = parse.parse();
- Set<Entry<String, ReplaceRegion>> parseEntrys = parseResult.entrySet();
- for (Entry<String, ReplaceRegion> entry : parseEntrys) {
- if(entry.getValue() != null){
- this.replaceRegionMap.put(entry.getKey(), entry.getValue());
- }
- }
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- }
- /**
- * 生成新的PDF文件
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- * @param fileName
- * @throws DocumentException
- * @throws IOException
- */
- public void toPdf(String fileName) throws DocumentException, IOException{
- FileOutputStream fileOutputStream = null;
- try{
- process();
- fileOutputStream = new FileOutputStream(fileName);
- fileOutputStream.write(output.toByteArray());
- fileOutputStream.flush();
- }catch(IOException e){
- logger.error(e.getMessage(), e);
- throw e;
- }finally{
- if(fileOutputStream != null){
- fileOutputStream.close();
- }
- close();
- }
- logger.info("文件生成成功");
- }
- /**
- * 将生成的PDF文件转换成二进制数组
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- * @return
- * @throws DocumentException
- * @throws IOException
- */
- public byte[] toBytes() throws DocumentException, IOException{
- try{
- process();
- logger.info("二进制数据生成成功");
- return output.toByteArray();
- }finally{
- close();
- }
- }
- /**
- * 添加替换区域
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- * @param replaceRegion
- */
- public void addReplaceRegion(ReplaceRegion replaceRegion){
- this.replaceRegionMap.put(replaceRegion.getAliasName(), replaceRegion);
- }
- /**
- * 通过别名得到替换区域
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- * @param aliasName
- * @return
- */
- public ReplaceRegion getReplaceRegion(String aliasName){
- return this.replaceRegionMap.get(aliasName);
- }
- public int getFontSize() {
- return fontSize;
- }
- /**
- * 设置字体大小
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- * @param fontSize
- * @throws DocumentException
- * @throws IOException
- */
- public void setFont(int fontSize) throws DocumentException, IOException{
- if(fontSize != this.fontSize){
- this.fontSize = fontSize;
- BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
- font = new Font(bf,this.fontSize,Font.BOLD);
- }
- }
- public void setFont(Font font){
- if(font == null){
- throw new NullPointerException("font is null");
- }
- this.font = font;
- }
- public static void main(String[] args) throws IOException, DocumentException {
- PdfReplacer textReplacer = new PdfReplacer("I://test.pdf");
- textReplacer.replaceText("陈坤", "小白");
- textReplacer.replaceText("本科", "社会大学");
- textReplacer.replaceText("0755-29493863", "15112345678");
- textReplacer.toPdf("I://ticket_out.pdf");
- }
- }
- /**********************************************************************
- * <pre>
- * FILE : ReplaceRegion.java
- * CLASS : ReplaceRegion
- *
- * AUTHOR : caoxu-yiyang@qq.com
- *
- * FUNCTION : TODO
- *
- *
- *======================================================================
- * CHANGE HISTORY LOG
- *----------------------------------------------------------------------
- * MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
- *----------------------------------------------------------------------
- * |2016年11月9日|caoxu-yiyang@qq.com| Created |
- * DESCRIPTION:
- * </pre>
- ***********************************************************************/
- package com.cx.itext;
- /**
- * 需要替换的区域
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- */
- public class ReplaceRegion {
- private String aliasName;
- private Float x;
- private Float y;
- private Float w;
- private Float h;
- public ReplaceRegion(String aliasName){
- this.aliasName = aliasName;
- }
- /**
- * 替换区域的别名
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- * @return
- */
- public String getAliasName() {
- return aliasName;
- }
- public void setAliasName(String aliasName) {
- this.aliasName = aliasName;
- }
- public Float getX() {
- return x;
- }
- public void setX(Float x) {
- this.x = x;
- }
- public Float getY() {
- return y;
- }
- public void setY(Float y) {
- this.y = y;
- }
- public Float getW() {
- return w;
- }
- public void setW(Float w) {
- this.w = w;
- }
- public Float getH() {
- return h;
- }
- public void setH(Float h) {
- this.h = h;
- }
- }
- /**********************************************************************
- * <pre>
- * FILE : PdfPositionParse.java
- * CLASS : PdfPositionParse
- *
- * AUTHOR : caoxu-yiyang@qq.com
- *
- * FUNCTION : TODO
- *
- *
- *======================================================================
- * CHANGE HISTORY LOG
- *----------------------------------------------------------------------
- * MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
- *----------------------------------------------------------------------
- * |2016年11月9日|caoxu-yiyang@qq.com| Created |
- * DESCRIPTION:
- * </pre>
- ***********************************************************************/
- package com.cx.itext;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import com.cx.itext.listener.PositionRenderListener;
- import com.itextpdf.text.pdf.PdfReader;
- import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
- /**
- * 解析PDF中文本的x,y位置
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- */
- public class PdfPositionParse {
- private PdfReader reader;
- private List<String> findText = new ArrayList<String>(); //需要查找的文本
- private PdfReaderContentParser parser;
- public PdfPositionParse(String fileName) throws IOException{
- FileInputStream in = null;
- try{
- in =new FileInputStream(fileName);
- byte[] bytes = new byte[in.available()];
- in.read(bytes);
- init(bytes);
- }finally{
- in.close();
- }
- }
- public PdfPositionParse(byte[] bytes) throws IOException{
- init(bytes);
- }
- private boolean needClose = true;
- /**
- * 传递进来的reader不会在PdfPositionParse结束时关闭
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- * @param reader
- */
- public PdfPositionParse(PdfReader reader){
- this.reader = reader;
- parser = new PdfReaderContentParser(reader);
- needClose = false;
- }
- public void addFindText(String text){
- this.findText.add(text);
- }
- private void init(byte[] bytes) throws IOException {
- reader = new PdfReader(bytes);
- parser = new PdfReaderContentParser(reader);
- }
- /**
- * 解析文本
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- * @throws IOException
- */
- public Map<String, ReplaceRegion> parse() throws IOException{
- try{
- if(this.findText.size() == 0){
- throw new NullPointerException("没有需要查找的文本");
- }
- PositionRenderListener listener = new PositionRenderListener(this.findText);
- parser.processContent(1, listener);
- return listener.getResult();
- }finally{
- if(reader != null && needClose){
- reader.close();
- }
- }
- }
- }
- /**********************************************************************
- * <pre>
- * FILE : PositionRenderListener.java
- * CLASS : PositionRenderListener
- *
- * AUTHOR : caoxu-yiyang@qq.com
- *
- * FUNCTION : TODO
- *
- *
- *======================================================================
- * CHANGE HISTORY LOG
- *----------------------------------------------------------------------
- * MOD. NO.| DATE | NAME | REASON | CHANGE REQ.
- *----------------------------------------------------------------------
- * |2016年11月9日|caoxu-yiyang@qq.com| Created |
- * DESCRIPTION:
- * </pre>
- ***********************************************************************/
- package com.cx.itext.listener;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import com.cx.itext.ReplaceRegion;
- import com.itextpdf.awt.geom.Rectangle2D.Float;
- import com.itextpdf.text.pdf.parser.ImageRenderInfo;
- import com.itextpdf.text.pdf.parser.RenderListener;
- import com.itextpdf.text.pdf.parser.TextRenderInfo;
- /**
- * pdf渲染监听,当找到渲染的文本时,得到文本的坐标x,y,w,h
- * @user : caoxu-yiyang@qq.com
- * @date : 2016年11月9日
- */
- public class PositionRenderListener implements RenderListener{
- private List<String> findText;
- private float defaultH; ///出现无法取到值的情况,默认为12
- private float fixHeight; //可能出现无法完全覆盖的情况,提供修正的参数,默认为2
- public PositionRenderListener(List<String> findText, float defaultH,float fixHeight) {
- this.findText = findText;
- this.defaultH = defaultH;
- this.fixHeight = fixHeight;
- }
- public PositionRenderListener(List<String> findText) {
- this.findText = findText;
- this.defaultH = 12;
- this.fixHeight = 2;
- }
- @Override
- public void beginTextBlock() {
- }
- @Override
- public void endTextBlock() {
- }
- @Override
- public void renderImage(ImageRenderInfo imageInfo) {
- }
- private Map<String, ReplaceRegion> result = new HashMap<String, ReplaceRegion>();
- @Override
- public void renderText(TextRenderInfo textInfo) {
- String text = textInfo.getText();
- for (String keyWord : findText) {
- if (null != text && text.equals(keyWord)){
- Float bound = textInfo.getBaseline().getBoundingRectange();
- ReplaceRegion region = new ReplaceRegion(keyWord);
- region.setH(bound.height == 0 ? defaultH : bound.height);
- region.setW(bound.width);
- region.setX(bound.x);
- region.setY(bound.y-this.fixHeight);
- result.put(keyWord, region);
- }
- }
- }
- public Map<String, ReplaceRegion> getResult() {
- for (String key : findText) { //补充没有找到的数据
- if(this.result.get(key) == null){
- this.result.put(key, null);
- }
- }
- return this.result;
- }
- }
我用到的jar包如下:
大家可以从官网下载,可以构建maven项目省去自己找包的麻烦。如果没有用maven又想下载具体的jar包,可以直接访问maven仓库下载:http://mvnrepository.com/
使用itext直接替换PDF中的文本的更多相关文章
- java itext替换PDF中的文本
itext没有提供直接替换PDF文本的接口,我们可以通过在原有的文本区域覆盖一个遮挡层,再在上面加上文本来实现. 所需jar包: 1.先在PDF需要替换的位置覆盖一个白色遮挡层(颜色可根据PDF文字背 ...
- servlet操作本地文件汇总: 判断文件是否存在;文件重命名;文件复制; 获取文件属性信息,转成Json对象; 获取指定类型的文件; 查找替换.txt中的文本
package servlet; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; ...
- C# 设置或验证 PDF中的文本域格式
概述 PDF中的文本域可以通过设置不同格式,用于显示数字.货币.日期.时间.邮政编码.电话号码和社保号等等.Adobe Acrobat提供了许多固定的JavaScripts用来设置和验证文本域的格式, ...
- Java 替换PDF中的字体
文档中可通过应用不同的字体来呈现不一样的视觉效果,通过字体来实现文档布局.排版等设计需要.应用字体时,可在创建文档时指定字体,也可以用新字体去替换文档中已有的字体.下面,以Java代码展示如何来替换P ...
- Java 读取PDF中的文本和图片
本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取. 使用工具:Free Spire.PDF for Ja ...
- Java 设置PDF中的文本旋转、倾斜
本文介绍通过Java程序在PDF文档中设置文本旋转.倾斜的方法.设置文本倾斜时,通过定义方法TransformText(page);并设置page.getCanvas().skewTransform( ...
- python 之文本搜索与替换文件中的文本
#!/usr/local/env python import os, sys nargs = len(sys.argv) if not 3 <= nargs <= 5: print &qu ...
- Java 在PDF中添加水印——文本/图片水印
水印是一种十分常用的防伪手段,常用于各种文档.资料等.常见的水印,包括文字类型的水印.图片或logo类型的水印.以下Java示例,将分别使用insertTextWatermark(PdfPageBas ...
- java从pdf中提取文本
一(单文件转换):下载pdfbox包,百度搜pdfbox.(fontbox-1.8.16.jar和pdfbox-app-1.8.16.jar) package pdf; import java.io. ...
随机推荐
- Spring(十四):使用FactoryBean配置Bean
FactoryBean简介: 1)Spring中Bean包含两种一种是普通Bean,另外一种是FactoryBean.它们都受IOC容器管理,但是也有不同之处. 2)普通Bean与FactoryBea ...
- eclipse启动错误:java.lang.NoClassDefFoundError: org/eclipse/core/resources/IContainer
转自:http://blog.csdn.net/niu_hao/article/details/9332521 eclipse启动时报错如下:java.lang.NoClassDefFoundErro ...
- mysql function 中使用游标
create function p_t1(tid varchar(20))returns varchar(200)begin declare tmpName varchar(50) default ' ...
- ScrollView嵌套ListView只显示一行解决方案
在ScrollView里边嵌套了个ListView,后边就发现数据源里好多数据,但ListView只是显示1行. 各种debug,打log,数据什么的都没问题,上网百度了下,发现原来是ScrollVi ...
- VMware vCloud Director network ports diagram
see:http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&exter ...
- JAVA设计模式——第 8 章 适配器模式【Adapter Pattern】(转)
好,请安静,后排聊天的同学别吵醒前排睡觉的同学了,大家要相互理解嘛.今天讲适配器模式,这个模式也很简单,你笔记本上的那个拖在外面的黑盒子就是个适配器,一般你在中国能用,在日本也能用,虽然两个国家的的电 ...
- 安装 LUA
为了以后方便,记录一下步骤 1.yum install readline-devel 2.wget http://www.lua.org/ftp/lua-5.1.4.tar.gz 3.cd lua-5 ...
- MongoDB副本集配置系列二:配置MongoDB副本集
接上一篇博客: http://www.cnblogs.com/xiaoit/p/4479066.html 1:首先创建3台虚拟机作为配置环境 IP1:192.168.91.128 IP2:192.16 ...
- XML与JSON使用区别
一:XML XML 文档形成了一种树结构,它从"根部"开始,然后扩展到"枝叶". XML 文档形成一种树结构 XML 文档必须包含根元素.该元素是所有其他元素的 ...
- sqlserver 表中记录生成insert,可以加条件,可以生成建表语句
sqlserver 表中记录生成insert,可以加条件,可以生成建表语句 create PROCEDURE [sp_getinsert] ( ) , --如果非默认架构,可以加上架构名 例如:sch ...