1.话不多说,直接上代码
package com.e9z.archivesManagement.rpc.service.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.e9z.archivesManagement.common.utils.PropertyUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import javax.servlet.http.HttpServletRequest;
public class PdfUtilImg {
public static File Pdf(List<String> imageUrllist, String mOutputPdfFileName) {
Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
try{
PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
doc.open();
for (int i = 0; i < imageUrllist.size(); i++) {
doc.newPage();
Image png1 = Image.getInstance(imageUrllist.get(i));
float heigth = png1.getHeight();
float width = png1.getWidth();
int percent = getPercent2(heigth, width);
png1.setAlignment(Image.MIDDLE);
png1.scalePercent(percent+3);
doc.add(png1);
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (DocumentException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
doc.close();
}
File mOutputPdfFile = new File(mOutputPdfFileName);
if (!mOutputPdfFile.exists()) {
mOutputPdfFile.deleteOnExit();
return null;
}
return mOutputPdfFile;
}
public static int getPercent(float h, float w) {
int p = 0;
float p2 = 0.0f;
if (h > w) {
p2 = 297 / h * 100;
}else {
p2 = 210 / w * 100;
}
p = Math.round(p2);
return p;
}
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 File imgOfPdf(String filepath,String filepathW,List<String> imageUrllist) {
boolean result = true;
String pdfWPath=null;
File fileOut=null;
String pdfUrl = filepath+(new Date().getTime())+".pdf";
if(PdfUtilImg.createDirectory(filepath)){
try {
if (result == true) {
File file = PdfUtilImg.Pdf(imageUrllist, pdfUrl);
file.createNewFile();
}
}catch (IOException e){
result=false;
e.printStackTrace();
}
}
if(PdfUtilImg.createDirectory(filepathW)){
try {
if(result){
String SyPath ="C:\Users\Administrator\Desktop\cscscscscs\shuiyin.png";
pdfWPath=filepathW+(new Date().getTime())+"w.pdf";
new addWaterMarkUtil().addWaterMark(pdfUrl, pdfWPath, "", SyPath, 500, 50, 250, 400);
}
}catch (Exception e){
result = false;
e.printStackTrace();
}
}
if(result&&pdfWPath!=null){
fileOut=new File(pdfUrl);
}
return fileOut;
}
public static Boolean createDirectory(String filePath){
Boolean result=true;
try {
File file=new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
}catch (Exception e){
result=false;
e.printStackTrace();
}
return result;
}
}
2.如何使用
public static void main(String[] rags) throws Exception {
String filepath="C:\Users\Administrator\Desktop\cscscscscs";
String filepathW="C:\Users\Administrator\Desktop\cscscscscs\shuiyin";
ArrayList<String> imageUrllist=new ArrayList<>();
imageUrllist.add("C:\Users\Administrator\Desktop\cscscscscs\1581323858059.jpg");
File result=PdfUtilImg.imgOfPdf(filepath,filepathW,imageUrllist);
}
3.下面附上给pdf添加水印的工具类,有需要的自己可以参考一下
package com.e9z.archivesManagement.rpc.service.util;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.e9z.archivesManagement.common.base.BaseResult;
import com.e9z.common.utils.ObjectUtils;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class addWaterMarkUtil {
public BaseResult addWaterMark(String srcFile, String destFile, String text, String imgFile, int textWidth,
int textHeight, int imgWidth, int imgHeight) throws IOException, DocumentException {
BaseResult result = new BaseResult();
PdfReader reader = new PdfReader(srcFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
String path = this.getClass().getResource("/").getPath();
BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
String waterText = text;
Image image = null;
if (!StringUtils.isBlank(imgFile)) {
image = Image.getInstance(imgFile);
image.setAbsolutePosition(imgWidth, imgHeight);
image.scaleToFit(100, 150);
for (int i = 1; i < total; i++) {
content = stamper.getOverContent(i);
if (image != null) {
content.addImage(image);
}
}
}
int j = waterText.length();
char c = 0;
int high = 0;
high = 50;
content = stamper.getOverContent(total - 1);
if (ObjectUtils.isNotNull(text)) {
content.beginText();
content.setColorFill(BaseColor.BLACK);
content.setFontAndSize(font, 15);
content.setTextMatrix(textWidth, textHeight);
content.showTextAligned(Element.ALIGN_LEFT, text, textWidth, textHeight, 0);
content.endText();
}
stamper.close();
result.markSuccess("添加水印到成功",destFile,null);
System.out.print("===" + srcFile + "===添加水印到==" + destFile + "==成功=====");
return result;
}
}