The company made a small optimization: Because some pictures are uploaded separately (more than 10), and then need to print, you need to provide a one-time direct printing, without having to save each picture in the print (the uploader and the print staff is not the same dial), so you want to be able to convert multiple images in a PDF file.
A tool class is used here:
<dependency> <groupId>com.itextpdf</groupId> <artifactid>itextpdf</ Artifactid> <version>5.5.10</version></dependency>
Overall, it's relatively simple:
PackageCom.example.zgz.demo;Importcom.itextpdf.text.Document;ImportCom.itextpdf.text.Image;ImportCom.itextpdf.text.Rectangle;ImportCom.itextpdf.text.pdf.PdfWriter;ImportJava.io.File;ImportJava.io.FileOutputStream;/** * @authorZhangguangze *@versionv1.0 * @project: Zgz * @description: This describes the use of classes * @copyright:? 2018 * @company: * @date 2018/5/16 14:23*/ Public classPdfutil {Private StaticString FILEPATH = "f:\\testpdf\\pdf\\"; /** * * @paramfileName * Generate PDF file *@paramImagespath * An array of picture paths that need to be converted*/ Public Static voidimagestopdf (String fileName, String imagespath) {Try{fileName= filepath+filename+ ". pdf"; File File=NewFile (fileName); //First step: Create a Document object. Document document =NewDocument (); Document.setmargins (0, 0, 0, 0); //Step Two://Create a PDFWriter instance,Pdfwriter.getinstance (document,Newfileoutputstream (file)); //Step Three: Open the document. Document.open (); //Fourth Step: Add a picture to the document. File files =NewFile (Imagespath); string[] Images=files.list (); intLen =images.length; for(inti = 0; i < Len; i++) { if(Images[i].tolowercase (). EndsWith (". bmp") || Images[i].tolowercase (). EndsWith (". jpg") || Images[i].tolowercase (). EndsWith (". jpeg") || Images[i].tolowercase (). EndsWith (". gif") || Images[i].tolowercase (). EndsWith (". png") ) {String temp= Imagespath + "\ \" +Images[i]; Image img=image.getinstance (temp); Img.setalignment (Image.align_center); //according to the picture Size setting page, must first set up the page, then NewPage (), otherwise invalidDocument.setpagesize (NewRectangle (Img.getwidth (), Img.getheight ())); Document.newpage (); Document.add (IMG); } } //Fifth Step: Close the document. Document.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public Static voidMain (string[] args) {String name= "20001543"; String Imagespath= "F:\\testpdf\\image"; Imagestopdf (name, Imagespath); }}
Java Multi-image conversion pdf