当前位置:网站首页>img2pdf
img2pdf
2022-06-24 22:28:00 【jialan75】
One picture to 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 Screenshot 20220624140018.png");
float pageWidth = instance.getWidth();
float pageHeight = instance.getHeight();
Document document = new Document(new Rectangle(pageWidth, pageHeight));// Create a new document and set the page size
FileOutputStream outputStream = new FileOutputStream(outPath);// Create a new one pdf file ;
PdfWriter writer = PdfWriter.getInstance(document, outputStream);// Put the new pdf Assign a value to document
writer.setPdfVersion(PdfWriter.VERSION_1_5);
document.open();// open document file
PdfContentByte cb = writer.getDirectContent();
cb.addImage(instance,pageWidth,0,0,pageHeight,0,0);
outputStream.flush();// Close file
document.close();// Close file
outputStream.close();// Close file
} catch (Exception e) {
e.printStackTrace();
}
}
}
Multiple pictures to 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 Screenshot 20220624140018.png");
listImg.add("C:\\Users\\Administrator\\Desktop\\QQ Screenshot 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));// Create a new document and set the page size
FileOutputStream outputStream = new FileOutputStream(outPath);// Create a new one pdf file ;
PdfWriter writer = PdfWriter.getInstance(document, outputStream);// Put the new pdf Assign a value to document
writer.setPdfVersion(PdfWriter.VERSION_1_5);
document.open();// open document file
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();// Close file
document.close();// Close file
outputStream.close();// Close file
} catch (Exception e) {
e.printStackTrace();
}
}
}
边栏推荐
- 磁盤的結構
- Rotate the square array of two-dimensional array clockwise by 90 °
- Huada 04A operating mode / low power consumption mode
- What aspects should we start with in the feasibility analysis of dry goods?
- In the era of industrial Internet, there is no Internet in the traditional sense
- 产业互联网时代,并不存在传统意义上的互联网
- 04A中断的配置
- Yyds dry goods inventory junit5 learning II: assumptions class
- Double linked list implementation
- Flutter 如何使用在线转码工具将 JSON 转为 Model
猜你喜欢

Creating files, recursively creating directories
![leetcode:45. Jumping game II [classic greed]](/img/69/ac5ac8fe22dbb8ab719d09efda4a54.png)
leetcode:45. Jumping game II [classic greed]

如何比较两个或多个分布:从可视化到统计检验的方法总结

The profound meaning of unlimited ecological development in Poka -- Multidimensional Interpretation of parallel chain

NiO, bio, AIO

故障安全移动面板KTP900F Mobile下载程序提示无法下载,目标设备正在运行或未处于传输模式的解决办法

These map operations in guava have reduced my code by 50%

try-with-resources 中的一个坑,注意避让

Balanced binary search tree

Detailed installation and use of performance test tool wrk
随机推荐
DAO 中常见的投票治理方式
After Firefox drag and drop, Baidu search will always be opened by default. If it is a picture, the picture will be opened.
St Table + two points
PostMan工具介绍及安装使用
I really can't do it. After 00, I collapsed and wanted to leave
代理模式详解
leetcode:55. 跳跃游戏【经典贪心】
L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验
In the era of industrial Internet, there is no Internet in the traditional sense
04A interrupt configuration
img2pdf
YGG 近期游戏合作伙伴一览
socket done
How to grab the mobile phone bag for analysis? Fiddler artifact may help you!
软件设计的七大原则
Technology inventory: past, present and future of Message Oriented Middleware
Cannot find reference 'imread' in 'appears in pycharm__ init__. py‘
Ansible basic configuration
Kubevela v1.2 release: the graphical operation console velaux you want is finally here
Process communication mode