package com.sgcc.test;
im
port java.io.BufferedInputStream;
im
port java.io.BufferedOutputStream;
im
port java.io.ByteArrayOutputStream;
im
port java.io.File;
im
port java.io.FileInputStream;
im
port java.io.IOException;
im
port java.io.InputStream;
im
port java.io.OutputStream;
im
port java.util.ArrayList;
im
port java.util.HashMap;
im
port java.util.List;
im
port java.util.Map;
im
port com.itextpdf.text.BadElementException;
im
port com.itextpdf.text.docu
mentException;
im
port com.itextpdf.text.Element;
im
port com.itextpdf.text.Image;
im
port com.itextpdf.text.pdf.PdfContentByte;
im
port com.itextpdf.text.pdf.PdfReader;
im
port com.itextpdf.text.pdf.PdfStamper;
im
port com.itextpdf.text.pdf.parser.ImageRenderInfo;
im
port com.itextpdf.text.pdf.parser.PdfReaderContentParser;
im
port com.itextpdf.text.pdf.parser.RenderListener;
im
port 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 positio
nString = 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 (docu
mentException | 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);
}
PdfReaderCo
ntentParser pdfReaderCo
ntentParser = new PdfReaderCo
ntentParser(pdfReader);
try {
for (int i = 1; i <= pageNum; i++) {
final int finalI = i;
pdfReaderContentParser.processCo
ntent(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.getba
seline()
.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().co
ntains(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;
}