当前位置: 首页 > news >正文

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);}}
}
http://www.lryc.cn/news/357094.html

相关文章:

  • 揭秘智慧校园:可视化技术引领教育新篇章
  • 基础9 探索图形化编程的奥秘:从物联网到工业自动化
  • RPC-----RCF
  • StarRocks中,这些配置项是表属性的一部分
  • Activity->Activity生命周期
  • 乐鑫ESP串口驱动安装,安装cp210x驱动
  • Django缓存
  • Python 元组
  • JAVA面试题大全(十八)
  • 如何利用Firebase Hosting来托管网站
  • 揭秘“循环消费”模式:消费即收益,购物新体验
  • 图片怎样在线改像素大小?电脑快速修改图片大小的方法
  • SELINUX=enforcing时无法启动httpd服务的解决方案(semanage命令以及setroubleshoot-server插件的妙用)
  • 【C++】list的使用方法和模拟实现
  • 【物联网实战项目】STM32C8T6+esp8266/mqtt+dht11+onenet+uniapp
  • Pyhton 二叉树层级遍历
  • Flutter 中的 FadeTransition 小部件:全面指南
  • 缓存存储器:性能提升的关键
  • 『大模型笔记』工程师的LLMs简介!
  • Vue中的常用指令
  • 百度页面奔跑的白熊html、css
  • Day-02面向对象
  • Sentinel-2 哨兵二号数据介绍及下载
  • 阿里智能信息数据挖掘复盘
  • Flutter中图片是怎么在flutter上呈现出来的?
  • 使用 CNN 训练自己的数据集
  • 自动控制: 最小二乘估计(LSE)、加权最小二乘估计(WLS)和线性最小方差估计
  • 基于VMware安装Linux虚拟机
  • 6、phpjm混淆解密和php反序列化
  • Codeforces Round 909 (Div. 3) E. Queue Sort(模拟 + 贪心之找到了一个边界点)