mport com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.multipdf.PDFCloneUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFMarkedContentExtractor;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class MyTest {
public static void main(String[] args) throws IOException {
String file1="D:/测试/2022-10/(3).pdf";
PDDocument load = PDDocument.load(new File(file1));
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(load);
//文本
String[] lines = pdfFileInText.split("\\r?\\n");
//识别图片
PDFCloneUtility pdfCloneUtility = new PDFCloneUtility(load);
PDPageTree pages = load.getDocumentCatalog().getPages();
for (PDPage page : pages) {
PDResources resources = page.getResources();
Iterable<COSName> xObjectNames = resources.getXObjectNames();
for (COSName xObjectName : xObjectNames) {
PDXObject xObject = resources.getXObject(xObjectName);
if (xObject instanceof PDImageXObject) {
BufferedImage image = ((PDImageXObject) xObject).getImage();
// 可以打印到本地,查看输出顺序
String imglocation = "D:/测试/2022-10/";
File imgfile = new File(imglocation + "测试" + ".png");
ImageIO.write(image, "png", imgfile);
}
}
}
}
}
提取pdf文案内容,图片识别
于 2022-11-03 17:56:43 首次发布