import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.imageio.ImageIO;
import javax.swing.SwingUtilities;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class PdfToJpgTest {
public static String pdfPath = "d://pdf//";
public static String jpgPath = "d://jpg//";
public static void setup() throws IOException {
// load a pdf from a byte buffer
File dir = new File(PdfToJpgTest.pdfPath);
File[] fs = dir.listFiles();
for (int a = 0; a < fs.length; a++) {
File file = fs[a];
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
channel.size());
PDFFile pdffile = new PDFFile(buf);
int pageCounts = pdffile.getNumPages();
System.out.println("页数: " + pageCounts);
System.out.println("PdfToJpgTest.jpgPath is :"
+ PdfToJpgTest.jpgPath);
for (int i = 1; i <= pdffile.getNumPages(); i++) {
// draw the first page to an image
PDFPage page = pdffile.getPage(i);
// get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
// generate the image
Image img = page.getImage(rect.width, rect.height, // width &
// height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height,
null);
// 输出到文件流
FileOutputStream out = new FileOutputStream(
PdfToJpgTest.jpgPath + "\\" + i + ".jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag);
param.setQuality(1f, false);// 1f是提高生成的图片质量
encoder.setJPEGEncodeParam(param);
encoder.encode(tag); // JPEG编码
out.close();
}
// 合并图片至一张图片
File d = new File(PdfToJpgTest.jpgPath);
File list[] = d.listFiles();
if (pageCounts > 0) {
BufferedImage Image = ImageIO.read(list[0]);
int width = Image.getWidth();// 图片宽度
int height = Image.getHeight();// 图片高度
BufferedImage ImageNew = new BufferedImage(width, height
* pageCounts, BufferedImage.TYPE_INT_RGB);
for (int j = 1; j <= pageCounts; j++) {
File jpgFile = new File(PdfToJpgTest.jpgPath + j + ".jpg");
Image = ImageIO.read(jpgFile);
// 从图片中读取RGB
int[] ImageArray = new int[width * height];
ImageArray = Image.getRGB(0, 0, width, height, ImageArray,
0, width);
ImageNew.setRGB(0, height * (j - 1), width, height,
ImageArray, 0, width);// 拼装图片
// 处理一张删除一张
jpgFile.delete();
}
File outFile = new File(PdfToJpgTest.jpgPath
+ file.getName().substring(0,
file.getName().lastIndexOf(".")) + ".jpg");
ImageIO.write(ImageNew, "jpg", outFile);// 写图片
System.out.println(file.getName() + "图片生成成功!");
}
}
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
PdfToJpgTest.setup();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}