pdfbox 第一页加内容_Java使用PDFBox操作PDF文件获取页码、文章内容、缩略图

该博客介绍了如何使用Java的PDFBox库来操作PDF文件,包括获取PDF文件的总页数、提取文章内容以及生成PDF文件的缩略图。示例代码详细展示了实现这些功能的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、依赖

com.sleepycat

je

5.0.73

org.apache.pdfbox

pdfbox

2.0.8

二、实现代码

import lombok.extern.slf4j.Slf4j;

import org.apache.pdfbox.pdfparser.PDFParser;

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.rendering.ImageType;

import org.apache.pdfbox.rendering.PDFRenderer;

import org.apache.pdfbox.text.PDFTextStripper;

import javax.imageio.IIOImage;

import javax.imageio.ImageIO;

import javax.imageio.ImageWriter;

import javax.imageio.stream.ImageOutputStream;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Iterator;

@Slf4j

public class PdfUtil {

/**

* 通过PDFbox获取文章总页数

*

* @param filePath:文件路径

* @return

* @throws IOException

*/

public static int getNumberOfPages(String filePath) throws IOException, InterruptedException {

File file = new File(filePath);

PDDocument pdDocument = PDDocument.load(new File(filePath));

int pages = pdDocument.getNumberOfPages();

pdDocument.close();

return pages;

}

}

/**

* 通过PDFbox获取文章内容

*

* @param filePath

* @return

*/

public static String getContent(String filePath) throws IOException {

PDFParser pdfParser = new PDFParser(new org.apache.pdfbox.io.RandomAccessFile(new File(filePath), "rw"));

pdfParser.parse();

PDDocument pdDocument = pdfParser.getPDDocument();

String text = new PDFTextStripper().getText(pdDocument);

pdDocument.close();

return text;

}

/**

* 通过PDFbox生成文件的缩略图

*

* @param filePath:文件路径

* @param outPath:输出图片路径

* @throws IOException

*/

public static void getThumbnails(String filePath, String outPath) throws IOException {

// 利用PdfBox生成图像

PDDocument pdDocument = PDDocument.load(new File(filePath));

PDFRenderer renderer = new PDFRenderer(pdDocument);

// 构造图片

BufferedImage img_temp = renderer.renderImageWithDPI(0, 30, ImageType.RGB);

// 设置图片格式

Iterator it = ImageIO.getImageWritersBySuffix("png");

// 将文件写出

ImageWriter writer = (ImageWriter) it.next();

ImageOutputStream imageout = ImageIO.createImageOutputStream(new FileOutputStream(outPath));

writer.setOutput(imageout);

writer.write(new IIOImage(img_temp, null, null));

img_temp.flush();

imageout.flush();

imageout.close();

//Warning: You did not close a PDF Document

pdDocument.close();

}

}

三、测试类--Main

import java.io.IOException;

/**

* @author Mr.lu

* @Title: Main

* @ProjectName DocCloud

* @Description: TODO

* @date 2018/11/6:22:17

*/

public class Main {

public static void main(String[] args) throws IOException, InterruptedException {

int numberOfPages = getNumberOfPages("D:\\Desktop\\DocCloud\\testDir\\hadoopClientCode.pdf");

System.out.println(numberOfPages);

String content = getContent("");

System.out.println(content);

getThumbnails("D:\\Desktop\\DocCloud\\testDir\\hadoopClientCoed.pdf",

"D:\\Desktop\\DocCloud\\testDir\\hadoopClientCoed.pdf.png");

}

}

1>首先测试生成PDF文件的页码,在控制台可以看到

2>测试获取PDF文件的内容,在控制台可以看到--你自己PDF文件中的内容

3>测试生成PDF缩略图

缩略图的大小,可以在代码中修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值