@ -5,6 +5,7 @@ import com.lowagie.text.Font;
import com.lowagie.text.Image ;
import com.lowagie.text.Image ;
import com.lowagie.text.pdf.* ;
import com.lowagie.text.pdf.* ;
import com.lowagie.text.pdf.codec.TiffImage ;
import com.lowagie.text.pdf.codec.TiffImage ;
import org.apache.commons.lang3.StringUtils ;
import org.slf4j.Logger ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.slf4j.LoggerFactory ;
@ -14,6 +15,7 @@ import java.io.*;
import java.net.URL ;
import java.net.URL ;
import java.text.SimpleDateFormat ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.Date ;
import java.util.Iterator ;
import java.util.List ;
import java.util.List ;
/ * *
/ * *
@ -129,6 +131,287 @@ public class Jpg2PdfUtil {
writer . close ( ) ;
writer . close ( ) ;
}
}
}
}
public static boolean check ( String file ) {
PdfReader pdfReader = null ;
try {
File f = new File ( file ) ;
if ( f . isFile ( ) ) {
pdfReader = new PdfReader ( file ) ;
if ( pdfReader . getNumberOfPages ( ) ! = 0 ) {
return true ;
}
}
} catch ( Exception e ) {
} finally {
if ( null ! = pdfReader ) {
pdfReader . close ( ) ;
}
}
return false ;
}
public static void mulFile2One ( HttpServletResponse response , List < String > filePaths , String waterMarkName ) {
SimpleDateFormat fmt = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
Iterator < String > iterator = filePaths . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
String fileStr = iterator . next ( ) ;
File file = new File ( fileStr ) ;
if ( file . isFile ( ) ) {
boolean flag = check ( fileStr ) ;
if ( ! flag ) {
iterator . remove ( ) ;
System . out . println ( fmt . format ( new Date ( ) ) + ":" + file + "文件损坏" ) ;
}
} else {
iterator . remove ( ) ;
System . out . println ( fmt . format ( new Date ( ) ) + ":" + file + "文件不存在" ) ;
}
}
if ( ! filePaths . isEmpty ( ) ) {
// pdf合并工具类
Document document = null ;
PdfCopy copy = null ;
try {
document = new Document ( new PdfReader ( filePaths . get ( 0 ) ) . getPageSize ( 1 ) ) ;
copy = new PdfCopy ( document , response . getOutputStream ( ) ) ;
document . open ( ) ;
for ( String file : filePaths ) {
PdfReader reader = new PdfReader ( file ) ;
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
PdfReader pdfReader = null ;
//判断是否加水印
if ( StringUtils . isNotBlank ( waterMarkName ) ) {
setWatermark ( bos , reader , waterMarkName , null ) ;
pdfReader = new PdfReader ( bos . toByteArray ( ) ) ;
}
int n = reader . getNumberOfPages ( ) ;
for ( int j = 1 ; j < = n ; j + + ) {
document . newPage ( ) ;
PdfImportedPage page = null ;
if ( StringUtils . isNotBlank ( waterMarkName ) ) {
page = copy . getImportedPage ( pdfReader , j ) ;
} else {
page = copy . getImportedPage ( reader , j ) ;
}
copy . addPage ( page ) ;
}
if ( null ! = pdfReader ) {
pdfReader . close ( ) ;
}
reader . close ( ) ;
try {
bos . flush ( ) ;
bos . close ( ) ;
} catch ( Exception e ) {
//e.printStackTrace();
}
}
} catch ( Exception e ) {
//e.printStackTrace();
} finally {
if ( null ! = document ) {
document . close ( ) ;
}
}
}
}
public static void setWatermark ( ByteArrayOutputStream bos , PdfReader reader , String waterMarkName , String imgPath ) {
PdfStamper stamper = null ;
try {
stamper = new PdfStamper ( reader , bos ) ;
int total = reader . getNumberOfPages ( ) + 1 ;
PdfContentByte content ;
BaseFont base = BaseFont . createFont ( "STSong-Light" , "UniGB-UCS2-H" , BaseFont . EMBEDDED ) ;
PdfGState gs = new PdfGState ( ) ;
for ( int i = 1 ; i < total ; i + + ) {
content = stamper . getOverContent ( i ) ; // 在内容上方加水印
//加文字水印
if ( StringUtils . isNotBlank ( waterMarkName ) ) {
gs . setFillOpacity ( 0.3f ) ;
gs . setStrokeOpacity ( 0.3f ) ;
content . setGState ( gs ) ;
content . beginText ( ) ;
content . setColorFill ( Color . LIGHT_GRAY ) ;
content . setFontAndSize ( base , 50 ) ;
content . setTextMatrix ( 70 , 200 ) ;
String [ ] waterMarks = waterMarkName . split ( "&" ) ;
//v:距左 v1:距下 v2:
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 0 ] , 250 , 600 , 40 ) ;
/ * content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 0 ] , 250 , 200 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 0 ] , 800 , 600 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 0 ] , 800 , 200 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 0 ] , 250 , 1100 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 0 ] , 250 , 1500 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 0 ] , 800 , 1100 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 0 ] , 800 , 1500 , 40 ) ; * /
if ( waterMarks . length > 1 ) {
//v:距左 v1:距下 v2:
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 1 ] , 310 , 400 , 40 ) ;
/* content.showTextAligned(Element.ALIGN_CENTER, waterMarks[1], 310, 150, 40);*/
/ * content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 1 ] , 860 , 550 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 1 ] , 860 , 150 , 40 ) ; * /
/ * content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 1 ] , 300 , 900 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 1 ] , 300 , 1300 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 1 ] , 800 , 900 , 40 ) ;
content . showTextAligned ( Element . ALIGN_CENTER , waterMarks [ 1 ] , 800 , 1300 , 40 ) ; * /
}
content . endText ( ) ;
}
if ( StringUtils . isNotBlank ( imgPath ) ) {
Image image = Image . getInstance ( imgPath ) ;
image . setAbsolutePosition ( 200 , 206 ) ; // set the first background
image . scaleToFit ( 200 , 200 ) ;
content . addImage ( image ) ;
}
content . setColorFill ( Color . BLACK ) ;
content . setFontAndSize ( base , 8 ) ;
}
} catch ( IOException | DocumentException e ) {
e . printStackTrace ( ) ;
} finally {
try {
assert stamper ! = null ;
stamper . close ( ) ;
} catch ( DocumentException | IOException e ) {
e . printStackTrace ( ) ;
}
}
}
public static void mulFile2One3 ( HttpServletResponse response , List < String > filePaths , String filename ) {
String waterMarkName = "" ;
SimpleDateFormat fmt = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
Iterator < String > iterator = filePaths . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
String fileStr = iterator . next ( ) ;
File file = new File ( fileStr ) ;
if ( file . isFile ( ) ) {
boolean flag = check ( fileStr ) ;
if ( ! flag ) {
iterator . remove ( ) ;
System . out . println ( fmt . format ( new Date ( ) ) + ":" + file + "文件损坏" ) ;
}
} else {
iterator . remove ( ) ;
System . out . println ( fmt . format ( new Date ( ) ) + ":" + file + "文件不存在" ) ;
}
}
if ( ! filePaths . isEmpty ( ) ) {
// pdf合并工具类
Document document = null ;
PdfCopy copy = null ;
try {
response . reset ( ) ;
response . setCharacterEncoding ( "utf-8" ) ;
response . setContentType ( "application/pdf" ) ;
response . setHeader ( "Content-Disposition" , "attachment; filename=" + filename + ".pdf" ) ;
document = new Document ( new PdfReader ( filePaths . get ( 0 ) ) . getPageSize ( 1 ) ) ;
copy = new PdfCopy ( document , response . getOutputStream ( ) ) ;
document . open ( ) ;
for ( String file : filePaths ) {
PdfReader reader = new PdfReader ( file ) ;
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
PdfReader pdfReader = null ;
//判断是否加水印
if ( StringUtils . isNotBlank ( waterMarkName ) ) {
setWatermark ( bos , reader , waterMarkName , null ) ;
pdfReader = new PdfReader ( bos . toByteArray ( ) ) ;
}
int n = reader . getNumberOfPages ( ) ;
for ( int j = 1 ; j < = n ; j + + ) {
document . newPage ( ) ;
PdfImportedPage page = null ;
if ( StringUtils . isNotBlank ( waterMarkName ) ) {
page = copy . getImportedPage ( pdfReader , j ) ;
} else {
page = copy . getImportedPage ( reader , j ) ;
}
copy . addPage ( page ) ;
}
if ( null ! = pdfReader ) {
pdfReader . close ( ) ;
}
reader . close ( ) ;
try {
bos . flush ( ) ;
bos . close ( ) ;
} catch ( Exception e ) {
//e.printStackTrace();
}
}
} catch ( Exception e ) {
//e.printStackTrace();
} finally {
if ( null ! = document ) {
document . close ( ) ;
}
}
}
}
public static void mulFile2One2 ( HttpServletResponse response , List < String > filePaths ) {
SimpleDateFormat fmt = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
Iterator < String > iterator = filePaths . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
String fileStr = iterator . next ( ) ;
File file = new File ( fileStr ) ;
if ( file . isFile ( ) ) {
boolean flag = check ( fileStr ) ;
if ( ! flag ) {
iterator . remove ( ) ;
System . out . println ( fmt . format ( new Date ( ) ) + ":" + file + "文件损坏" ) ;
}
} else {
iterator . remove ( ) ;
System . out . println ( fmt . format ( new Date ( ) ) + ":" + file + "文件不存在" ) ;
}
}
if ( ! filePaths . isEmpty ( ) ) {
// pdf合并工具类
Document document = null ;
PdfCopy copy = null ;
try {
response . reset ( ) ;
response . setCharacterEncoding ( "utf-8" ) ;
response . setContentType ( "application/pdf" ) ;
response . setHeader ( "Content-Disposition" , "attachment; " + "pdf" ) ;
document = new Document ( new PdfReader ( filePaths . get ( 0 ) ) . getPageSize ( 1 ) ) ;
copy = new PdfCopy ( document , response . getOutputStream ( ) ) ;
document . open ( ) ;
for ( String file : filePaths ) {
PdfReader reader = new PdfReader ( file ) ;
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
PdfReader pdfReader = null ;
int n = reader . getNumberOfPages ( ) ;
if ( null ! = pdfReader ) {
pdfReader . close ( ) ;
}
reader . close ( ) ;
try {
bos . flush ( ) ;
bos . close ( ) ;
} catch ( Exception e ) {
//e.printStackTrace();
}
}
} catch ( Exception e ) {
//e.printStackTrace();
} finally {
if ( null ! = document ) {
document . close ( ) ;
}
}
}
}
/ * *
/ * *
* 在 不 改 变 图 片 形 状 的 同 时 , 判 断 , 如 果 h > w , 则 按 h 压 缩 , 否 则 在 w > h 或 w = h 的 情 况 下 , 按 宽 度 压 缩
* 在 不 改 变 图 片 形 状 的 同 时 , 判 断 , 如 果 h > w , 则 按 h 压 缩 , 否 则 在 w > h 或 w = h 的 情 况 下 , 按 宽 度 压 缩