簡介
pdfbox是Apache開源的一個(gè)項(xiàng)目,支持pdf文檔操作功能。 官網(wǎng)地址:?Apache PDFBox | A Java PDF Library 支持的功能如下圖.引入依賴
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-app</artifactId>
<version>2.0.19</version>
</dependency>
pdf轉(zhuǎn)換成圖片
/**
* 經(jīng)過測試,dpi為96,100,105,120,150,200中,105顯示效果較為清晰,體積穩(wěn)定,dpi越高圖片體積越大,一般電腦顯示分辨率為96
*/
public static final float DEFAULT_DPI = 105;
/**
* 默認(rèn)轉(zhuǎn)換的圖片格式為jpg
*/
public static final String DEFAULT_FORMAT = "jpg";
/**
* pdf轉(zhuǎn)換成圖片
*
* @param pdfPath pdf文件的路徑 例如: D:\\test\\test.pdf (2頁)
* @param targetPath 輸出的圖片路徑 D:\\test\\
* @return 抽取出來的圖片路徑數(shù)組 Arrays.asList( "D:\\test\\1.jpg","D:\\test\\2.jpg" )
*/
public static List<String> pdfToManyImage(String pdfPath, String targetPath) {
File file = new File(pdfPath);
if (!file.exists()) {
return null;
}
try {
//加載pdf文件
PDDocument doc = PDDocument.load(file);
//讀取pdf文件
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
List<String> stringList = new ArrayList<>(pageCount);
String filePath = null;
BufferedImage image;
for (int i = 0; i < pageCount; i++) {
//96/144/198
// Windows native DPI
image = renderer.renderImageWithDPI(i, DEFAULT_DPI);
// BufferedImage srcImage = resize(image, 240, 240);//產(chǎn)生縮略圖
filePath = targetPath + (i + 1) + "." + DEFAULT_FORMAT;
//保存圖片
ImageIO.write(image, DEFAULT_FORMAT, new File(filePath));
stringList.add(filePath);
}
return stringList;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
圖片合成pdf
/**
* 多圖片合成pdf的限制后綴
*/
private static final List IMAGE_SUFFIX = Arrays.asList("jpg", "png", "jpeg");
/**
* 多個(gè)圖片合成一個(gè)pdf
*
* @param imgFolder 多圖片的文件夾路徑 例如:"D:\\image\\"
* @param target 合并的圖片路徑 "D:\\image\\merge.pdf"
* @throws IOException
*/
public static void manyImageToOnePdf(String imgFolder, String target) throws IOException {
PDDocument doc = new PDDocument();
//創(chuàng)建一個(gè)空的pdf文件
doc.save(target);
PDPage page;
PDImageXObject pdImage;
PDPageContentStream contents;
BufferedImage bufferedImage;
String fileName;
float w, h;
String suffix;
File tempFile;
int index;
File folder = new File(imgFolder);
for (int i = 0; i < folder.listFiles().length; i++) {
tempFile = folder.listFiles()[i];
if (!tempFile.isFile()) {
continue;
}
fileName = tempFile.getName();
index = fileName.lastIndexOf(".");
if (index == -1) {
continue;
}
//獲取文件的后綴
suffix = fileName.substring(index + 1);
//如果文件后綴不是圖片格式,跳過當(dāng)前循環(huán)
if (!IMAGE_SUFFIX.contains(suffix)) {
continue;
}
bufferedImage = ImageIO.read(folder.listFiles()[i]);
//Retrieving the page
pdImage = LosslessFactory.createFromImage(doc, bufferedImage);
w = pdImage.getWidth();
h = pdImage.getHeight();
page = new PDPage(new PDRectangle(w, h));
contents = new PDPageContentStream(doc, page);
contents.drawImage(pdImage, 0, 0, w, h);
System.out.println("Image inserted");
contents.close();
doc.addPage(page);
}
//保存pdf
doc.save(target);
//關(guān)閉pdf
doc.close();
}
多個(gè)pdf合成1個(gè)pdf
/**
* pdf合并拼接
*
* @param files pdf文件列表 例如: Arrays.asList(new File("D:\\1.pdf"),new File("D:\\2.pdf"))
* @param targetPath 合并后的文件 D:\\merge.pdf
* @return 合并后的文件File對象
*/
public static File manyPdfToOne(List<File> files, String targetPath) {
try {
PDFMergerUtility mergePdf = new PDFMergerUtility();
for (File f : files) {
if (f.exists() && f.isFile()) {
// 循環(huán)添加要合并的pdf
mergePdf.addSource(f);
}
}
// 設(shè)置合并生成pdf文件名稱
mergePdf.setDestinationFileName(targetPath);
// 合并pdf
mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
return new File(targetPath);
} catch (Exception e) {
return null;
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-740371.html
文章來源:http://www.zghlxwxcb.cn/news/detail-740371.html
到了這里,關(guān)于Java使用pdfbox進(jìn)行pdf和圖片之間的轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!