JAva pdf生成ピクチャ
4787 ワード
ステップ1:pomのインポート
ステップ2:
commons-logging
commons-logging
1.2
org.apache.pdfbox
fontbox
2.0.15
org.apache.pdfbox
pdfbox
2.0.15
ステップ2:
package com.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
public class Test2 {
public static void main(String[] args) {
//pdf2multiImage("H:\\test\\zhaofengdeng\\3.pdf","H:\\test\\zhaofengdeng\\3.jpg");
test();
}
public static void test() {
List imageList = pdfToImagePath("H:\\test\\zhaofengdeng\\2.pdf");
Iterator iterator = imageList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
/**
* PDF jpg
*
* @param filePath
* @return
*/
public static List pdfToImagePath(String filePath) {
List 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.25f); // ,
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 piclist = new ArrayList();
for (int i = 0; i < actSize; i++) {
BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i, 130, ImageType.RGB);
piclist.add(image);
}
yPic(piclist, outpath);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* , ## :
*
* @param piclist
* @param outPath
*/
public static void yPic(List 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 imgRGB = new ArrayList(); // 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();
}
}
}