이 기사는 참조를 위해 사진을 PDF 파일로 결합하는 특정 Java 코드를 공유합니다. 특정 내용은 다음과 같습니다
프로그램 인터페이스 다이어그램 :
코드 목록 :
패키지는 이미지를 PDF 파일로 결합합니다. 가져 오기 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.jfilchooSer; javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.filechooser.filenameeextensionfilter; import com.ithtpdf com.ithtpdf.text.documentexception; import com.ithtpdf.text.image; import com.ithtpdf.text.pagesize; import com.ithtpdf.text.pdf.pdf.pdfwriter;/ * * * * * * * * * * * * * * * * * */class jiemian_mian extrest jiemian jiemian jiemian jiemian. SerialVersionUid = 1657254256189721759L; 최종 개인 문자열 shuoming = "사용 지침 :이 프로그램의 주요 기능은 이미지 그룹을 동일한 PDF 파일로 비율로 스케일링하는 것입니다.“Winmin"; private String dir_open = "; public jframe Jf = null; null; null; JBUTTON JB_OPEN = NULL; 개인 JBUTTON JB_SAVE = NULL; 개인 JBUTTON JB_OK = NULL; 개인 JTEXTFIELD JT_DIR_OPEN = NULL; 개인 JTEXTFIELD JT_DIR_SAVE = NULL; Private JL_DIR_OPEN = NULL; PRIVET JLAVEL JLAVEL JLBEL JL_DIR_S. jl_lujing_open = null; private jlabel jlabel jlabel jl_lujing_save = null; private jtextfield jtf = null; public jiemian_mian () {jf = new Jframe ( "PDF 파일에 그림을 결합"); Jlabel ( "이미지 그룹이 위치한 폴더를 선택하십시오 :"); JL_DIR_SAVE = NEW JLABEL ( "PDF의 합성 위치를 선택하십시오 :"); JL_LUJing_Open = New Jlabel ( "Path :"); JL_LUJing_SAVE = NEW JLABEL ( "PATH : 50, 50, 50, 50, 50, 50, 20); JL_DIR_SAVE.SETBOUNDS (420, 50, 200, 20); 20); = New JBUTTON ( "START SYNGISES"); JB_OPEN.SETBOUNDS (230, 80, 65, 20); JB_SAVE.SETBOUNDS (600, 80, 65, 20); JB_OK.SETBOUNDS (310, 165, 90, 30); 저장 ()); jtextfield (); jt_dir_open.setbounds (90, 80, 130, 20); jt_dir_save.setbounds (460, 80, 130, 20); 50); jtf.setenabled (false); jp.add (jtf);/*main box*/jf.add (jp); jf.setsize (715,315); jf.setResizable (false); jf.setLocationRelativeTo (null); jf.setviseable (true); 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 (dir_open); pdf*/private class save에 의해 생성 된 위치는 ActionListener {public void actionperformed (actionEvent e) {lujing_get lujing_get = new lujing_get (jf); // lujing_get 객체를 생성하고 pdf dir_save =에 의해 생성 된 위치를 저장하는 경로를 가져옵니다. lujing_get.save_get (); dir_open.equals ( "")) {joptionpane.showmessagedialog (jf, "이미지 그룹의 경로와 pdf의 저장 경로를 입력하십시오", "경고", "경고", joptionpane.warning_message);} else {file_deal fd = new File_deal (dir_open); if (fd.files ()! = null) {/*pdf 이름 지정 규칙 (접미사 이름 제거)*/if (dir_save.lastIndexof ( ")! = (-1)) dir_save = dir_save.substring (0, dear_save.lastindexof ("); wm_creatpdf (fd.files (), dir_save); // pdftry {pdf_creat.creatpdf (); // pdf 파일 합성} catch (documentexception e1) {// 포획 된 블록 e1.printstacktrace (); TODO 자동 생성 캐치 블록 e1.printstacktrace ();} joptionpane.showMessagedIalog (jf, 합성 PDF 파일, "합성 PDF 파일, 위치 :"+dir_save+". pdf", "complete", joptionpane.plain_message (joptionpane.showmessagedialog); (.jpg/.png/.bmp/.tif)! ","경고 ", joptionpane.warning_message);}}}} class lujing_get {private jframe jf = null; public lujing_get (jframe jf) {this.jf = jf;} public string () {jfo us on gget () jfc = new JfileChooser (); // "파일 브라우저 선택"객체 jfc.setDialogTitle ( "이미지 그룹이있는 폴더를 선택하십시오"); == returnVal) {dir = jfc.getSelectedFile (). toString ();} return dir; // return path} public String save_get () {string dir = ""; filenameextensionFilter ( "pdf", "pdf"); jfc.setfilefilter (필터); int returnVal = jfc.showsavedialog (jf); if (jfilechooser.pack_option == returnval) {dipc.gget (jfc.get); tostring (); // return path}}/ * * 이미지 파일을 가져옵니다 * * */class file_deal {private string dir_open = ""; public file_deal (string dir_open) {this.dir_open = dir_open;} public file [] {file f = new file (dir_open); file fs [] = f.listfiles (new Phlistfiles); 0) fs; elsereturn null;}}/ * * * 파일 필터, 디렉토리에서 이미지 형식 파일을 반환 */클래스 photofilefilter는 filefilter {@overridepublic boolean accept (파일 파일) {// {file.isdirectory ())가 {끈 이름을 반환합니다. file.getName (); if (name.endswith ( ". jpg") || name.endswith ( ". png")) return true; elseif (name.endswith ( ". bmp") || name.endswith ( ". tif")) retud; elsereturn false;}}}}}}}}}}}}}}}}/ * * * reture pdf를 요구합니다. " 다운로드 주소는 다음과 같습니다. http://www.java2s.com/code/jar/i/i/iithtpdf.htm) * * */class wm_creatpdf {Final Private float a4_weight = 595-60; 개인 문자열 dir_save = ""; public wm_creatpdf (file [] files, string dir_save) {this.files = files; this.dir_save = dir_save;} public void creatpdf ()는 documentException, ioException {document document = new Document (pagesize.a4,30,30,30,30); // 문서 컨테이너 생성 pdfwriter.getInstance (document, new fileoutputStream (dir_save+"); // 문서 (pdf type) 컨테이너 float % = 100; float w, h; for (int i = 0; i <files.length; i ++) {im (a4_weight*100)/w; elseif ((w <a4_weight) && (h> a4_height)) % = (a4_height*100)/h; elseif ((w> a4_weight) && (h> a4_height)) { % = (a4_weight*100)/w; H*pcent)/100; if (h> a4_ height)/100; (a4_height*100)/h;} img.scalepercent ( %); docut 위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.