Browse Source

word/excel导出PDF与PDF合并开发

anderx 5 months ago
parent
commit
aa2ed3b007
1 changed files with 0 additions and 64 deletions
  1. 0 64
      src/main/java/com/goafanti/common/utils/pdf/PDFUtils.java

+ 0 - 64
src/main/java/com/goafanti/common/utils/pdf/PDFUtils.java

@@ -17,76 +17,12 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.apache.poi.xwpf.usermodel.*;
 import org.springframework.http.MediaType;
 
-import javax.imageio.ImageIO;
 import javax.servlet.http.HttpServletResponse;
-import java.awt.image.BufferedImage;
 import java.io.*;
 import java.util.Date;
 
 public class PDFUtils {
 
-    /**
-     * 将多个图片合并为一个PDF文件
-     *
-     * @param imagesDir   图片目录路径
-     * @param pdfFilePath 输出PDF文件路径
-     * @throws IOException
-     * @throws DocumentException
-     */
-    public static void mergeImagesToPDF(String imagesDir, String pdfFilePath)
-            throws IOException, DocumentException {
-        Document document = new Document(PageSize.A4);
-        PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
-        document.open();
-
-        File dir = new File(imagesDir);
-        File[] imageFiles = dir.listFiles((d, name) -> name.toLowerCase().endsWith(".png") ||
-                name.toLowerCase().endsWith(".jpg") ||
-                name.toLowerCase().endsWith(".jpeg"));
-
-        if (imageFiles != null) {
-            // 按文件名排序确保页面顺序正确
-            java.util.Arrays.sort(imageFiles, (f1, f2) -> {
-                String name1 = f1.getName().replaceAll("[^0-9]", "");
-                String name2 = f2.getName().replaceAll("[^0-9]", "");
-                if (name1.isEmpty() || name2.isEmpty()) {
-                    return f1.getName().compareTo(f2.getName());
-                }
-                return Integer.parseInt(name1) - Integer.parseInt(name2);
-            });
-
-            // 将每个图片添加到PDF中
-            for (File imageFile : imageFiles) {
-                BufferedImage bufferedImage = ImageIO.read(imageFile);
-                if (bufferedImage != null) {
-                    // 计算图片在页面中的尺寸
-                    float documentWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();
-                    float documentHeight = document.getPageSize().getHeight() - document.topMargin() - document.bottomMargin();
-
-                    // 按比例缩放图片
-                    float imageWidth = bufferedImage.getWidth();
-                    float imageHeight = bufferedImage.getHeight();
-                    float scale = Math.min(documentWidth / imageWidth, documentHeight / imageHeight);
-
-                    Image image = Image.getInstance(imageFile.getAbsolutePath());
-                    image.scaleAbsolute(imageWidth * scale, imageHeight * scale);
-
-                    // 居中显示图片
-                    image.setAlignment(Element.ALIGN_CENTER);
-
-                    // 添加图片到PDF
-                    document.add(image);
-
-                    // 如果不是最后一个图片,则添加新页面
-                    if (imageFile != imageFiles[imageFiles.length - 1]) {
-                        document.newPage();
-                    }
-                }
-            }
-        }
-
-        document.close();
-    }
 
     /**
      * 删除临时目录及其内容