pdfbox转换html,Apache PDFBox将pdf转换为图像

public class PDFtoJPGConverter {

public List convertPdfToImage(File file, String destination) throws Exception {

File destinationFile = new File(destination);

if (!destinationFile.exists()) {

destinationFile.mkdir();

System.out.println("DESTINATION FOLDER CREATED -> " + destinationFile.getAbsolutePath());

}else if(destinationFile.exists()){

System.out.println("DESTINATION FOLDER ALLREADY CREATED!!!");

}else{

System.out.println("DESTINATION FOLDER NOT CREATED!!!");

}

if (file.exists()) {

PDDocument doc = PDDocument.load(file);

PDFRenderer renderer = new PDFRenderer(doc);

List fileList = new ArrayList();

String fileName = file.getName().replace(".pdf", "");

System.out.println("CONVERTER START.....");

for (int i = 0; i 

// default image files path: original file path

// if necessary, file.getParent() + "/" => another path

File fileTemp = new File(destination + fileName + "_" + i + ".jpg"); // jpg or png

BufferedImage image = renderer.renderImageWithDPI(i, 200);

// 200 is sample dots per inch.

// if necessary, change 200 into another integer.

ImageIO.write(image, "JPEG", fileTemp); // JPEG or PNG

fileList.add(fileTemp);

}

doc.close();

System.out.println("CONVERTER STOPTED.....");

System.out.println("IMAGE SAVED AT -> " + destinationFile.getAbsolutePath());

return fileList;

} else {

System.err.println(file.getName() + " FILE DOES NOT EXIST");

}

return null;

}

public static void main(String[] args) {

try {

PDFtoJPGConverter converter = new PDFtoJPGConverter();

Scanner sc = new Scanner(System.in);

System.out.print("Enter your destination folder where save image \n");

// Destination = D:/PPL/;

String destination = sc.nextLine();

System.out.print("Enter your selected pdf files name with source folder \n");

String sourcePathWithFileName = sc.nextLine();

// Source Path = D:/PDF/ant.pdf,D:/PDF/abc.pdf,D:/PDF/xyz.pdf

if (sourcePathWithFileName != null || sourcePathWithFileName != "") {

String[] files = sourcePathWithFileName.split(",");

for (String file : files) {

File pdf = new File(file);

System.out.print("FILE:>> "+ pdf);

converter.convertPdfToImage(pdf, destination);

}

}

} catch (Exception ex) {

ex.printStackTrace();

}

}}

====================================

在这里,我使用Apache pdfbox-2.0.8,commons-logging-1.2和fontbox-2.0.8 Library

快乐编码:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值