springboot使⽤itextpdf框架实现多个图⽚合成⼀个pdf⽂件以下两个⽅法引⼊头
import *;
import pdf.PdfWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.dering.ImageType;
import org.dering.PDFRenderer;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
1、图⽚转pdf⽂件
pom ⽂件引⼊
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
public File imageToPdf(List<String> imageUrllist, String mOutputPdfFileName) {
String TAG = "PdfManager";
Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
try {
for (int i = 0; i < imageUrllist.size(); i++) {
//                doc.add(new Paragraph("简单使⽤iText"));
Image png1 = (i));
float heigth = Height();
float width = Width();
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 (!ists()) {
mOutputPdfFile.deleteOnExit();
return null;
}
return mOutputPdfFile;
}
/**
* 第⼀种解决⽅案在不改变图⽚形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩 *
* @param h
* @param w
* @return
*/
private 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 = und(p2);
return p;
}
/**
* 第⼆种解决⽅案,统⼀按照宽度压缩这样来的效果是,所有图⽚的宽度是相等的,⾃我认为给客户的效果是最好的 *
* @param
*/
private int getPercent2(float h, float w) {
int p = 0;
springboot架构图
float p2 = 0.0f;
p2 = 530 / w * 100;
p = und(p2);
return p;
}
2、pdf 转图⽚
pom 引⼊
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
//经过测试,dpi为96,100,105,120,150,200中,105显⽰效果较为清晰,体积稳定,dpi越⾼图⽚体积越⼤
//⼀般电脑显⽰分辨率为96
public static final float DEFAULT_DPI=105;
/**pdf转图⽚
* @param pdfPath
*/
public  String pdfToImage(String pdfPath, HttpServletResponse res, HttpServletRequest request){ try{
if(pdfPath==null||"".equals(pdfPath)||!dsWith(".pdf"))
return null;
//图像合并使⽤参数
int width = 0; // 总宽度
int[] singleImgRGB; // 保存⼀张图⽚中的RGB数据
int shiftHeight = 0;
BufferedImage imageResult = null;//保存每张图⽚的像素值
//利⽤PdfBox⽣成图像
PDDocument pdDocument = PDDocument.load(new File(pdfPath));
PDFRenderer renderer = new PDFRenderer(pdDocument);
//循环每个页码
for(int i=0,NumberOfPages(); i<len; i++){
BufferedImage derImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);
int Height();
int Width();
if(i==0){//计算⾼度和偏移量
width=imageWidth;//使⽤第⼀张图⽚宽度;
//保存每页图⽚的像素值
imageResult= new BufferedImage(width, imageHeight*len, BufferedImage.TYPE_INT_RGB);                }else{
shiftHeight += imageHeight; // 计算偏移⾼度
}
singleImgRGB= RGB(0, 0, width, imageHeight, null, 0, width);
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 写⼊流中            }
pdDocument.close();
String outPath = place(".pdf", "_"+DEFAULT_DPI+".jpg");
File outFile = new File(outPath);
ImageIO.write(imageResult, "jpg", outFile);// 写图⽚
return outPath;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。