用java将pdf转换成jpg图片的代码

来源:互联网 发布:php简历项目描述 编辑:程序博客网 时间:2024/05/01 14:46
  1. package pdf;  
  2.   
  3. import java.awt.Image;  
  4. import java.awt.Rectangle;  
  5. import java.awt.image.BufferedImage;  
  6.   
  7. import java.io.File;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.io.RandomAccessFile;  
  11. import java.nio.ByteBuffer;  
  12. import java.nio.channels.FileChannel;  
  13. import javax.swing.SwingUtilities;  
  14. import com.sun.image.codec.jpeg.JPEGCodec;  
  15. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  16. import com.sun.pdfview.PDFFile;  
  17. import com.sun.pdfview.PDFPage;  
  18.   
  19. public class PdfToJpgTest {  
  20.     public static void setup() throws IOException {  
  21.   
  22.         // load a pdf from a byte buffer  
  23.         File file = new File(  
  24.                 "c://xxxxx.pdf");  
  25.         RandomAccessFile raf = new RandomAccessFile(file, "r");  
  26.         FileChannel channel = raf.getChannel();  
  27.         ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel  
  28.                 .size());  
  29.         PDFFile pdffile = new PDFFile(buf);  
  30.   
  31.         System.out.println("页数: " + pdffile.getNumPages());  
  32.   
  33.         for (int i = 1; i <= pdffile.getNumPages(); i++) {  
  34.             // draw the first page to an image  
  35.             PDFPage page = pdffile.getPage(i);  
  36.   
  37.             // get the width and height for the doc at the default zoom  
  38.             Rectangle rect = new Rectangle(00, (int) page.getBBox()  
  39.                     .getWidth(), (int) page.getBBox().getHeight());  
  40.   
  41.             // generate the image  
  42.             Image img = page.getImage(rect.width, rect.height, // width &  
  43.                                                                 // height  
  44.                     rect, // clip rect  
  45.                     null// null for the ImageObserver  
  46.                     true// fill background with white  
  47.                     true // block until drawing is done  
  48.                     );  
  49.   
  50.             BufferedImage tag = new BufferedImage(rect.width, rect.height,  
  51.                     BufferedImage.TYPE_INT_RGB);  
  52.             tag.getGraphics().drawImage(img, 00, rect.width, rect.height,  
  53.                     null);  
  54.             FileOutputStream out = new FileOutputStream(  
  55.                     "c://picture//"  
  56.                             + i + ".jpg"); // 输出到文件流  
  57.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  58.             encoder.encode(tag); // JPEG编码  
  59.   
  60.             out.close();  
  61.         }  
  62.   
  63.         // show the image in a frame  
  64.         // JFrame frame = new JFrame("PDF Test");  
  65.         // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  66.         // frame.add(new JLabel(new ImageIcon(img)));  
  67.         // frame.pack();  
  68.         // frame.setVisible(true);  
  69.     }  
  70.   
  71.     public static void main(final String[] args) {  
  72.         SwingUtilities.invokeLater(new Runnable() {  
  73.             public void run() {  
  74.                 try {  
  75.                     PdfToJpgTest.setup();  
  76.                 } catch (IOException ex) {  
  77.                     ex.printStackTrace();  
  78.                 }  
  79.             }  
  80.         });  
  81.     }  
  82.   
  83. }
  84. 这段代码必须的jar包,PDFRenderer.jar

http://blog.csdn.net/kiss_the_java/archive/2009/09/11/4538105.aspx

原创粉丝点击