欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Java实现多张图片合并保存到pdf中

Java实现多张图片合并保存到pdf中

2025/3/14 20:29:55 来源:https://blog.csdn.net/m0_46487331/article/details/139284384  浏览:    关键词:Java实现多张图片合并保存到pdf中

Java实现多张图片合并保存到pdf中

1、依赖–maven
        <dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.24</version></dependency>
2、上代码
package com.hxlinks.hxiot.controller;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.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.thymeleaf.expression.Lists;import javax.imageio.ImageIO;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;public class PdfBoxMultipleImagesToPdf {public static void main(String[] args) {List<String> imagePaths = Arrays.asList("C:\\Users\\89131\\Desktop\\1.png", "C:\\Users\\89131\\Desktop\\2.png","C:\\Users\\89131\\Desktop\\3.png", "C:\\Users\\89131\\Desktop\\4.png");File outputPdf = new File("D:/output.pdf");try {createPdfFromImages(imagePaths, outputPdf);System.out.println("PDF with images created successfully.");} catch (IOException e) {e.printStackTrace();}}
//
//    public static void createPdfFromImages(List<String> imagePaths, File outputFile) throws IOException {
//        try (PDDocument document = new PDDocument()) {
//            for (String imagePath : imagePaths) {
//                // Load image
//                PDImageXObject image = LosslessFactory.createFromImage(document, ImageIO.read(new File(imagePath)));
//
//                // Create a new page and add it to the document
//                PDPage page = new PDPage(PDRectangle.A4);
//                document.addPage(page);
//
//                // Get the content stream for the page
//                try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
//                    // Define the position and size for the image on the page
//                    float imgWidth = image.getWidth();
//                    float imgHeight = image.getHeight();
//                    float margin = 50; // Example margin
//                    float x = (page.getMediaBox().getWidth() - imgWidth) / 2;
//                    float y = (page.getMediaBox().getHeight() - imgHeight) - margin;
//
//                    // Draw the image onto the page
//                    contentStream.drawImage(image, x, y, imgWidth, imgHeight);
//                }
//            }
//
//            // Save the final document
//            document.save(outputFile);
//        }
//    }public static void createPdfFromImages(List<String> imagePaths, File outputFile) throws IOException {try (PDDocument document = new PDDocument()) {float margin = 50f; // Define the marginfor (String imagePath : imagePaths) {// Load imagePDImageXObject image = LosslessFactory.createFromImage(document, ImageIO.read(new File(imagePath)));// Calculate scaling factor to fit image within the page minus margins, maintaining aspect ratiofloat imgWidth = image.getWidth();float imgHeight = image.getHeight();float pageWidthForImage = PDRectangle.A4.getWidth() - 2 * margin; // Subtracting total margin from widthfloat pageHeightForImage = PDRectangle.A4.getHeight() - 2 * margin; // Subtracting total margin from heightfloat scaleX = pageWidthForImage / imgWidth;float scaleY = pageHeightForImage / imgHeight;float scale = Math.min(scaleX, scaleY);// Resize the image dimensions with the scaleimgWidth *= scale;imgHeight *= scale;// Create a new page and add it to the documentPDPage page = new PDPage(PDRectangle.A4);document.addPage(page);// Get the content stream for the pagetry (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {// Calculate the centered position considering the total margin around the pagefloat x = (page.getMediaBox().getWidth() - imgWidth) / 2; // Centered without adding margin againfloat y = (page.getMediaBox().getHeight() - imgHeight) / 2; // Centered without adding margin again// Draw the scaled image onto the page at the correctly centered positioncontentStream.drawImage(image, x, y, imgWidth, imgHeight); // Apply margin here}}// Save the final documentdocument.save(outputFile);}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词