パッケージ com.dhcc.zhfc.elesign.util;
org.apache.commons.lang.StringUtils をインポートします。
org.apache.pdfbox.pdmodel.PDDocument をインポートします。
org.apache.pdfbox.pdmodel.PDPageContentStream をインポートします。
org.apache.pdfbox.text.PDFTextStripper をインポートします。
org.apache.pdfbox.text.TextPosition をインポートします。
org.hibernate.annotations.common.util.StringHelper をインポートします。
java.io.* をインポートします。
java.util.ArrayListをインポートします。
java.util.Listをインポートします。
/**
* @ClassName PdfBoxKeyWordPosition
* @Description TODO
* @Author 86173
* @Date 2020/5/11 15:14
* @Version 1.0
*/
public class PdfBoxKeyWordPosition extends PDFTextStripper { // キーワード文字配列 private char[] key; / / PDF ファイルパス private String pdfPath; private byte[] fileBytes; // 座標情報収集 private List<float[]> list = new ArrayList<float[]>(); // 現在のページ情報収集 private List<float[ ] > pagelist = new ArrayList<float[]>(); // パラメータを持つコンストラクター public PdfBoxKeyWordPosition(String keyWords, String pdfPath,byte[] bin) throws IOException { super();
super.setSortByPosition(true);
this.pdfPath = pdfPath;
this.fileBytes= ビン;
char[] key = new char[keyWords.length()];
for (int i = 0; i < keyWords.length(); i++) { key[i] = keyWords.charAt(i); this.key = キー; } public char[] getKey() { キーを返します。 public void setKey(char[] key) { this.key = key; public String getPdfPath() { pdfPath を返します。 public void setPdfPath(String pdfPath) { this.pdfPath = pdfPath; }
// 获取坐标情報
public List<float[]> getCooperative() throws IOException { try { if(!StringHelper.isEmpty(pdfPath)){ document = PDDocument.load(new File(pdfPath)); if(document==null&&fileBytes!=null){ document = PDDocument.load(fileBytes) ; int ページ = document.getNumberOfPages(); for (int i = 1; i <= ページ; i++) { pagelist.clear(); super.setSortByPosition(true); super.setStartPage(i); super.setEndPage(i);
ライターダミー = new OutputStreamWriter(new ByteArrayOutputStream());
super.writeText(ドキュメント、ダミー);
for (float[] li : pagelist) { li[2] = i; list.addAll (ページリスト); リストを返します 。 } catch (例外 e) { e.printStackTrace(); 最後に { if (document != null) { document.close(); リストを返し ます 。 }
// 获取坐标信息
@Override
protected void writeString(String string, List<TextPosition> textPositions) throws IOException { for (int i = 0; i < textPositions.size(); i++) { String fonts = textPositions.get(i) .getFont().getName(); 文字列 str = textPositions.get(i).getUnicode(); if (str.equals(key[0] + "")) { int count = 0; for (int j = 0; j < key.length-1; j++) { String s = ""; { s = textPositions.get(i + j) を試してください。getUnicode(); } catch (例外 e) { s = "";
}
if (s.equals(key[j] + "")) { count++; } } if (count == key.length-1) { float[] idx = new float[3]; // いくつかの調整が必要ですシールでフォントをカバーする // フォントの長さがここで X 座標に追加されます。または、直接 idx[0] = textPositions.get(i).getX() idx[0] = textPositions.get(i) ).getX ()+textPositions.get(i).getFontSize(); //ここで Y 座標から差し引いたフォントの長さを直接指定することもできます idx[1] = textPositions.get(i).getPageHeight()- textPositions.get( i).getY()
idx[1] = textPositions.get(i).getHeight()-textPositions.get(i).getY()-4*textPositions.get(i).getFontSize();
System.out.println("x=" + idx[0] + ",y=" + idx[1]);
ページリスト.add(idx);
戻る;
}
} }
}
public static void main(String[] args) throws IOException { String pdfPath = "C:\\Users\\pangq\\Desktop\\555.pdf"; ファイル file = 新しいファイル(pdfPath); //PDDocument doc = PDDocument.load(file); String keyWords = "纪海祥"; //PDImageXObject pdImage = PDImageXObject.createFromFile("C:/Programs/test/sign.png", doc); byte[] バイト = File2byte(file); PdfBoxKeyWordPosition pdf = new PdfBoxKeyWordPosition(keyWords, "",bytes); PDPageContentStream contentStream = null; List<float[]> list = pdf.getCooperative(); List<Integer> 変換結果 = 変換(リスト); 文字列 a = Convert2String(list);
// 多页pdfの処理*/
for (float[] fs : list) { float x = fs[0]; float y = fs[1]; } //doc.close(); } public static byte[] File2byte(File tradeFile){ byte[] バッファ = null; FileInputStream fis =null; ByteArrayOutputStream bos =null; { fis = new FileInputStream(tradeFile); を試してください。 bos = 新しい ByteArrayOutputStream(); byte[] b = 新しいバイト[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n);
fis.close
();
bos.close();
バッファ = bos.toByteArray();
}catch (FileNotFoundException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); }最後に { if(fis !=null){ 試してください { fis.close(); }catch (IOException io){ io.printStackTrace(); if (bos ! = null){ try { bos.close();
}catch (IOException io){ io.printStackTrace(); バッファを 返し ます 。 }
public static List<Integer> Convert(List<float[]> list){ List<Integer> res = new ArrayList<Integer>(); if(list!=null&&list.size()>0) { for (float[] fs : list) { int page = (int) fs[2]; if(!res.contains(page)){ res.add(page); 応答を 返し ます 。 public static String Convert2String(List<float[]> list){ List<Integer> res = Convert(list) ; String str = StringUtils.join(res.iterator(),","); 文字列を返します。 }
/**
* pdf のページ番号を取得
* @param bystes
* @return
*/
public static int getPdfNubers(byte[] bystes){
int ページ = 0 ;
ByteArrayInputStream in = new ByteArrayInputStream (bystes) ;
PDDocument pdfReader = null;
{ pdfReader = PDDocument.load(in);を試してください。 ページ = pdfReader.getNumberOfPages(); } catch(IOException e) { ページを返します。 } }
ページを返す。
}
}