Import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import java.net. URL; import Org. apache. log4j. logger; import COM. lowagie. text. document; import COM. lowagie. text. extends entexception; import COM. lowagie. text. image; import COM. lowagie. text. utilities; import com.lowagie.text. using writer; import com.lowagie.text.pdf. randomaccessfileorarray; import COM. lowagie. text. P DF. codec. export image;/***** <PRE> * convert normal image format to PDF format * </PRE> **/public class image2pdf {Private Static logger = logger. getlogger (image2pdf. class); public static Boolean convert (string oldpath, string newpath) {logger.info ("converted from file [" + oldpath + "] to [" + newpath + "] --- start! "); // Create a Document Object document DOC = new document (); try {// define the location of the output file using writer. getinstance (Doc, new fileoutputstream (newpath); // enable Doc. open (); If (! Oldpath. endswith (". TIF ") {image JPG = image. getinstance (oldpath); // path of the original image // float heigth = JPG. getheight (); float width = JPG. getwidth (); // reasonably compress, h> W, and press W to compress; otherwise, press W to compress int percent = getpercent (heigth, width); // set the image to center and display jpg. setalignment (image. middle); // display the image proportion by percentage if (width> 1024 | heigth> 786) {JPG. scalepercent (percent);} Doc. add (JPG);} else // tiff multi-page {object localobject1 = NULL; object localobje CT2 = NULL; image localimage1 = NULL; Url paramurl = utilities. tourl (oldpath); try {If (paramurl. getprotocol (). equals ("file") {localobject2 = paramurl. getFile (); localobject2 = utilities. unescapeurl (string) localobject2); localobject1 = new randomaccessfileorarray (string) localobject2);} else {localobject1 = new randomaccessfileorarray (paramurl);} int pagenums = custom image. getnumberofpages (randomacce Ssfileorarray) localobject1); If (pagenums> 0) {for (INT I = 1; I <= pagenums; I ++) {localobject2 = custom image. gettiffimage (randomaccessfileorarray) localobject1, I); image JPG = (image) localobject2; // obtain the Image Height float heigth = JPG. getheight (); float width = JPG. getwidth (); // reasonably compress, h> W, and press W to compress; otherwise, press W to compress int percent = getpercent (heigth, width); // set the image to center and display jpg. setalignment (image. middle); // display the image proportion by percentage if (width> 1024 | heigth> 786) {JPG. scalepercent (percent) ;}doc. Add (JPG) ;}} if (localobject1! = NULL) (randomaccessfileorarray) localobject1). Close ();} finally {If (localobject1! = NULL) (randomaccessfileorarray) localobject1 ). close () ;}} catch (filenotfoundexception e) {logger. error ("converting from file [" + oldpath + "] to [" + newpath + "] failed !, Cause: "+ E. getmessage (); E. printstacktrace ();} catch (incluentexception e) {logger. error ("converting from file [" + oldpath + "] to [" + newpath + "] failed !, Cause: "+ E. getmessage (); E. printstacktrace ();} catch (ioexception e) {logger. error ("converting from file [" + oldpath + "] to [" + newpath + "] failed !, Cause: "+ E. getmessage (); E. printstacktrace ();} finally {If (Doc! = NULL) {Doc. Close () ;}} logger.info ("convert from file [" + oldpath + "] to [" + newpath + "] --- end! "); Return true;}/*** when the first solution does not change the image shape, it determines that if h> W, It is compressed by H, otherwise, compress the data by the width ** @ Param H * @ Param w * @ return */public static int getpercent (float H, float W) {int p = 0; float P2 = 0.0f; If (h> W) {P2 = 210/H * 279 ;} else {P2 = 210/W * 279;} p = math. round (P2); Return P;}/*** second solution, which compresses data according to the width, the width of all images is equal ** @ Param ARGs */public static int getpercent2 (float H, float W) {int p = 0; float P2 = 0.0f; p2 = 530/W * 100; P = math. round (P2); Return P;} public static void main (string [] ARGs) {convert ("D: \ Bak \ 20121207 \ document4.tif", "D: \ Bak \ 20121207 \ document4.pdf ");}}