依賴導入
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.15</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.15</version>
</dependency>
工具代碼
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
/**
* @author zh
* @data 2019/10/28 16:20
* 說明:TODO
*/
public class TestPDF {
public static void main(String[] args) throws IOException {
}
/**
* pdf拷貝到新文件,
* @param pdfFile
* @param newFile
* @param from 從第幾頁考
* @param end 考到第幾頁
*/
public static void copyPdfFile(String pdfFile, String newFile, int from, int end) {
Document document = null;
PdfCopy copy = null;
try {
PdfReader reader = new PdfReader(pdfFile);
int n = reader.getNumberOfPages();
if (end == 0) {
end = n;
}
ArrayList<String> savepaths = new ArrayList<String>();
String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\") + 1);
String savepath = staticpath + newFile;
savepaths.add(savepath);
document = new Document(reader.getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0)));
document.open();
for (int j = from; j <= end; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
document.close();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 將全部的pdf切割成一頁
*/
public static void cutOnePageFormPDF(String pdfFile, String toPath,String fileName){
Document document = null;
PdfCopy copy = null;
try {
PdfReader reader = new PdfReader(pdfFile);
int n = reader.getNumberOfPages();
for(int i=1; i <= n; i++){
document = new Document(reader.getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(toPath+fileName+"-"+i+".pdf"));
document.open();
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, i);
copy.addPage(page);
document.close();
}
System.out.println(n);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 多個pdf合併
*/
public static void sumPDFFile(String newfilePath, String... filePaths){
int length = filePaths.length;
Document document = null;
PdfCopy copy = null;
try {
if(length > 0){
document = new Document(new PdfReader(filePaths[0]).getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(newfilePath));
document.open();
for(int i=0; i < length; i++){
PdfReader reader = new PdfReader(filePaths[i]);
for(int j=1; j <= reader.getNumberOfPages(); j++){
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
document.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 將PDF按頁數每頁轉換成一個jpg圖片
* @param filePath
* @return
*/
public static List<String> pdfToImagePath(String filePath){
List<String> list = new ArrayList<>();
String fileDirectory = filePath.substring(0,filePath.lastIndexOf("."));//獲取去除後綴的文件路徑
String imagePath;
File file = new File(filePath);
try {
File f = new File(fileDirectory);
if(!f.exists()){
f.mkdir();
}
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for(int i=0; i<pageCount; i++){
// 方式1,第二個參數是設置縮放比(即像素)
// BufferedImage image = renderer.renderImageWithDPI(i, 296);
// 方式2,第二個參數是設置縮放比(即像素)
BufferedImage image = renderer.renderImage(i, 1.8f); //第二個參數越大生成圖片分辨率越高,轉換時間也就越長
imagePath = fileDirectory + "/"+i + ".jpg";
ImageIO.write(image, "PNG", new File(imagePath));
list.add(imagePath);
}
doc.close(); //關閉文件,否則該pdf文件會一直被佔用。
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* @Description pdf轉成一張圖片
* @created 2019年4月19日 下午1:54:13
* @param pdfFile
* @param outpath
*/
private static void pdf2multiImage(String pdfFile, String outpath) {
try {
InputStream is = new FileInputStream(pdfFile);
PDDocument pdf = PDDocument.load(is);
int actSize = pdf.getNumberOfPages();
List<BufferedImage> piclist = new ArrayList<BufferedImage>();
for (int i = 0; i < actSize; i++) {
BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i,130, org.apache.pdfbox.rendering.ImageType.RGB);
piclist.add(image);
}
yPic(piclist, outpath);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 將寬度相同的圖片,豎向追加在一塊兒 ##注意:寬度必須相同
* @param piclist 文件流數組
* @param outPath 輸出路徑
*/
public static void yPic(List<BufferedImage> piclist, String outPath) {// 縱向處理圖片
if (piclist == null || piclist.size() <= 0) {
System.out.println("圖片數組爲空!");
return;
}
try {
int height = 0, // 總高度
width = 0, // 總寬度
_height = 0, // 臨時的高度 , 或保存偏移高度
__height = 0, // 臨時的高度,主要保存每一個高度
picNum = piclist.size();// 圖片的數量
int[] heightArray = new int[picNum]; // 保存每一個文件的高度
BufferedImage buffer = null; // 保存圖片流
List<int[]> imgRGB = new ArrayList<int[]>(); // 保存全部的圖片的RGB
int[] _imgRGB; // 保存一張圖片中的RGB數據
for (int i = 0; i < picNum; i++) {
buffer = piclist.get(i);
heightArray[i] = _height = buffer.getHeight();// 圖片高度
if (i == 0) {
width = buffer.getWidth();// 圖片寬度
}
height += _height; // 獲取總高度
_imgRGB = new int[width * _height];// 從圖片中讀取RGB
_imgRGB = buffer.getRGB(0, 0, width, _height, _imgRGB, 0, width);
imgRGB.add(_imgRGB);
}
_height = 0; // 設置偏移高度爲0
// 生成新圖片
BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < picNum; i++) {
__height = heightArray[i];
if (i != 0) _height += __height; // 計算偏移高度
imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 寫入流中
}
File outFile = new File(outPath);
ImageIO.write(imageResult, "jpg", outFile);// 寫圖片
} catch (Exception e) {
e.printStackTrace();
}
}
}