图片转PDF

图片转PDF小程序

代码

package com;

import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileOutputStream;

/**
 * 只需要根据情况更改FILEPATH 和 imagesPath
 */
public class Test06 {
    /**
     * FILEPATH 需要有一个E盘下的pdf文件夹 当然也可以是其它的 把这里改了就行
     */
    private static final String FILEPATH = "E:\\pdf\\";
    /**
     * imagesPath 图片存放位置 当然也可以是其它的 把这里改了就行
     */
    private static final String imagesPath ="E:\\note\\JavaSE\\01 Java语法基础(第一阶段)";

    @Test
    public void main65() {
        imagesToPdf("我的pdf文件", imagesPath);
    }
    /**
     * @param fileName   生成pdf文件
     * @param imagesPath 需要转换的图片路径的数组
     */
    public void imagesToPdf(String fileName, String imagesPath) {
        try {
            fileName = FILEPATH + fileName + ".pdf";
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            // 第一步:创建一个document对象。
            Document document = new Document();
            document.setMargins(0, 0, 0, 0);
            // 第二步:
            // 创建一个PdfWriter实例,
            PdfWriter.getInstance(document, new FileOutputStream(file));
            // 第三步:打开文档。
            document.open();
            // 第四步:在文档中增加图片。
            File files = new File(imagesPath);
            String[] images = files.list();
            int len = images.length;
            for (int i = 0; i < len; i++) {
                if (images[i].toLowerCase().endsWith(".bmp")
                        || images[i].toLowerCase().endsWith(".jpg")
                        || images[i].toLowerCase().endsWith(".jpeg")
                        || images[i].toLowerCase().endsWith(".gif")
                        || images[i].toLowerCase().endsWith(".png")) {
                    String temp = imagesPath + "\\" + images[i];
                    Image img = Image.getInstance(temp);
                    img.setAlignment(Image.ALIGN_CENTER);
                    img.scalePercent(100);
                    // 根据图片大小设置页面,一定要先设置页面,再newPage(),否则无效
                    document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
                    document.newPage();
                    document.add(img);
                }
            }
            // 第五步:关闭文档。
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

所需依赖

<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>
<!--随便找个测试依赖就行-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <version>2.7.4</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python提供了多种库和工具来进行图片PDF的操作。其中比较常用的是使用`PIL`(Python Imaging Library)库来处理图片,以及使用`reportlab`库来生成PDF文件。 下面是一个简单的示例代码,演示如何使用Python将多张图片换为PDF文件: ```python from PIL import Image from reportlab.pdfgen import canvas def images_to_pdf(image_paths, output_path): pdf = canvas.Canvas(output_path) for image_path in image_paths: image = Image.open(image_path) width, height = image.size pdf.setPageSize((width, height)) pdf.drawImage(image_path, 0, 0, width, height) pdf.showPage() pdf.save() # 调用示例 image_paths = ['image1.jpg', 'image2.jpg', 'image3.jpg'] output_path = 'output.pdf' images_to_pdf(image_paths, output_path) ``` 上述代码中,首先导入了`PIL`库中的`Image`模块和`reportlab`库中的`canvas`模块。然后定义了一个名为`images_to_pdf`的函数,该函数接受图片路径列表和输出路径作为参数。在函数内部,通过循环遍历图片路径列表,打开每张图片并获取其宽度和高度。然后使用`reportlab`库的`canvas`对象创建一个PDF文件,并设置页面大小为当前图片的宽度和高度。接着使用`drawImage`方法将图片绘制到PDF页面上,并调用`showPage`方法创建新的页面。最后调用`save`方法保存PDF文件。 你可以根据实际需求修改代码中的图片路径列表和输出路径,以及调整其他参数来满足你的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值