本文共 6674 字,大约阅读时间需要 22 分钟。
在日常开发工作中,我们经常需要处理PDF文档的内容提取需求。Apache pdfbox是一款功能强大的PDF处理工具,可以帮助我们轻松提取PDF文档中的文字内容和图片资源。本文将详细介绍如何使用pdfbox进行PDF文档的内容提取工作。
为了使用pdfbox进行PDF文档的内容提取,我们需要先完成以下环境搭建工作:
org.apache.pdfbox fontbox 2.0.1 org.apache.pdfbox pdfbox 2.0.1 com.itextpdf itextpdf 5.5.13 net.coobird thumbnailator 0.4.8
import org.apache.pdfbox.getDocumentInformation.PDDocumentInformation;import org.apache.pdfbox.pdfinfo.PDFTextStripper;import org.apache.pdfbox.model.PDDocument;import org.apache.pdfbox.model.PDPage;import org.apache.pdfbox.model.COSName;import org.apache.pdfbox.resources.PDResources;
pdfbox 提供了强大的API来处理PDF文档的内容提取。以下是一个简单的文字内容提取示例:
public static void extractText(String pdfFilePath) throws Exception { try (PDDocument document = PDDocument.load(new File(pdfFilePath))) { // 检查是否有内容提取权限 AccessPermission ap = document.getCurrentAccessPermission(); if (!ap.canExtractContent()) { throw new IOException("You do not have permission to extract text"); } PDFTextStripper stripper = new PDFTextStripper(); stripper.setSortByPosition(true); for (int p = 1; p <= document.getNumberOfPages(); ++p) { stripper.setStartPage(p); stripper.setEndPage(p); String text = stripper.getText(document); // 打印提取结果 System.out.println("Page " + p + ":"); System.out.println(text.trim()); System.out.println(); } }} PDDocument.load方法加载目标PDF文档。PDFTextStripper对象,用于提取PDF文档的文字内容。startPage和endPage来指定提取的具体页面范围。除了文字内容,pdfbox还提供了丰富的API来提取PDF文档的其他信息。以下是一个完整的PDF文档信息提取示例:
public static void pdfParse(String pdfPath) throws Exception { InputStream input = null; PDDocument document = null; try { document = PDDocument.load(new File(pdfPath)); // 提取文档基本信息 PDDocumentInformation info = document.getDocumentInformation(); System.out.println("标题:" + info.getTitle()); System.out.println("主题:" + info.getSubject()); System.out.println("作者:" + info.getAuthor()); System.out.println("关键字:" + info.getKeywords()); System.out.println("创建时间:" + dateFormat(info.getCreationDate())); System.out.println("修改时间:" + dateFormat(info.getModificationDate())); // 提取页面资源信息 PDDocumentCatalog cata = document.getDocumentCatalog(); for (int i = 0; i < document.getNumberOfPages(); ++i) { PDPage page = document.getPage(i); if (page != null) { PDResources res = page.getResources(); Iterable xit = res.getXObjectNames(); Iterator iterator = xit.iterator(); while (iterator.hasNext()) { COSName cosName = iterator.next(); if (res.isImageXObject(cosName)) { PDImageXObject pdImageXObject = (PDImageXObject) res.getXObject(cosName); Thumbnails.of(pdImageXObject.getImage()).scale(0.9).toFile(new File("D:\\pdf\\" + System.currentTimeMillis() + ".jpg")); } } } } } catch (Exception e) { throw e; } finally { if (input != null) { input.close(); } if (document != null) { document.close(); } }} PDDocument.load方法加载目标PDF文档。PDDocumentInformation类获取PDF文档的基本信息,包括标题、主题、作者等。PDResources类获取页面的资源对象,判断是否为图片资源。Thumbnails.of方法将图片转换为指定大小的图片文件,并保存到目标目录中。如果需要将PDF文件转换为图片文件,可以使用以下方法:
private static boolean pdf2Image(String PdfFilePath, String dstImgFolder, int dpi) { File file = new File(PdfFilePath); try { PDDocument pdDocument = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(pdDocument); String imgPDFName = file.getName().substring(0, file.getName().lastIndexOf('.')); String imgFolderPath = (dstImgFolder.isEmpty()) ? (file.getParent() + File.separator + imgPDFName) : (dstImgFolder + File.separator + imgPDFName); if (createDirectory(imgFolderPath)) { for (int i = 0; i < renderer.getNumberOfPages(); ++i) { BufferedImage image = renderer.renderImageWithDPI(i, dpi); String imgFilePath = imgFolderPath + File.separator + imgPDFName + "_".concat(String.valueOf(formatNumber(i + 1))) + ".jpg"; File dstFile = new File(imgFilePath); ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); writer.setOutput(ImageIO.createImageOutputStream(dstFile)); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.3f); writer.write(null, new IIOImage(image, null, null), param); } System.out.println("PDF文档转图片成功!" + dstImgFolder); return true; } else { System.out.println("PDF文档转图片失败:" + "创建" + imgFolderPath + "失败"); return false; } } catch (IOException e) { e.printStackTrace(); return false; }} createDirectory方法检查并创建目标图片存储目录。PDFRenderer渲染每页的图片。为了提高开发效率,我们还开发了一些辅助功能:
dateFormat方法将日期信息格式化为指定的字符串:private static String dateFormat(Calendar calendar) throws Exception { if (null == calendar) { return null; } String pattern = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat format = new SimpleDateFormat(pattern); return format.format(calendar.getTime());} createDirectory方法检查并创建指定的目录:private static boolean createDirectory(String folder) { File dir = new File(folder); if (dir.exists()) { return true; } else { return dir.mkdirs(); }} 通过本文的介绍,我们可以清晰地了解如何使用pdfbox进行PDF文档的内容提取工作。无论是单纯的文字内容提取,还是对PDF文档的详细信息提取,都可以通过pdfbox提供的强大API轻松完成。通过合理配置依赖管理,我们可以快速启动项目,并通过自定义工具类提高开发效率。希望本文对您的PDF处理开发工作能有所帮助。
转载地址:http://hovfk.baihongyu.com/