この記事では、参照のために写真をPDFファイルに結合するJavaの特定のコードを共有しています。特定のコンテンツは次のとおりです
プログラムインターフェイス図:
コードリスト:
パッケージは画像をPDFファイルに結合します。 java.awt.event.actionevent;インポート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.swing.swing.swing.swing.swing.swing.swing.swing.swing.swing.swing.swing.swing.swing.swing javax.swing.jframe; Import javax.swing.jlabel; import javax.swing.joptionpane; Import javax.swing.jpanel; Import javax.swing.jtextfield; import javax.swing.filechooser.filenameextensionfilter; com.ithtpdf.textument; Import.thtpdf.textument; com.ithtpdf.text.documentexception;インポートcom.ithtpdf.text.image; Import com.ithtpdf.text.text.pagesize; Import com.ithtpdf.text.pdf.pdfwriter; SerialVersioniOD = 1657254256189721759L;最終的なプライベートストリングshuoming = "使用の命令:このプログラムの主な機能は、画像グループを同じPDFファイルに比例して拡大することです。 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_dir_dir_dir_save = null; jl_lujing_open = null; private 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(50); JL_DIR_OPEN( 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 = 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(jb_sevener(jb_pensevener(jb); save()); jb_ok.adddactioncactActioncactActionCactActionCactActionCactACRACTACTACTACTACTACTACTACTACTACTACTACTACTACTACTIACTIACTIACTIARSTENER(new begin()); jp.add(jb_open); jp.add(jb_save); jp.add(jb_ok);/*single-lineテキスト(パス表示)*/jt_dir_open = new jtextfield(); jt_dir_save = new jtextfield jtextfield(); jt_dir_open.setbounds(90、80、130、20); jt_dir_save.setbounds(460、80、130、 20); jt_dir_open.setedable(false); jt_dir_save.setedable(false); jp.add(jt_dir_open); jp.add(jt_dir_save);/*textbox(使用の指示)*/jtf = new jtextfield(shuoming); jtf.set(50,225、50,25 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_on_on_on_on_on_on_on_on); ActionListener {public void actionperformed(actionevent e){lujing_get lujing_get = new lujing_get(jf); // lujing_getオブジェクトを作成し、get group path dir_open = lujing_get.open_get(); jt_dir_open。 PDF*/プライベートクラスの保存によって生成された場所は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の合成のためのトランクイベント処理actionlistener {public void actionperformed(actionevent e){/*取得パスを正確に処理する| dir_open.equals( "")){joptionpane.showmessageialog(jf、 "画像グループとPDFの保存パスのパスを入力してください"、 "joptionpane.warning_message); if(fd.files()!= null){/*pdf命名ルール(接尾辞名を削除)*/if(dir_save.lastindexof( " wm_creatpdf(fd.files()、dir_save); // wm_creatpdfオブジェクトを作成してpdftry {pdf_creat.creatpdf(); //合成pdfファイル} catch(documentexception e1){// dodo catch e1.printstacktrace() TODOはCatch block e1.printstacktrace();} joptionpane.showmessageialog(jf、 "合成されたpdfファイル、場所:"+dir_save+"。pdf"、 "complete"、joptionpane.plain_message); (.jpg/.png/.bmp/.tif) jfc = new jfilechooser(); //「ファイルブラウザーの選択」オブジェクトJfc.setDialogTitle( "画像グループがあるフォルダーを選択してください); jfc.setFileSelectionMode(jfilechooser.directories_only); == returnVal){dir = jfc.getSelectedFile()。toString();} return dir; // return path} public string save_get(){string dir = ""; jfilechooser jfc = new jfilechooser(); //「select file browowser "object jfc.setdialogtitle(" PDFの合成位置を選択してください "); filenameextensionfilter filter = new new filenameextensionFilter( "pdf"、 "pdf"); jfc.setfilefilter(filter); int returnval = jfc.showsavedialog(jf); if(jfilechooser.approve_option == returnval){dir = jfc.getSelectedfile()。 // path}}/ * *画像ファイルを取得 * * * */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(f.listfiles()f.listfiles() 0)fs; elsereturn null;}}/ * * *ファイルフィルターを返し、ディレクトリの画像形式ファイルを返します。 file.getName(); if(name.endswith( "。jpg")|| name.endswith( "。png"))return true; elseif(name.endswith( "。bmp")|| name.endswith( "。tif")return true; elsereturn fals;}}}}} * * geneate pdf file "住所は:http://www.java2s.com/code/jar/i/i/i/iithtpdf.htm) * * * */class wm_creatpdf {最終的なフロートa4_weight = 595-60; private string dir_save = ""; public wm_creatpdf(file [] files、string dir_save){this.files = files; this.dir_save = dir_save;} public void createpdf()throws documentException、ioException {document document = new Document(pagesize.a4,30,30,30,30); //ドキュメントコンテナpdfwriter.getinstance(document、new fileoutputStream(dir_save+"); pdf");/createコンテナフロートパーセント= 100;フロートW、h; (a4_weight*100)/w; elseif((w <a4_weight)&&(h> a4_height))パーセント=(a4_height*100)/h; elseif((w> a4_weight)&&(h> a4_height)){percent =(a4_weight*)/w; h =(h = a4 (a4_height*100)/h;} img.scalepercent(percent); document.add(img);} close(); // close of contain} public static void main(string [] args){// todo automatic generated method stub new jiemian_mian();}}}}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。