Spring Boot 提取pdf中的文字
提取pdf中的文字,由于字体不同,可能会提取出来乱码。(友情提示:建议先pdf文件转成图片,然后调用百度api提取文字,准确率高。跳转链接:https://blog.csdn.net/weixin_45652692/article/details/118190220)
pom.xml
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.20</version>
</dependency>
<!-- 提取pdf中的文字 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>
<!--获取pdf文件的总页数-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.8.11</version>
</dependency>
PDFToWordUtils.java
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import jdk.nashorn.internal.ir.IfNode;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @description: 提取PDF中的文字
* @author: LCHYUE
* @time: 2021/6/15 9:29
*/
@Component
public class PDFToWordUtils {
/**
* @Description: 提取pdf中的文字 第一种方法
* @Param: fileUrlList:地址
* @Param: pages:页码
* @return: content:提取的文字
* @Author: LCHYUE
* @Date: 2021/6/15
*/
public String PDFToWord(String fileUrlList) throws IOException {
//linux---start---
fileUrlList=fileUrlList.replaceAll("\\\\", File.separator);
//linux--end----
Integer pages = PDFToPage(fileUrlList);
String fileName = fileUrlList;//源文件的位置
PdfReader reader = null;//PDF读取器
reader = new PdfReader(fileName);
String content = "";
for (int i = 1; i <= pages; i++) {
content += PdfTextExtractor.getTextFromPage(reader, i); // 读取PDF中第i页(用哪一页就写几)的文档内容,并转成String
}
content = content.replace("\n", "");
content = content.replace("\\\\r", "");
content = content.replace("\\\\t", "");
System.out.println(content);//控制台打印PDF第一页的内容
return content;
}
/**
* @Description: 提取pdf中的页码
* @Param: fileUrlList:地址
* @return: pages:页码数
* @Author: LCHYUE
* @Date: 2021/6/15
*/
public int PDFToPage(String fileUrlList) {
//linux---start---
fileUrlList=fileUrlList.replaceAll("\\\\", File.separator);
//linux--end----
File file = new File(fileUrlList);
PdfReader pdfReader = null;
try {
pdfReader = new PdfReader(new FileInputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
int pages = pdfReader.getNumberOfPages();
System.out.println("pdf文件的总页数为:" + pages);
return pages;
}
/**
* @Description: 提取pdf中的文字 第二种方法
* @Param: pdfPath:地址
* @return: content:提取的文字
* @Author: LCHYUE
* @Date: 2021/6/15
*/
public static String getTextFromPdf(String pdfPath) throws Exception {
// 是否排序
boolean sort = false;
// 开始提取页数
int startPage = 1;
// 结束提取页数
int endPage = Integer.MAX_VALUE;
String content = null;
InputStream input = null;
//linux---start---
pdfPath=pdfPath.replaceAll("\\\\", File.separator);
//linux--end----
File pdfFile = new File(pdfPath);
PDDocument document = null;
try {
input = new FileInputStream(pdfFile);
// 加载 pdf 文档
PDFParser parser = new PDFParser(input);
parser.parse();
document = parser.getPDDocument();
// 获取内容信息
PDFTextStripper pts = new PDFTextStripper();
pts.setSortByPosition(sort);
endPage = document.getNumberOfPages();
System.out.println("Total Page: " + endPage);
pts.setStartPage(startPage);
pts.setEndPage(endPage);
try {
content = pts.getText(document);
} catch (Exception e) {
throw e;
}
System.out.println("Get PDF Content ...");
} catch (Exception e) {
throw e;
} finally {
if (null != input)
input.close();
if (null != document)
document.close();
}
content = content.replace("\n", "");
content = content.replace("\\\\r", "");
content = content.replace("\\\\t", "");
System.out.println(content);
return content;
}
public static void main(String[] args) throws Exception {
// Integer page = PDFToPage("D:\\Desktop\\图书文件夹\\4.pdf");
// PDFToWord("D:\\Desktop\\图书文件夹\\4.pdf", page);
// Integer page = PDFToPage("D:\\图书.pdf");
// PDFToWord("D:\\图书.pdf", page);
getTextFromPdf("D:\\Desktop\\图书文件夹\\4.pdf");
}
}