当前位置:网站首页>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();
}
}
}
边栏推荐
- 壹沓科技签约七匹狼,助力「中国男装领导者」数字化转型
- 嵌入式开发:技巧和窍门——干净地从引导加载程序跳转到应用程序代码
- 是真干不过00后,给我卷的崩溃,想离职了...
- You are using pip version 21.1.2; however, version 22.1.2 is available
- I really want to send a bunch of flowers
- L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验
- Notes on writing questions (18) -- binary tree: common ancestor problem
- LINQ query collection class introductory cases Wulin expert class
- 代理模式详解
- 关于自动控制原理资料更新
猜你喜欢

刷题笔记(十八)--二叉树:公共祖先问题

Raspberry pie preliminary use

60 个神级 VS Code 插件!!

Flutter-使用 typedef的注意事项

PyCharm 中出现Cannot find reference ‘imread‘ in ‘__init__.py‘

Réduire le PIP à la version spécifiée (mettre à jour le PIP avec pycharm avant de le réduire à la version originale)

一个女孩子居然做了十年硬件。。。
![Find the maximum value in each tree row [extension of one of the hierarchical traversals]](/img/5b/81ff20b61c0719ceb6873e44878859.png)
Find the maximum value in each tree row [extension of one of the hierarchical traversals]

DAO 中常见的投票治理方式

华大04a工作模式/低功耗模式
随机推荐
The logic of "Ali health" has long changed
L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验
软件设计的七大原则
First order model realizes photo moving (with tool code) | machine learning
是真干不过00后,给我卷的崩溃,想离职了...
Short video mall system, how does scroll view adapt to the remaining height of the page
Flutter 如何使用在线转码工具将 JSON 转为 Model
How to grab the mobile phone bag for analysis? Fiddler artifact may help you!
解决dataframe报错ValueError: Cannot take a larger sample than population when ‘replace=False‘
PyCharm 中出现Cannot find reference ‘imread‘ in ‘__init__.py‘
Binary search tree template
TCP RTT测量妙计
代理模式详解
Docker 安装 Redis-5.0.12,详细步骤
磁盤的結構
Summary of papers on traveling salesman problem (TSP)
try-with-resources 中的一个坑,注意避让
Two implementation methods of stack
Balanced binary search tree
leetcode:55. Jumping game [classic greed]