介绍
这篇文章主要介绍java如何实现查找PDF关键字所在页码及其坐标,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1,因为最近有这方面的需求,用过之后记录一下。
2,此功能跟PDF中Ctrl + F性质一样,如果PDF中为图片形式的不支持定位到关键字。
import com.itextpdf.awt.geom.Rectangle2D.Float; import com.itextpdf.text.pdf.PdfDictionary; import com.itextpdf.text.pdf.PdfName; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.parser。*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import 并不知道;/* * ,*消失的太阳 ,*/public class  MyTest { ,public static  void main (String [], args), throws IOException {//1 .给才能定文件 File 才能;pdfFile =, new 文件(“D://test.pdf");//2。才能定义一个字节数组,长度为文件的长度 ,,byte [], pdfData =, new 字节((int), pdfFile.length ());//3. io流才能读取文件内容到字节数组 FileInputStream 才能;inputStream =,空; try {才能 ,,inputStream =, new FileInputStream (pdfFile); ,,read (pdfData); ,,},catch (IOException e), { ,,throw e; ,,},{finally ,,if (inputStream !=, null), { ,,,try { ,,,,inputStream.close (); ,,,},catch (IOException e), { ,,,} ,,} ,,}//4 .指才能定关键字 String 才能;keyword =,“消失的太阳:“;//5 .调才能用方法,给定关键字和文件 List<才能;浮动[]祝辞,positions =, findKeywordPostions (pdfData,关键字);//6。才能返回值类型是,List<浮动[]祝辞,每个列表元素代表一个匹配的位置,分别为,浮动[0]所在页码,浮动[1]所在x轴,浮动[2]所在y轴 System.out.println才能(“总:“,+,positions.size ()); if 才能;(positions !=, null ,,, positions.size(),祝辞,0),{ ,,for (浮动[],position :,位置),{ ,,,System.out.print (“pageNum:,“, +, (int),位置[0]); ,,,System.out.print (“\ tx:,“, +,位置[1]); ,,,System.out.println (“\ ty:,“, +,位置[2]); ,,} ,,} ,}/* * *才能findKeywordPostions *,才能@param pdfData ,通过IO流,PDF文件转化的字节数组 *,才能@param keyword ,关键字 *,才能@return Listjava如何实现查找PDF关键字所在页码及其坐标