当前位置:网站首页>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();
}
}
}
边栏推荐
- PostMan工具介绍及安装使用
- SAP interface debug setting external breakpoints
- 04A中断的配置
- 01--- conditions for interference of two trains of waves at the meeting place
- KT6368A蓝牙双模透传芯片软件版本选型说明
- 波卡生态发展不设限的奥义——多维解读平行链
- A pit in try with resources
- Flutter 如何使用在线转码工具将 JSON 转为 Model
- Development trend and path of SaaS industry in China
- 性能测试工具wrk安装使用详解
猜你喜欢

Junior college background, 2 years in Suning, 5 years in Ali. How can I get promoted quickly?

985 test engineer is hanged. Who is more important in terms of education and experience?

如何抓手机的包进行分析,Fiddler神器或许能帮到您!

虚拟人的产业发展现状

Filtered data analysis

Huada 04A operating mode / low power consumption mode

What aspects should we start with in the feasibility analysis of dry goods?

How to extract dates from web pages?

代理模式详解

Want to be a test leader, do you know these 6 skills?
随机推荐
如何抓手机的包进行分析,Fiddler神器或许能帮到您!
Practice of hierarchical management based on kubesphere
降低pip到指定版本(通过cmp升级pip,在降低到原来版本)
代理模式详解
学习笔记23--多传感器信息融合基础理论(上)
Filtered data analysis
Raspberry pie preliminary use
磁盘的结构
Find the maximum value in each tree row [extension of one of the hierarchical traversals]
leetcode1720_ 2021-10-14
try-with-resources 中的一个坑,注意避让
虚拟人的产业发展现状
Machine learning: linear regression
Heartless sword Chinese English bilingual poem 003 The sea of books
L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验
985测试工程师被吊打,学历和经验到底谁更重要?
Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
零代码即可将数据可视化应用到企业管理中
Redis+caffeine two-level cache enables smooth access speed
[200 opencv routines] 209 Color image segmentation in HSV color space