欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java将图片组合成PDF文件的方法

程序员文章站 2024-02-11 13:36:28
本文实例为大家分享了java将图片组合成pdf文件的具体代码,供大家参考,具体内容如下 程序界面图: 代码清单: package 将图片组合成pdf文件;...

本文实例为大家分享了java将图片组合成pdf文件的具体代码,供大家参考,具体内容如下

程序界面图:

Java将图片组合成PDF文件的方法

代码清单:

package 将图片组合成pdf文件;

import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.io.file;
import java.io.filefilter;
import java.io.fileoutputstream;
import java.io.ioexception;


import javax.swing.jbutton;
import javax.swing.jfilechooser;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.joptionpane;
import javax.swing.jpanel;
import javax.swing.jtextfield;
import javax.swing.filechooser.filenameextensionfilter;


import com.itextpdf.text.document;
import com.itextpdf.text.documentexception;
import com.itextpdf.text.image;
import com.itextpdf.text.pagesize;
import com.itextpdf.text.pdf.pdfwriter;



/*
 * 绘制主界面以及处理按钮事件类——jiemian_main
 * 
 * */
class jiemian_mian extends jframe{

private static final long serialversionuid = 1657254256189721759l;


final private string shuoming = "
使用说明: 本程序的主要功能就是将图片组按比例缩放合成于同一pdf文件里。 ——winmin";
private string dir_open = "";
private string dir_save = "";

public jframe jf = null;
private jpanel jp = null;
private jbutton jb_open = null;
private jbutton jb_save =null;
private jbutton jb_ok = null;
private jtextfield jt_dir_open = null;
private jtextfield jt_dir_save = null;
private jlabel jl_dir_open = null;
private jlabel jl_dir_save = null;
private jlabel jl_lujing_open = null;
private jlabel jl_lujing_save = null;
private jtextfield jtf = null;

public jiemian_mian(){

jf = new jframe("将图片组合成pdf文件");
jp = new jpanel();
jp.setlayout(null);

/*标签*/
jl_dir_open = new jlabel("请选择图片组所在的文件夹:");
jl_dir_save = new jlabel("请选择pdf的合成位置:");
jl_lujing_open = new jlabel("路径:");
jl_lujing_save = new jlabel("路径:");
jl_dir_open.setbounds(50, 50, 200, 20);
jl_dir_save.setbounds(420, 50, 200, 20);
jl_lujing_open.setbounds(50, 80, 40, 20);
jl_lujing_save.setbounds(420, 80, 40, 20);
jp.add(jl_dir_open);
jp.add(jl_dir_save);
jp.add(jl_lujing_open);
jp.add(jl_lujing_save);

/*按钮*/
jb_open = new jbutton("浏览");
jb_save = new jbutton("浏览");
jb_ok = new jbutton("开始合成");
jb_open.setbounds(230, 80, 65, 20);
jb_save.setbounds(600, 80, 65, 20);
jb_ok.setbounds(310, 165, 90, 30);
jb_open.addactionlistener(new open());
jb_save.addactionlistener(new save());
jb_ok.addactionlistener(new begin());
jp.add(jb_open);
jp.add(jb_save);
jp.add(jb_ok);

/*单行文本(路径显示)*/
jt_dir_open = new jtextfield();
jt_dir_save = new jtextfield();
jt_dir_open.setbounds(90, 80, 130, 20);
jt_dir_save.setbounds(460, 80, 130, 20);
jt_dir_open.seteditable(false);
jt_dir_save.seteditable(false);
jp.add(jt_dir_open);
jp.add(jt_dir_save);

/*文本框(使用说明)*/
jtf = new jtextfield(shuoming);
jtf.setbounds(50,225, 615, 50);
jtf.setenabled(false);
jp.add(jtf);

/*主框*/
jf.add(jp);
jf.setsize(715,315);
jf.setresizable(false);
jf.setlocationrelativeto(null);
jf.setvisible(true);
jf.setdefaultcloseoperation(jframe.exit_on_close);

}


/*获取图片路径的按钮事件处理*/
private class open implements actionlistener{

public void actionperformed(actionevent e){

lujing_get lujing_get = new lujing_get(jf);
//创建lujing_get对象,获取图片组路径
dir_open = lujing_get.open_get();
jt_dir_open.settext(dir_open);
}
}

/*获取选择保存pdf所生成位置的路径的按钮事件处理*/
private class save implements actionlistener{

public void actionperformed(actionevent e){

lujing_get lujing_get = new lujing_get(jf);
//创建lujing_get对象,获取选择保存pdf所生成位置的路径
dir_save = lujing_get.save_get();
jt_dir_save.settext(dir_save);
}
}

/*合成pdf的按钮事件处理*/
private class begin implements actionlistener{

public void actionperformed(actionevent e){

/*准确处理所获取的路径*/
if(dir_save.equals("") || dir_open.equals("")){

joptionpane.showmessagedialog(jf, "请输入图片组的路径及pdf的保存路径","警告",joptionpane.warning_message);

}else{

file_deal fd = new file_deal(dir_open);
//创建file_deal对象,将图片组路径下的所有图片文件准确加载

if(fd.files() != null){

/*准确处理pdf的命名规则(去掉后缀名)*/
if(dir_save.lastindexof(".") != (-1))
dir_save = dir_save.substring(0, dir_save.lastindexof("."));

wm_creatpdf pdf_creat = new wm_creatpdf(fd.files(),dir_save);
//创建wm_creatpdf对象,生成pdf

try {
pdf_creat.creatpdf();
//合成pdf文件
} catch (documentexception e1) {
// todo 自动生成的 catch 块
e1.printstacktrace();
} catch (ioexception e1) {
// todo 自动生成的 catch 块
e1.printstacktrace();
}

joptionpane.showmessagedialog(jf,"已合成pdf文件,所在位置:"+dir_save+".pdf",
"完成",joptionpane.plain_message);

}else 
joptionpane.showmessagedialog(jf, "该文件夹下没有图片格式文件(.jpg/.png/.bmp/.tif)!",
"警告",joptionpane.warning_message);

} 
}
}

}



class lujing_get {

private jframe jf = null;

public lujing_get(jframe jf){

this.jf = jf;
}

public string open_get(){

string dir = "";

jfilechooser jfc = new jfilechooser();
//创建“选择文件浏览器”对象
jfc.setdialogtitle("请选择图片组所在的文件夹");
jfc.setfileselectionmode(jfilechooser.directories_only);
int returnval = jfc.showopendialog(jf);
if(jfilechooser.approve_option == returnval){

dir = jfc.getselectedfile().tostring();

}

return dir; //返回路径
}

public string save_get(){

string dir = "";

jfilechooser jfc = new jfilechooser();
//创建“选择文件浏览器”对象
jfc.setdialogtitle("请选择pdf的合成位置");
filenameextensionfilter filter = new filenameextensionfilter("pdf","pdf");
jfc.setfilefilter(filter);
int returnval = jfc.showsavedialog(jf);
if(jfilechooser.approve_option == returnval){

dir = jfc.getselectedfile().tostring();
}

return dir; //返回路径
}
}



/*
 *获取图片文件
 * 
 * */
class file_deal{

private string dir_open = "";

public file_deal(string dir_open){

this.dir_open = dir_open;
}

public file[] files(){

file f = new file(dir_open);
file fs[] = f.listfiles(new photofilefilter());

if(fs.length != 0)

return fs;
else
return null;
} 
}



/*
 * 
 * 文件过滤器,将所在目录下的图片格式文件返回
 * */
class photofilefilter implements filefilter{


@override
public boolean accept(file file) {
// todo 自动生成的方法存根

if(file.isdirectory())
return false;
else{

string name = file.getname();
if(name.endswith(".jpg") || name.endswith(".png"))
return true;
else
if(name.endswith(".bmp") || name.endswith(".tif"))
return true;
else
return false;
}
}
}



/*
 * 生成pdf文件(此类要外部引用“itextpdf.jar”,下载地址为:http://www.java2s.com/code/jar/i/itextpdf.htm)
 * 
 * */
class wm_creatpdf {


final private float a4_weight = 595-60;
//标准a4的宽
final private float a4_height = 842-60;
//标准a4的高
private file[] files = null; 
private string dir_save ="";

public wm_creatpdf(file[] files,string dir_save){

this.files = files;
this.dir_save = dir_save;
}

public void creatpdf() throws documentexception, ioexception{

document document = new document(pagesize.a4,30,30,30,30);
//创建文档容器
pdfwriter.getinstance(document,new fileoutputstream(dir_save+".pdf"));
//创建编写器(pdf类型)
document.open();
//打开容器

float percent=100;
float w,h;
for(int i=0;i<files.length;i++){

image img = image.getinstance(files[i].getcanonicalpath());

/*处理图片缩放比例*/
w = img.getwidth();
h = img.getheight();
if((w > a4_weight)&&(h < a4_height))
percent = (a4_weight*100)/w ;
else
if((w < a4_weight)&&(h > a4_height))
percent = (a4_height*100)/h;
else
if((w > a4_weight)&&(h > a4_height)){


percent = (a4_weight*100)/w ;
h = (h*percent)/100;
if(h > a4_height)
percent = (a4_height*100)/h;
}

img.scalepercent(percent);
document.add(img);
}

document.close();
//关闭容器
}


public static void main(string[] args) {
// todo 自动生成的方法存根

new jiemian_mian();


}


}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。