该方法可以选择多个图片是否合并为一个pdf输出,也可以选择图片为横向或者纵向输出,也可以选择pdf页面为A3或者A4
第一步
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
第二步
package com.example.demo.file.pdf;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ImageConditionToPdfConverter {
private static final List<String> SUPPORTED_IMAGE_SUFFIXES = Arrays.asList("png", "jpg", "jpeg", "gif", "bmp", "tiff");
public static void main(String[] args) {
String imagesFolder = "C:\\Users\\EDY\\Desktop\\测试图片"; // 图片文件夹路径
String outputFolder = "C:\\Users\\EDY\\Desktop\\output"; // 输出PDF文件夹路径
boolean mergePdf = true; // 是否将所有图片合并到同一个PDF文件中
boolean landscape = false; // 图片布局方向,横向为true,纵向为false
String pageSize = "A4"; // 页面大小,可以是"A3"或"A4"
List<File> imageFiles = getImageFiles(imagesFolder, SUPPORTED_IMAGE_SUFFIXES);
ensureOutputDirectoryExists(outputFolder); // 确保输出目录存在
try {
if (mergePdf) {
// 如果合并到同一个PDF,创建一个文档并添加所有页面
PDDocument document = new PDDocument();
for (File imageFile : imageFiles) {
PDPage page = createPage(pageSize, landscape);
document.addPage(page);
PDImageXObject pdImage = PDImageXObject.createFromFile(String.valueOf(imageFile), document);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
drawImage(contentStream, pdImage, page.getMediaBox(), landscape);
contentStream.close();
}
document.save(outputFolder + "\\output.pdf");
document.close();
} else {
// 如果不合并到同一个PDF,为每个图片创建一个文档
for (File imageFile : imageFiles) {
PDDocument document = new PDDocument();
PDPage page = createPage(pageSize, landscape);
document.addPage(page);
PDImageXObject pdImage = PDImageXObject.createFromFile(String.valueOf(imageFile), document);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
drawImage(contentStream, pdImage, page.getMediaBox(), landscape);
contentStream.close();
document.save(outputFolder + "\\output_" + imageFiles.indexOf(imageFile) + ".pdf");
document.close();
}
}
System.out.println("PDF created successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
private static PDPage createPage(String pageSize, boolean landscape) {
PDRectangle mediaBox = "A3".equalsIgnoreCase(pageSize) ? PDRectangle.A3 : PDRectangle.A4;
PDPage page = new PDPage(mediaBox);
if (landscape) {
page.setRotation(90);
}
return page;
}
private static void drawImage(PDPageContentStream contentStream, PDImageXObject pdImage, PDRectangle mediaBox, boolean landscape) throws IOException {
float imageWidth = pdImage.getWidth();
float imageHeight = pdImage.getHeight();
float pageWidth = mediaBox.getWidth();
float pageHeight = mediaBox.getHeight();
if (landscape) {
float temp = pageWidth;
pageWidth = pageHeight;
pageHeight = temp;
}
float scale = Math.min(pageWidth / imageWidth, pageHeight / imageHeight);
imageWidth *= scale;
imageHeight *= scale;
// Center the image on the page
float x = (pageWidth - imageWidth) / 2;
float y = (pageHeight - imageHeight) / 2;
contentStream.drawImage(pdImage, x, y, imageWidth, imageHeight);
}
private static List<File> getImageFiles(String folderPath, List<String> supportedSuffixes) {
File folder = new File(folderPath);
File[] files = folder.listFiles();
if (files != null) {
List<File> imageFiles = new ArrayList<>();
for (File file : files) {
if (file.isFile() && isImage(file, supportedSuffixes)) {
imageFiles.add(file);
}
}
return imageFiles;
}
return new ArrayList<>();
}
private static boolean isImage(File file, List<String> supportedSuffixes) {
String fileName = file.getName().toLowerCase();
return supportedSuffixes.stream().anyMatch(fileName::endsWith);
}
private static void ensureOutputDirectoryExists(String directoryPath) {
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
}
}