方式一
1、引入maven
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.25</version>
</dependency>
2、代码工具类
import com.google.common.collect.Lists;
import com.jiayou.peis.entity.ImageObject;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImage;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFTextStripper;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class PdfUtils {
public static String text(byte[] data) throws IOException {
return PdfUtils.text(data, true);
}
public static String text(byte[] data, boolean sortByPosition) throws IOException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
return PdfUtils.text(inputStream, sortByPosition);
}
public static String text(File file, boolean sortByPosition) throws IOException {
InputStream inputStream = new FileInputStream(file);
return PdfUtils.text(inputStream, sortByPosition);
}
public static String text(File file) throws IOException {
return PdfUtils.text(file, true);
}
public static String text(InputStream inputStream) throws IOException {
return text(inputStream, true);
}
public static String text(InputStream inputStream, boolean sortByPosition) throws IOException {
PDDocument document = null;
try {
document = Loader.loadPDF(inputStream);
PDFTextStripper textStripper = new PDFTextStripper();
int numberOfPages = document.getNumberOfPages();
textStripper.setStartPage(1);
textStripper.setEndPage(numberOfPages);
textStripper.setSortByPosition(sortByPosition);
textStripper.setShouldSeparateByBeads(true);
return StrUtils.removeReturnChar(textStripper.getText(document));
} finally {
CloseUtils.closeQuietly(document, inputStream);
}
}
public static List<ImageObject> images(File file) throws IOException {
InputStream inputStream = new FileInputStream(file);
return PdfUtils.images(inputStream);
}
public static List<ImageObject> images(byte[] data) throws IOException {
ByteArrayInputStream inputStream = null;
try {
inputStream = new ByteArrayInputStream(data);
return PdfUtils.images(inputStream);
} finally {
CloseUtils.closeQuietly(inputStream);
}
}
public static List<ImageObject> images(InputStream inputStream) throws IOException {
List<ImageObject> imageList = Lists.newArrayList();
PDDocument document = null;
try {
document = Loader.loadPDF(inputStream);
PDResources pdResources = document.getPage(0).getResources();
int i = 0;
for (COSName csName : pdResources.getXObjectNames()) {
PDXObject pdxObject = pdResources.getXObject(csName);
if (pdxObject instanceof PDImageXObject) {
PDStream pdStream = pdxObject.getStream();
PDImageXObject image = new PDImageXObject(pdStream, pdResources);
String imageSuffix = imageSuffix(image);
BufferedImage bufferedImage = image.getImage();
ImageObject object = new ImageObject();
object.setIndex(i++);
object.setImage(bufferedImage);
object.setSuffix(imageSuffix);
imageList.add(object);
}
}
} finally {
CloseUtils.closeQuietly(document, inputStream);
}
return imageList;
}
private static String imageSuffix(PDImageXObject pdImage) throws IOException {
String suffix = pdImage.getSuffix();
if (suffix == null || "jb2".equals(suffix)) {
suffix = "png";
} else if ("jpx".equals(suffix)) {
suffix = "jp2";
}
if (hasMasks(pdImage)) {
suffix = "png";
}
return suffix;
}
private static boolean hasMasks(PDImage pdImage) throws IOException {
if (pdImage instanceof PDImageXObject) {
PDImageXObject ximg = (PDImageXObject) pdImage;
return ximg.getMask() != null || ximg.getSoftMask() != null;
}
return false;
}
public static void saveImage(List<ImageObject> imageList, String dir, String prefixName) throws IOException {
File imgDir = new File(dir);
FileUtils.forceMkdir(imgDir);
for(ImageObject image:imageList){
File imgFile = new File(dir, prefixName+"_"+image.getIndex()+"."+image.getSuffix());
ImageIO.write(image.getImage(), image.getSuffix(), imgFile);
}
}
}
3、相关代码
CloseUtils
import java.io.Closeable;
import java.util.zip.ZipInputStream;
public class CloseUtils {
public static void closeQuietly(Closeable... closeables) {
if (closeables != null && closeables.length > 0) {
for (Closeable closeable : closeables) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {}
}
}
}
}
public static void closeEntryQuietly(ZipInputStream... closeables) {
if (closeables != null && closeables.length > 0) {
for (ZipInputStream closeable : closeables) {
if (closeable != null) {
try {
closeable.closeEntry();
} catch (Exception e) { }
}
}
}
}
}
StrUtils
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StrUtils {
public static List<String> lines(String text){
if(text == null || text.isEmpty()){
return null;
}
return Lists.newArrayList(text.split("\n+"));
}
public static String splitAt(String text, int lineIndex){
if(text == null || text.isEmpty()){
return null;
}
String[] lines = text.split("\\s+");
return lines.length > lineIndex ? lines[lineIndex] : null;
}
public static String lineAt(String text, int lineIndex){
if(text == null || text.isEmpty()){
return null;
}
String[] lines = text.split("\r?\n");
return lines.length > lineIndex ? lines[lineIndex] : null;
}
public static String substringBetween(String text, String startFlag, String closeFlag, String subType){
return substringBetween(text, startFlag, closeFlag, subType, null);
}
public static String substringBetween(String text, String startFlag, String closeFlag, String subType, String defaultVal){
if(StringUtils.isEmpty(text)){
return defaultVal;
}
if("substringBetween".equals(subType)){
if(StringUtils.isNotEmpty(startFlag) && StringUtils.isNotEmpty(closeFlag)){
return StrUtils.substringBetween(text, startFlag, closeFlag);
}
} else if("substringBetweenLast".equals(subType)){
if(StringUtils.isNotEmpty(startFlag) && StringUtils.isNotEmpty(closeFlag)){
return StrUtils.substringBetweenLast(text, startFlag, closeFlag);
}
} else if("substringBetweenLastBefore".equals(subType)){
if(StringUtils.isNotEmpty(startFlag) && StringUtils.isNotEmpty(closeFlag)){
return StrUtils.substringBetweenLastBefore(text, startFlag, closeFlag);
}
} else if("substringBetweenBeforeLast".equals(subType)){
if(StringUtils.isNotEmpty(startFlag) && StringUtils.isNotEmpty(closeFlag)){
return StrUtils.substringBetweenBeforeLast(text, startFlag, closeFlag);
}
} else if("substringAfter".equals(subType)){
if(StringUtils.isNotEmpty(startFlag)){
return StringUtils.substringAfter(text, startFlag);
}
} else if("substringAfterLast".equals(subType)){
if(StringUtils.isNotEmpty(startFlag)){
return StringUtils.substringAfterLast(text, startFlag);
}
} else if("substringBefore".equals(subType)){
if(StringUtils.isNotEmpty(closeFlag)){
return StringUtils.substringBefore(text, closeFlag);
}
} else if("substringBeforeLast".equals(subType)){
if(StringUtils.isNotEmpty(closeFlag)){
return StringUtils.substringBeforeLast(text, closeFlag);
}
}
return defaultVal;
}
public static String substringBetween(String text, String startFlag, String closeFlag){
if(text == null || text.isEmpty()){
return null;
}
int startIndex = text.indexOf(startFlag);
int endIndex = text.indexOf(closeFlag);
if(startIndex >= 0 && endIndex >= 0 && endIndex >= startIndex){
return text.substring(startIndex+startFlag.length(), endIndex);
}
return null;
}
public static String substringBetween(String text, String startFlag){
if(text == null || text.isEmpty()){
return null;
}
int startIndex = text.indexOf(startFlag);
if(startIndex >= 0 && startIndex != -1){
return text.substring(startIndex + startFlag.length());
}
return null;
}
public static String substringBetweenLast(String text, String startFlag, String closeFlag){
if(text == null || text.isEmpty()){
return null;
}
int startIndex = text.lastIndexOf(startFlag);
int endIndex = text.lastIndexOf(closeFlag);
if(startIndex >= 0 && endIndex >= 0 && endIndex >= startIndex){
return text.substring(startIndex+startFlag.length(), endIndex);
}
return null;
}
public static String substringBetweenLastBefore(String text, String startFlag, String closeFlag){
if(text == null || text.isEmpty()){
return null;
}
int startIndex = text.lastIndexOf(startFlag);
int endIndex = text.indexOf(closeFlag);
if(startIndex >= 0 && endIndex >= 0 && endIndex >= startIndex){
return text.substring(startIndex+startFlag.length(), endIndex);
}
return null;
}
public static String substringBetweenBeforeLast(String text, String startFlag, String closeFlag){
if(text == null || text.isEmpty()){
return null;
}
int startIndex = text.indexOf(startFlag);
int endIndex = text.lastIndexOf(closeFlag);
if(startIndex >= 0 && endIndex >= 0 && endIndex >= startIndex){
return text.substring(startIndex+startFlag.length(), endIndex);
}
return null;
}
public static String nullToEmpty(String val){
return val == null ? "" : val;
}
public static String removeReturnChar(String val){
return val == null ? "" : val.replace("\r", "");
}
public static String getLastNotNullText(String[] array){
List<String> collect = Arrays.stream(array).filter(x -> StrUtil.isNotBlank(x)).collect(Collectors.toList());
return collect.get(collect.size() - 1);
}
public static String getFirstNotNullText(String[] array){
List<String> collect = Arrays.stream(array).filter(x -> StrUtil.isNotBlank(x)).collect(Collectors.toList());
return collect.get(0);
}
}
方式二
https://blog.csdn.net/ThinkPet/article/details/131256428