liusheng
2024-04-22 57a6735173d59feb6cfd95c9a8ec76d6f87bc578
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.ruoyi.common.utils;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import com.ruoyi.common.utils.file.FileUtils;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfDocumentBase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
 
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
 
@Configuration
@Slf4j
public class MergeFilesToPDFUtils {
 
    @Value("${ruoyi.archived}")
    private String archived;
 
    /**
     * 将图片合成pdf
     */
    public Boolean addImageToPdf(String imagePath, String pdfPath) throws IOException {
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        try {
            PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
            document.open();
            // 获取图像的宽度和高度
            BufferedImage image = ImageIO.read(new File(imagePath));
            float imageWidth = image.getWidth();
            float imageHeight = image.getHeight();
 
            // 获取页面的宽度和高度
            float pageWidth = document.getPageSize().getWidth();
            float pageHeight = document.getPageSize().getHeight();
 
            // 计算调整比例,确保图像适应页面
            float widthRatio = pageWidth / imageWidth;
            float heightRatio = pageHeight / imageHeight;
            float ratio = Math.min(widthRatio, heightRatio);
 
            // 计算调整后的图像大小
            float adjustedWidth = imageWidth * ratio - 50;
            float adjustedHeight = imageHeight * ratio - 50;
 
            // 创建图像对象
            Image pdfImage = Image.getInstance(imagePath);
            pdfImage.scaleAbsolute(adjustedWidth, adjustedHeight);
 
            // 添加图像到文档
            document.add(pdfImage);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return true;
    }
 
 
    /**
     * WORD转PDF
     *
     * @param inputPath
     * @param outputPath
     */
    public Boolean wordtoPdf(String inputPath, String outputPath) {
        Document doc = new Document();
        //加载Word
        doc.loadFromFile(inputPath);
        //保存为PDF格式
        doc.saveToFile(outputPath, FileFormat.PDF);
        return true;
    }
 
    /**
     * pdf合并
     */
    public void mergePdfFiles(List<String> pdfFiles, String fileName) throws IOException, DocumentException {
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        log.info("fileName的全路径:{}", archived + "/" + fileName);
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(archived + "/" + fileName));
        document.open();
 
        for (String pdfFile : pdfFiles) {
            PdfReader reader = new PdfReader(pdfFile);
            int totalPages = reader.getNumberOfPages();
            for (int page = 1; page <= totalPages; page++) {
                PdfImportedPage importedPage = copy.getImportedPage(reader, page);
                copy.addPage(importedPage);
            }
            reader.close();
        }
        document.close();
    }
 
}