img2pdf

一张图片转pdf

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;

import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class ImgToPdf {
    public static void main(String[] args) {
        try {
            String outPath = "pdf.pdf";
            Image instance = Image.getInstance("C:\\Users\\Administrator\\Desktop\\QQ截图20220624140018.png");
            float pageWidth = instance.getWidth();
            float pageHeight = instance.getHeight();
            Document document = new Document(new Rectangle(pageWidth, pageHeight));//新建一个文档并且设置页面大小
            FileOutputStream outputStream = new FileOutputStream(outPath);//新建一个pdf文档;
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);//把新建的pdf 赋值给 document
            writer.setPdfVersion(PdfWriter.VERSION_1_5);
            document.open();//打开 document文档
            PdfContentByte cb = writer.getDirectContent();
            cb.addImage(instance,pageWidth,0,0,pageHeight,0,0);
            outputStream.flush();//关闭文件
            document.close();//关闭文件
            outputStream.close();//关闭文件
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

多张图片转pdf

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;

import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class ImgToPdf {
    public static void main(String[] args) {

        List<String> listImg = new ArrayList<String>();
        listImg.add("C:\\Users\\Administrator\\Desktop\\QQ截图20220624140018.png");
        listImg.add("C:\\Users\\Administrator\\Desktop\\QQ截图20220624140844.png");
        try {

            String outPath = "pdf.pdf";
            Image instance = Image.getInstance(listImg.get(0));
            float pageWidth = instance.getWidth();
            float pageHeight = instance.getHeight();
            Document document = new Document(new Rectangle(pageWidth, pageHeight));//新建一个文档并且设置页面大小
            FileOutputStream outputStream = new FileOutputStream(outPath);//新建一个pdf文档;
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);//把新建的pdf 赋值给 document
            writer.setPdfVersion(PdfWriter.VERSION_1_5);
            document.open();//打开 document文档
            PdfContentByte cb = writer.getDirectContent();
            cb.addImage(instance,pageWidth,0,0,pageHeight,0,0);
            for(int i=1;i<listImg.size();i++){
                Image instance2 = Image.getInstance(listImg.get(i));
                float pageWidth2= instance2.getWidth();
                float pageHeight2 = instance2.getHeight();
                document.setPageSize(new Rectangle(pageWidth2,pageHeight2));
                document.newPage();
                cb.addImage(instance2,pageWidth2,0,0,pageHeight2,0,0);
            }
            outputStream.flush();//关闭文件
            document.close();//关闭文件
            outputStream.close();//关闭文件
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值