推广 热搜: 行业  机械  设备    教师  系统    经纪  参数  蒸汽 

Java读取PDF查找指定文字位置,并添加悬浮图片(文档签字/盖章)

   日期:2024-11-12     移动:http://mip.riyuangf.com/quote/861.html
package com.sgcc.test;

Java读取PDF查找指定文字位置,并添加悬浮图片(文档签字/盖章)

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.itextpdf.text.BadElementException; import com.itextpdf.text.documentException; import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; 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 SearchStampPosition { // public static void main(String[] args) {resultList public static List searchPosition(String pdfUrl,String qzwz){ FileInputStream fin = null; BufferedInputStream bin = null; ByteArrayOutputStream baos = null; BufferedOutputStream bout = null; List resultList = new ArrayList(); Map map = null; try { // 建立读取文件的文件输出流 // fin = new FileInputStream("D:/home/PDFfiles/20191204_TYZCCK-CKD_825c8725-5f95-443a-9984-901ec91b6d12.pdf"); fin = new FileInputStream(pdfUrl); // 在文件输出流上安装节点流(更大效率读取) bin = new BufferedInputStream(fin); // 创建一个新的 byte 数组输出流,它具有指定大小的缓冲区容量 baos = new ByteArrayOutputStream(); // 创建一个新的缓冲输出流,以将数据写入指定的底层输出流 // bout = new BufferedOutputStream(baos); byte[] buffer = new byte[1024]; int len = bin.read(buffer); while (len != -1) { baos.write(buffer, 0, len); len = bin.read(buffer); } // 刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题 baos.flush(); String positionString = qzwz; resultList = printSign(baos, positionString); // map = printSign(baos, positionString); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { // System.exit(0); } return resultList; } static String getPDFBinary(File file) { return null; } public static List printSign(ByteArrayOutputStream outputStream,String positionString){ List<String> keyWords = new ArrayList<String>(); keyWords.add(positionString);//支持多关键字,默认选择第一个找到的关键字 PdfReader pdfReader; PdfStamper pdfStamper = null; Map map = null; List resultList = new ArrayList(); try { pdfReader = new PdfReader((outputStream).toByteArray()); pdfStamper = new PdfStamper(pdfReader, outputStream); List<List<float[]>> arrayLists = null; arrayLists = findKeywords(keyWords, pdfReader);//查找关键字所在坐标 //一个坐标也没找到,就返回 // if (ObjectUtil.isEmpty(arrayLists) && arrayLists.size() > 1) { if (arrayLists == null || arrayLists.size() == 0) { // resultList.add("failed"); // map.put("result", "位置出错!"); // resultList.add(map); return resultList; }else{ for(int i = 0;i<arrayLists.get(0).size();i++){ map = new HashMap(); float width = (float)arrayLists.get(0).get(i)[0]; float height = (float)arrayLists.get(0).get(i)[1]; int page = (int)arrayLists.get(0).get(i)[2]; map.put("width", width); map.put("height", height); map.put("page", page); resultList.add(map); } } } catch (Exception e) { e.printStackTrace(); }finally { //此处一定要关闭流,否则可能会出现乱码 if(pdfStamper != null){ try { pdfStamper.close(); } catch (documentException | IOException e) { e.printStackTrace(); } } } return resultList; } private static List<List<float[]>> findKeywords(final List<String> keyWords, PdfReader pdfReader) { if (keyWords == null || keyWords.size() == 0) { return null; } int pageNum = pdfReader.getNumberOfPages(); final List<List<float[]>> arrayLists = new ArrayList<List<float[]>>(keyWords.size()); for (int k = 0; k < keyWords.size(); k++) { List<float[]> positions = new ArrayList<float[]>(); arrayLists.add(positions); } PdfReaderContentParser pdfReaderContentParser = new PdfReaderContentParser(pdfReader); try { for (int i = 1; i <= pageNum; i++) { final int finalI = i; pdfReaderContentParser.processContent(i, new RenderListener() { private StringBuilder pdfsb = new StringBuilder(); private float yy = -1f; @Override public void renderText(TextRenderInfo textRenderInfo) { String text = textRenderInfo.getText(); com.itextpdf.awt.geom.Rectangle2D.Float boundingRectange = textRenderInfo.getbaseline() .getBoundingRectange(); if (yy == -1f) { yy = boundingRectange.y; } if (yy != boundingRectange.y) { yy = boundingRectange.y; pdfsb.setLength(0); } pdfsb.append(text); if (pdfsb.length() > 0) { for (int j = 0; j < keyWords.size(); j++) { List<String> key_words = parseList(keyWords.get(j), ","); // 假如配置了多个关键字,找到一个就跑 for (final String key_word : key_words) { // if (arrayLists.get(j) != null && arrayLists.get(j).size() > 0) { // break; // } if (pdfsb.length() > 0 && pdfsb.toString().contains(key_word)) { float[] resu = new float[3]; resu[0] = boundingRectange.x + boundingRectange.width * (key_word.length() - 1); resu[1] = boundingRectange.y; resu[2] = finalI; arrayLists.get(j).add(resu); System.out.println(resu.toString()); pdfsb.setLength(0); // break; } } } } } @Override public void renderImage(ImageRenderInfo arg0) { // renderImage } @Override public void endTextBlock() { // endTextBlock } @Override public void beginTextBlock() { // beginTextBlock } }); } } catch (Exception e) { e.printStackTrace(); } return arrayLists; } public static List<String> parseList(String source, String regex) { if (source == null || "".equals(source)) { return null; } List<String> strList = new ArrayList<String>(); if (regex == null || "".equals(regex)) { strList.add(source); } else { String[] strArr = source.split(regex); for (String str : strArr) { if (str != null || !"".equals(str)) { strList.add(str); } } } return strList; } private static Image getImgByInputstream(InputStream is) { if (is == null) { return null; } ByteArrayOutputStream output = new ByteArrayOutputStream(); Image img = null; try { readInputStream(is, output); try { img = Image.getInstance(output.toByteArray()); } catch (BadElementException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } return img; }
本文地址:http://fmiwue.riyuangf.com/quote/861.html    迅易网 http://fmiwue.riyuangf.com/ , 查看更多

特别提示:本信息由相关企业自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。


0相关评论
相关行业动态
推荐行业动态
点击排行
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  鄂ICP备2020018471号