This article shares the specific code of Java combining pictures into PDF files for your reference. The specific content is as follows
Program interface diagram:
Code list:
package combines images into PDF files; 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.ithtpdf.text.Document;import com.ithtpdf.text.DocumentException;import com.ithtpdf.text.Image;import com.ithtpdf.text.PageSize;import com.ithtpdf.text.pdf.PdfWriter;/* * Draw the main interface and handle button event class - Jiemian_main * * */class Jiemian_mian extends JFrame{private static final long serialVersionUID = 1657254256189721759L;final private String shuoming = "Instructions for use: The main function of this program is to scale the image groups into the same PDF file in proportion. ―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("Combining pictures into PDF files");jp = new JPanel();jp.setLayout(null);/*Tag*/jl_dir_open = new JLabel("Please select the folder where the image group is located: ");jl_dir_save = new JLabel("Please select the synthesis location of the PDF: ");jl_lujing_open = new JLabel("Path: ");jl_lujing_save = new JLabel("Path: ");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);/*button*/jb_open = new JButton("Browse");jb_save = new JButton("Browse");jb_ok = new JButton("Start synthesis");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);/*Single-line text (path display)*/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);/*Textbox (Instructions for use)*/jtf = new JTextField(shuoming);jtf.setBounds(50,225, 615, 50);jtf.setEnabled(false);jp.add(jtf);/*Main box*/jf.add(jp);jf.setSize(715,315);jf.setResizable(false);jf.setLocationRelativeTo(null);jf.setVisible(true);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}/*Button event processing to get the image path*/private class Open implements ActionListener{public void actionPerformed(ActionEvent e){Lujing_get lujing_get = new Lujing_get(jf);//Create Lujing_get object and get the image group path dir_open = lujing_get.open_get();jt_dir_open.setText(dir_open);}}/*Get button event processing that selects the path to save the location generated by the PDF*/private class Save implements ActionListener{public void actionPerformed(ActionEvent e){Lujing_get lujing_get = new Lujing_get(jf);//Create Lujing_get object and gets the path to save the location generated by the PDF dir_save = lujing_get.save_get();jt_dir_save.setText(dir_save);}}/*Trunk event processing for synthesizing PDF*/private class Begin implements ActionListener{public void actionPerformed(ActionEvent e){/*Accurately handle the acquired path*/if(dir_save.equals("") || dir_open.equals("")){JOptionPane.showMessageDialog(jf, "Please enter the path of the image group and the PDF's saving path", "Warning", JOptionPane.WARNING_MESSAGE);}else{File_deal fd = new File_deal(dir_open);//Create File_deal object to load all image files in the image group path accurately if(fd.files() != null){/*Accurately handle PDF naming rules (remove the suffix name)*/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);//Create WM_CreatPDF object to generate PDFtry {pdf_creat.creatPDF();//Synthesize PDF file} catch (DocumentException e1) {// TODO automatically generated catch block e1.printStackTrace();} catch (IOException e1) {// TODO automatically generated catch block e1.printStackTrace();}JOptionPane.showMessageDialog(jf,"Synthesized PDF file, location: "+dir_save+".pdf","Complete",JOptionPane.PLAIN_MESSAGE);}else JOptionPane.showMessageDialog(jf, "There is no image format file in this folder (.jpg/.png/.bmp/.tif)!","Warning",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();//Create the "Select File Browser" object jfc.setDialogTitle("Please select the folder where the image group is");jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);int returnVal = jfc.showOpenDialog(jf);if(JFileChooser.APPROVE_OPTION == returnVal){dir = jfc.getSelectedFile().toString();}return dir; //Return path}public String save_get(){String dir = "";JFileChooser jfc = new JFileChooser();//Create the "Select File Browser" object jfc.setDialogTitle("Please select the synthesis location of the 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; //Return path}}/* *Get the image file* * */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;elsereturn null;} }/* * * File filter, return the image format file in the directory * */class PhotoFileFilter implements FileFilter{@Overridepublic boolean accept(File file) {// TODO automatic generated method stub if(file.isDirectory()) return false;else{String name = file.getName();if(name.endsWith(".jpg") || name.endsWith(".png"))return true;elseif(name.endsWith(".bmp") || name.endsWith(".tif"))return true;elsereturn false;}}}/* * Generate PDF file (this class requires external reference to "itextpdf.jar", download address is: http://www.java2s.com/Code/Jar/i/i/iithtpdf.htm) * * */class WM_CreatPDF {final private float A4_weight = 595-60;//The width of standard A4 final private float A4_height = 842-60;//The high 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);//Create document container PdfWriter.getInstance(document,new FileOutputStream(dir_save+".pdf"));//Create writer (PDF type) document.open();//Open container float percent=100;float w,h;for(int i=0;i<files.length;i++){Image img = Image.getInstance(files[i].getCanonicalPath());/*process image scaling*/w = img.getWidth();h = img.getHeight();if((w > A4_weight)&&(h < A4_height))percent = (A4_weight*100)/w ;elseif((w < A4_weight)&&(h > A4_height))percent = (A4_height*100)/h;elseif((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();//Close the container}public static void main(String[] args) {// TODO automatic generated method stub new Jiemian_mian();}} The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.