以下是我自己封装的工具类,需要的小伙伴可以直接复制使用,有用的话,记得点赞哦
自己配置的xpdf完整版
public class XpdfByWinUtils {
private static Logger log = LoggerFactory.getLogger(XpdfByWinUtils.class);
/**
* xpdf文件地址
*/
private static String PATH_TO_XPDF = "E:\\xpdf\\xpdf";
private static String PATH_TO_XPDF_PNG = PATH_TO_XPDF + "\\pdftopng.exe";
private static String PATH_TO_XPDF_TEXT = PATH_TO_XPDF + "\\pdftotext.exe";
/**
* pdf文件转储png图片,默认转储所有
* @param fileUrl pdf文件路径
* @param imagesUrl 转储后图片储出路径
*/
public static void pdf2Png(String fileUrl, String imagesUrl){
String[] shell = {PATH_TO_XPDF_PNG, fileUrl, imagesUrl};
cmdShell(shell);
}
/**
* pdf文件转储png图片
* @param fileUrl pdf文件路径
* @param startPage pdf起始页
* @param endPage pdf结束页
* @param imagesUrl 转储后图片储出路径
*/
public static void pdf2Png(String fileUrl, String startPage, String endPage, String imagesUrl){
String[] shell = {PATH_TO_XPDF_PNG, "-f", startPage, "-l", endPage, fileUrl, imagesUrl};
cmdShell(shell);
}
/**
* pdf文件转储text文件
* @param fileUrl pdf文件路径
* @param textUrl text文件路径
*/
public static void pdf2Text(String fileUrl, String textUrl){
String[] shell = {PATH_TO_XPDF_TEXT, fileUrl, textUrl};
cmdShell(shell);
}
/**
* 使用java调用控制台执行命令
* @param shell
*/
private static void cmdShell(String[] shell){
BufferedReader br = null;
try {
//调用控制台执行shell
Process p = Runtime.getRuntime().exec(shell);
//获取执行后出现的错误;getInputStream是获取执行后的结果
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while (null != br.readLine()) {
log.info(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 测试类
* @param args
*/
public static void main(String[] args){
String fileName="E:\\xpdf\\1.pdf";
XpdfByWinUtils.pdf2Text(fileName,"E:\\xpdf\\1.text");
}
}