JAVA 将图片转换成pdf文件
2018-01-09 21:09
387 查看
/**
* 将图片转换成pdf文件
*imgFilePath 需要被转换的img所存放的位置。 例如imgFilePath="D:\\projectPath\\55555.jpg";
*pdfFilePath 转换后的pdf所存放的位置 例如pdfFilePath="D:\\projectPath\\test.pdf";
* @param image
* @return
* @throws IOException
*/
public boolean imgToPdf(String imgFilePath, String pdfFilePath)throws IOException {
File file=new File(imgFilePath);
if(file.exists()){
Document document = new Document();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pdfFilePath);
PdfWriter.getInstance(document, fos);
// 添加PDF文档的某些信息,比如作者,主题等等
document.addAuthor("arui");
document.addSubject("test pdf.");
// 设置文档的大小
document.setPageSize(PageSize.A4);
// 打开文档
document.open();
// 写入一段文字
//document.add(new Paragraph("JUST TEST ..."));
// 读取一个图片
Image image = Image.getInstance(imgFilePath);
float imageHeight=image.getScaledHeight();
float imageWidth=image.getScaledWidth();
int i=0;
while(imageHeight>500||imageWidth>500){
image.scalePercent(100-i);
i++;
imageHeight=image.getScaledHeight();
imageWidth=image.getScaledWidth();
System.out.println("imageHeight->"+imageHeight);
System.out.println("imageWidth->"+imageWidth);
}
image.setAlignment(Image.ALIGN_CENTER);
// //设置图片的绝对位置
// image.setAbsolutePosition(0, 0);
// image.scaleAbsolute(500, 400);
// 插入一个图片
document.add(image);
} catch (DocumentException de) {
System.out.println(de.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
document.close();
fos.flush();
fos.close();
return true;
}else{
return false;
}
}
* 将图片转换成pdf文件
*imgFilePath 需要被转换的img所存放的位置。 例如imgFilePath="D:\\projectPath\\55555.jpg";
*pdfFilePath 转换后的pdf所存放的位置 例如pdfFilePath="D:\\projectPath\\test.pdf";
* @param image
* @return
* @throws IOException
*/
public boolean imgToPdf(String imgFilePath, String pdfFilePath)throws IOException {
File file=new File(imgFilePath);
if(file.exists()){
Document document = new Document();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pdfFilePath);
PdfWriter.getInstance(document, fos);
// 添加PDF文档的某些信息,比如作者,主题等等
document.addAuthor("arui");
document.addSubject("test pdf.");
// 设置文档的大小
document.setPageSize(PageSize.A4);
// 打开文档
document.open();
// 写入一段文字
//document.add(new Paragraph("JUST TEST ..."));
// 读取一个图片
Image image = Image.getInstance(imgFilePath);
float imageHeight=image.getScaledHeight();
float imageWidth=image.getScaledWidth();
int i=0;
while(imageHeight>500||imageWidth>500){
image.scalePercent(100-i);
i++;
imageHeight=image.getScaledHeight();
imageWidth=image.getScaledWidth();
System.out.println("imageHeight->"+imageHeight);
System.out.println("imageWidth->"+imageWidth);
}
image.setAlignment(Image.ALIGN_CENTER);
// //设置图片的绝对位置
// image.setAbsolutePosition(0, 0);
// image.scaleAbsolute(500, 400);
// 插入一个图片
document.add(image);
} catch (DocumentException de) {
System.out.println(de.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
document.close();
fos.flush();
fos.close();
return true;
}else{
return false;
}
}
相关文章推荐
- JAVA 将图片转换成pdf文件
- java将图片缩放实现类(能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换)
- c#,将pdf文件转换成图片文件。
- java仿百度文库-使用SWFTools转换pdf文件
- 用java将pdf转换成jpg图片的代码
- 基于java的图片文件格式转换和线性缩放
- 用IrfanView将图片文件转换为PDF文件(由图片创建PDF文件) .
- JAVA中实现图片对象转换成图片文件和字节数组(Byte[ ])的方法讨论
- java导出pdf文件(插入图片及信息)
- java仿百度文库-使用SWFTools转换pdf文件
- winForm 读取pdf文件并,转换成图片
- java 生成pdf文件,添加图片
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- java仿百度文库-使用SWFTools转换pdf文件
- 基于java的图片文件格式转换和线性缩放
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- 基于Java的图片文件格式转换和线性缩放(1)
- 基于java的图片文件格式转换和线性缩放
- java导出pdf文件(插入图片及信息)
- 基于java的图片文件格式转换和线性缩放