下边是官方下载的例子 传说中应该是没有问题的 但是我一使用得到的img就是反向的是 注意这个反向是位置没变 但是字颠倒了。。。。
那位大侠给小弟知道一下 万分感激啊!
import java.awt.Color;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Date;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class ConvertPdfToImg {
public static void changePdfToImg(String pdfpath, String despath) {
try {
File file = new File(pdfpath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
0, channel.size());
PDFFile pdffile = new PDFFile(buf);
for (int i = 1; i <= pdffile.getNumPages(); i++) {
PDFPage page = pdffile.getPage(i);
Rectangle2D rect = new Rectangle(0, 0,
((int) page.getBBox() .getWidth()),
((int) page.getBBox().getHeight()));
Image img = page.getImage((int)rect.getWidth(), (int)rect.getHeight(), rect,
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
// BufferedImage bufferImage = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
// Graphics2D g2 = bufferImage.createGraphics();
// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// PDFRenderer renderer = new PDFRenderer(page,g2,new Rectangle(0,0,500,500),null,Color.RED);
// page.waitForFinish();
// renderer.run();
BufferedImage bufferImage = new BufferedImage((int)rect.getWidth(), (int)rect.getHeight(),BufferedImage.TYPE_INT_RGB);
bufferImage.getGraphics().drawImage(img, 0, 0, Color.WHITE,null);
FileOutputStream out = new FileOutputStream(despath + i + ".jpg"); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bufferImage); // JPEG编码
out.close();
}
channel.close();
raf.close();
unmap(buf);//如果要在转图片之后删除pdf,就必须要这个关闭流和清空缓冲的方法
// JFrame frame = new JFrame("PDF Test");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.add(new JLabel(new ImageIcon(despath+"1.jpg")));
// frame.pack();
// frame.setVisible(true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void unmap(final Object buffer) {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
Method getCleanerMethod = buffer.getClass().getMethod(
"cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod
.invoke(buffer, new Object[0]);
cleaner.clean();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
}
public static void main(String[] args) {
long start = new Date().getTime();
// TODO code application logic here
changePdfToImg("F:/孔乙己.pdf", "F:/img/");
System.out.println(new Date().getTime()-start);
}
}
结果如下