1. 依赖
< ! -- 图片转换pdf Start-- > < ! -- https: / / mvnrepository. com/ artifact/ com. itextpdf/ itextpdf -- > < dependency> < groupId> com. itextpdf< / groupId> < artifactId> itextpdf< / artifactId> < version> 5.5 .13 < / version> < / dependency> < ! -- https: / / mvnrepository. com/ artifact/ com. lowagie/ itext -- > < dependency> < groupId> com. lowagie< / groupId> < artifactId> itext< / artifactId> < version> 4.2 .1 < / version> < / dependency> < ! -- 图片转换pdf End-- >
2. 工具类抽取
package com. gblfy. ly. util; package com. sinosoft. cmiip. modular. taskbatch. util; import com. itextpdf. text. Document;
import com. itextpdf. text. Image;
import com. itextpdf. text. Rectangle;
import com. itextpdf. text. pdf. PdfWriter;
import org. slf4j. Logger;
import org. slf4j. LoggerFactory; import java. io. File;
import java. io. FileOutputStream;
import java. io. IOException;
import java. util. ArrayList;
public class PicToPdfUtil { private final static Logger logger = LoggerFactory. getLogger ( PicToPdfUtil. class ) ; public static void convert ( String source, String target) { Document document = new Document ( ) ; document. setMargins ( 0 , 0 , 0 , 0 ) ; FileOutputStream fos = null ; try { fos = new FileOutputStream ( target) ; PdfWriter. getInstance ( document, fos) ; document. open ( ) ; Image image = Image. getInstance ( source) ; float imageHeight = image. getScaledHeight ( ) ; float imageWidth = image. getScaledWidth ( ) ; Rectangle rectangle = new Rectangle ( imageWidth, imageHeight) ; document. setPageSize ( rectangle) ; image. setAlignment ( Image. ALIGN_CENTER) ; document. newPage ( ) ; document. add ( image) ; } catch ( Exception ioe) { logger. error ( "转换出现异常:" , ioe. getMessage ( ) ) ; } finally { document. close ( ) ; try { fos. flush ( ) ; fos. close ( ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } } logger. info ( "图片转换完成!!!" ) ; } public static void getPathFileList ( String picFilePath, String pdfFilePaths) { ArrayList< String> files = getFiles ( picFilePath) ; getFileName ( files, pdfFilePaths) ; } public static void getFileName ( ArrayList< String> files, String pdfFilePath) { for ( int i = 0 ; i < files. size ( ) ; i++ ) { String fileName = files. get ( i) ; String picFileName = fileName. substring ( fileName. lastIndexOf ( File. separator) + 1 ) ; String[ ] split = picFileName. split ( "\\." ) ; String pdfFileName = split[ 0 ] ; String pdfSuffix = ".pdf" ; convert ( fileName, pdfFilePath + pdfFileName + pdfSuffix) ; } } public static ArrayList< String> getFiles ( String picFilePath) { ArrayList< String> files = new ArrayList < String > ( ) ; File file = new File ( picFilePath) ; File[ ] fileList = file. listFiles ( ) ; for ( int i = 0 ; i < fileList. length; i++ ) { if ( fileList[ i] . isFile ( ) ) { files. add ( fileList[ i] . toString ( ) ) ; } } return files; } public static void main ( String[ ] args) { String picFilePath = "D:\\222\\pic\\" ; String pdfFilePath = "D:\\222\\pdf\\" ; getPathFileList ( picFilePath, pdfFilePath) ; }
}