当前位置:网站首页>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();
}
}
}
边栏推荐
- Publicity of the second batch of shortlisted enterprises! Annual Top100 smart network supplier selection
- 磁盤的結構
- Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
- Ansible basic configuration
- Common voting governance in Dao
- Disk structure
- Detailed explanation of agency mode
- YGG 近期游戏合作伙伴一览
- Collective search + drawing creation
- PyCharm 中出现Cannot find reference ‘imread‘ in ‘__init__.py‘
猜你喜欢
![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]

专科出身,2年进苏宁,5年跳阿里,论我是怎么快速晋升的?

Interviewer: you said you are proficient in redis. Have you seen the persistent configuration?
![leetcode:45. Jumping game II [classic greed]](/img/69/ac5ac8fe22dbb8ab719d09efda4a54.png)
leetcode:45. Jumping game II [classic greed]

How to extract dates from web pages?

CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)

Servlet details

Practice of hierarchical management based on kubesphere

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

华大04a工作模式/低功耗模式
随机推荐
磁盘的结构
性能测试工具wrk安装使用详解
[untitled]
leetcode_ one thousand three hundred and sixty-five
Notes on writing questions (18) -- binary tree: common ancestor problem
Filtered data analysis
Flutter 库冲突问题解决
降低pip到指定版本(通过cmp升级pip,在降低到原来版本)
第二批入围企业公示!年度TOP100智能网联供应商评选
Short video mall system, how does scroll view adapt to the remaining height of the page
权限想要细化到按钮,怎么做?
L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验
Docker 安装 MySQL 8.0,详细步骤
The process from troubleshooting to problem solving: the browser suddenly failed to access the web page, error code: 0x80004005, and the final positioning: "when the computer turns on the hotspot, the
Binary search tree template
04A interrupt configuration
【OpenCV 例程200篇】209. HSV 颜色空间的彩色图像分割
无心剑汉英双语诗003. 《书海》
In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
Servlet详解