当前位置:网站首页>img2pdf
img2pdf
2022-06-24 19:35:00 【jialan75】
一张图片转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();
}
}
}
边栏推荐
- 嵌入式开发:技巧和窍门——干净地从引导加载程序跳转到应用程序代码
- Want to be a test leader, do you know these 6 skills?
- Common voting governance in Dao
- Process communication mode
- Excel layout
- Heartless sword Chinese English bilingual poem 003 The sea of books
- 性能测试工具wrk安装使用详解
- Balanced binary search tree
- Two implementation methods of stack
- St Table + two points
猜你喜欢

排查到解决问题的过程:浏览器突然无法访问网页,错误代码:0x80004005,最终定位:“电脑打开热点,电脑就不能上网了”

Flutter 库冲突问题解决

Redis+caffeine two-level cache enables smooth access speed

KT6368A蓝牙双模透传芯片软件版本选型说明

Yida technology signed a contract with seven wolves to help the digital transformation of "Chinese men's wear leader"

华大04a工作模式/低功耗模式

Want to be a test leader, do you know these 6 skills?

Huada 04A operating mode / low power consumption mode

Main steps of system test

What aspects should we start with in the feasibility analysis of dry goods?
随机推荐
干货丨产品的可行性分析要从哪几个方面入手?
leetcode1720_ 2021-10-14
Double linked list implementation
Ansible basic configuration
Minimum spanning tree based on Kruskal
KT6368A蓝牙芯片的主从机之前透传功能说明,2.4G跳频自动连接
波卡生态发展不设限的奥义——多维解读平行链
Filtered data analysis
Excel layout
Flutter: Unsupported value: false/true
想当测试Leader,这6项技能你会吗?
故障安全移动面板KTP900F Mobile下载程序提示无法下载,目标设备正在运行或未处于传输模式的解决办法
学习笔记23--多传感器信息融合基础理论(上)
磁盤的結構
好想送对象一束花呀
AQS源码分析
Flutter: Unsupported value: false/true
Multithreaded finalization
Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)