需要用到下面三个库
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
下面是代码及示例
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class PdfUtils {
public static void main(String[] args) throws IOException {
String fileUrl = "xx.pdf";
pdfToImg(fileUrl, "C:\code\img", "jpg").forEach(System.out::println);
String targetPdfFile = "hello.pdf";
List<String> list = Arrays.asList("C:\code\img\0ea9685e5db943d29868a9893f4b0600.jpg",
"C:\code\img\1a41f1790060466b97ded7f2b0c85588.jpg");
new File(targetPdfFile).createNewFile();
imgToPdf(list, targetPdfFile);
}
/**
* PDF转图片 根据页码一页一页转
*
* @throws IOException imgType:转换后的图片类型 jpg,png
*/
public static List<String> pdfToImg(String pdfUrl, String basePath, String imgType) {
List<String> files = new ArrayList<>();
PDDocument pdDocument = null;
/* dpi越大转换后越清晰,相对转换速度越慢 */
int dpi = 200;
try {
pdDocument = getPDDocument(pdfUrl);
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pages = pdDocument.getNumberOfPages();
for (int page = 0; page < pages; page++) {
String fileName = basePath
+ "/" + UUID.randomUUID().toString().replaceAll("-", "")
+ "." + imgType;
File f = new File(fileName);
BufferedImage image = renderer.renderImageWithDPI(page, dpi);
ImageIO.write(image, imgType, f);
files.add(fileName);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pdDocument != null) {
try {
pdDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return files;
}
public static void imgToPdf(List<String> imgList, String target) {
//创建文档
Document document = new Document();
//设置文档页边距
document.setMargins(0,0,0,0);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(target);
PdfWriter.getInstance(document, fos);
//打开文档
document.open();
document.setPageCount(imgList.size());
for (String source : imgList) {
//获取图片的宽高
Image image = Image.getInstance(source);
float imageHeight=image.getScaledHeight();
float imageWidth=image.getScaledWidth();
//设置页面宽高与图片一致
Rectangle rectangle = new Rectangle(imageWidth, imageHeight);
document.setPageSize(rectangle);
//图片居中
image.setAlignment(Image.ALIGN_CENTER);
//新建一页添加图片
document.newPage();
document.add(image);
}
} catch (Exception ioe) {
System.out.println(ioe.getMessage());
} finally {
//关闭文档
document.close();
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static List<String> pdfToImg(File file, String basePath, String imgType) {
List<String> files = new ArrayList<>();
PDDocument pdDocument = null;
/* dpi越大转换后越清晰,相对转换速度越慢 */
int dpi = 200;
try {
pdDocument = getPDDocument(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pages = pdDocument.getNumberOfPages();
for (int page = 0; page < pages; page++) {
String fileName = basePath
+ "/" + UUID.randomUUID().toString().replaceAll("-", "")
+ "." + imgType;
File f = new File(fileName);
BufferedImage image = renderer.renderImageWithDPI(page, dpi);
ImageIO.write(image, imgType, f);
files.add(fileName);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pdDocument != null) {
try {
pdDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return files;
}
private static PDDocument getPDDocument(String fileUrl) throws IOException {
File file = new File(fileUrl);
FileInputStream inputStream = new FileInputStream(file);
return PDDocument.load(inputStream);
}
private static PDDocument getPDDocument(File file) throws IOException {
FileInputStream inputStream = new FileInputStream(file);
return PDDocument.load(inputStream);
}
}