您好,登錄后才能下訂單哦!
本文實例為大家分享了Java將圖片組合成PDF文件的具體代碼,供大家參考,具體內(nèi)容如下
程序界面圖:
代碼清單:
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); /*標(biāo)簽*/ 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); //創(chuàng)建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); //創(chuàng)建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){ /*準(zhǔn)確處理所獲取的路徑*/ if(dir_save.equals("") || dir_open.equals("")){ JOptionPane.showMessageDialog(jf, "請輸入圖片組的路徑及PDF的保存路徑","警告",JOptionPane.WARNING_MESSAGE); }else{ File_deal fd = new File_deal(dir_open); //創(chuàng)建File_deal對象,將圖片組路徑下的所有圖片文件準(zhǔn)確加載 if(fd.files() != null){ /*準(zhǔn)確處理PDF的命名規(guī)則(去掉后綴名)*/ 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); //創(chuàng)建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(); //創(chuàng)建“選擇文件瀏覽器”對象 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(); //創(chuàng)建“選擇文件瀏覽器”對象 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; //標(biāo)準(zhǔn)A4的寬 final private float A4_height = 842-60; //標(biāo)準(zhǔn)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); //創(chuàng)建文檔容器 PdfWriter.getInstance(document,new FileOutputStream(dir_save+".pdf")); //創(chuàng)建編寫器(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(); //關(guān)閉容器 } public static void main(String[] args) { // TODO 自動生成的方法存根 new Jiemian_mian(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。