public static List<String> pdf2images(String inPath, Integer start, Integer end) {
if (!getLicense()) { // 验证License 若不验证则转化出的会有水印产生
return null;
}
//文件 获取文件名字
File oriFile = new File(inPath);
String name = oriFile.getName();
//截取不带后缀名的字段
String fileName = name.substring(0, name.lastIndexOf("."));
//文件上传路径
String parent = oriFile.getParent();
//创建同名文件夹
new File(parent + "/" + fileName).mkdir();
Document doc1 = new Document(inPath); // inPath是将要被转化的文档
PageCollection pages = doc1.getPages();
log.info("pages:" + pages.size());
// ---此处限制文件页码最大300页,超出300页按异常处理
Integer exceptionPages = 300;
if (exceptionPages < pages.size()) {
throw new RuntimeException("页码过大,请核实文件是否正常");
}
if (Objects.isNull(start) || start.equals(0)) {
start = 1;
}
if (Objects.isNull(end) || end > pages.size()) {
end = pages.size();
}
if (start > end) {
log.error("页数错误");
return null;
}
List<String> jpgList = new ArrayList<>(pages.size());
File file = null;
com.aspose.pdf.devices.Resolution reso = new com.aspose.pdf.devices.Resolution(140);
com.aspose.pdf.devices.JpegDevice jpegDevice = new com.aspose.pdf.devices.JpegDevice(reso, 60);
// 下标从1开始
for (int i = start; i <= end; i++) {
file = new File(parent + "/" + fileName + "/" + i + ".jpg"); // 新建一个文件
log.info(i + " pdf => jpg :" + file.getAbsolutePath());
try (FileOutputStream os = new FileOutputStream(file)) {
Page item = pages.get_Item(i);
jpegDevice.process(item, os);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
jpgList.add(file.getAbsolutePath());
}
doc1.close();
return jpgList;
}
Would you please share your sample PDF document for our reference? We will test the scenario in our environment and address it accordingly.
The link you shared required login access. Can you please share a public link so that we can download it without filling any information?
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFJAVA-43247
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.