当前位置:网站首页>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();
}
}
}
边栏推荐
猜你喜欢
![leetcode:45. Jumping game II [classic greed]](/img/69/ac5ac8fe22dbb8ab719d09efda4a54.png)
leetcode:45. Jumping game II [classic greed]
![leetcode:515. Find the maximum value in each tree row [brainless BFS]](/img/87/1926d783fb6f8d8439213d86b5da40.png)
leetcode:515. Find the maximum value in each tree row [brainless BFS]

You are using pip version 21.1.2; however, version 22.1.2 is available

软件设计的七大原则

Balanced binary search tree

降低pip到指定版本(通過PyCharm昇級pip,在降低到原來版本)

Embedded development: tips and tricks -- clean jump from boot loader to application code

无心剑汉英双语诗003. 《书海》

985测试工程师被吊打,学历和经验到底谁更重要?

Detailed explanation of agency mode
随机推荐
无心剑汉英双语诗003. 《书海》
Object. Defineproperty and reflect Fault tolerance of defineproperty
Resolving the conflict problem of the flutter Library
SAP interface debug setting external breakpoints
一个女孩子居然做了十年硬件。。。
Notes on writing questions (18) -- binary tree: common ancestor problem
如何抓手机的包进行分析,Fiddler神器或许能帮到您!
Zero code can apply data visualization to enterprise management
Ideal L9, new trend of intelligent cockpit
Publicity of the second batch of shortlisted enterprises! Annual Top100 smart network supplier selection
磁盤的結構
leetcode:55. 跳跃游戏【经典贪心】
Flutter: Unsupported value: false/true
理想L9,智能座舱新潮流
AQS源码分析
Raspberry pie preliminary use
解决dataframe报错ValueError: Cannot take a larger sample than population when ‘replace=False‘
DP problem set
权限想要细化到按钮,怎么做?
Docker 安装 Redis-5.0.12,详细步骤