springboot 使用itextpdf 框架實現多個圖片合成一個pdf文件

如下兩個方法引入頭java

import com.lowagie.text.*; import com.lowagie.text.pdf.PdfWriter; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.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;

 

一、圖片轉pdf文件spring

pom 文件引入apache

<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 { PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName)); doc.open(); for (int i = 0; i < imageUrllist.size(); i++) { doc.newPage(); // doc.add(new Paragraph("簡單使用iText"));
                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; }

/**
* 第一種解決方案 在不改變圖片形狀的同時,判斷,若是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 = Math.round(p2);
return p;
}

/**
* 第二種解決方案,統一按照寬度壓縮 這樣來的效果是,全部圖片的寬度是相等的,自我認爲給客戶的效果是最好的
*
* @param
*/
private int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
}
 

二、pdf 轉圖片測試

pom 引入spa

<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)||!pdfPath.endsWith(".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,len=pdDocument.getNumberOfPages(); i<len; i++){ BufferedImage image=renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB); int imageHeight=image.getHeight(); int imageWidth=image.getWidth(); if(i==0){//計算高度和偏移量
                    width=imageWidth;//使用第一張圖片寬度; //保存每頁圖片的像素值
                    imageResult= new BufferedImage(width, imageHeight*len, BufferedImage.TYPE_INT_RGB); }else{ shiftHeight += imageHeight; // 計算偏移高度
 } singleImgRGB= image.getRGB(0, 0, width, imageHeight, null, 0, width); imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 寫入流中
 } pdDocument.close(); String outPath = pdfPath.replace(".pdf", "_"+DEFAULT_DPI+".jpg"); File outFile = new File(outPath); ImageIO.write(imageResult, "jpg", outFile);// 寫圖片

            return outPath; }catch (Exception e) { e.printStackTrace(); } return null; }
相關文章
相關標籤/搜索