<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.tkk7.com/sxyx2008/謝謝合作!!!

    雪山飛鵠

    溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.tkk7.com/sxyx2008/謝謝合作!!!

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
    package effective.java;

    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.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 void setup() throws IOException {

            
    // load a pdf from a byte buffer
            File file = new File("D:\\workspace\\word2pdf\\src\\tests.doc.pdf");
            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);

            System.out.println(
    "頁數: " + pdffile.getNumPages());

            String getPdfFilePath 
    = System.getProperty("user.dir"+ "\\pdfPicFile";

            System.out.println(
    "getPdfFilePath is  :" + getPdfFilePath);

            
    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(00, (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, 
    00, rect.width, rect.height,
                        
    null);

                
    // 輸出到文件流
                FileOutputStream out = new FileOutputStream(getPdfFilePath + "\\"
                        
    + i + ".jpg");
                System.out.println(
    "成功保存圖片到:" + getPdfFilePath + "\\" + i + ".jpg");

                
    /*
                 * JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                 * encoder.encode(tag); // JPEG編碼 out.close();
                 
    */

                JPEGImageEncoder encoder 
    = JPEGCodec.createJPEGEncoder(out);
                JPEGEncodeParam param2 
    = encoder.getDefaultJPEGEncodeParam(tag);
                param2.setQuality(1f, 
    false);// 1f是提高生成的圖片質量
                encoder.setJPEGEncodeParam(param2);
                encoder.encode(tag); 
    // JPEG編碼
                out.close();

            }

            
    // show the image in a frame
            
    // JFrame frame = new JFrame("PDF Test");
            
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
    // frame.add(new JLabel(new ImageIcon(img)));
            
    // frame.pack();
            
    // frame.setVisible(true);
        }

        
    public static void main(final String[] args) {
            SwingUtilities.invokeLater(
    new Runnable() {
                
    public void run() {
                    
    try {
                        PdfToJpgTest.setup();
                    } 
    catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }

    }
    依賴的jar包
    PDFRenderer.jar
    posted on 2011-06-17 11:34 雪山飛鵠 閱讀(3378) 評論(2)  編輯  收藏 所屬分類: javase

    Feedback

    # re: java轉換pdf to jpg 2011-10-19 11:54 web
    sun.awt.image.ImageFormatException: Unsupported color conversion request
    at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
    at sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:119)
    at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)  回復  更多評論
      

    # re: java轉換pdf to jpg[未登錄] 2016-07-12 12:30 1
    111  回復  更多評論
      

    主站蜘蛛池模板: 亚洲天堂一区二区三区| 精品国产_亚洲人成在线高清| 久久久久亚洲精品日久生情 | 国产男女猛烈无遮挡免费视频网站 | 亚洲精品免费网站| 999国内精品永久免费视频| 亚洲免费在线视频观看| 亚洲免费观看网站| 亚洲国产最大av| 免费无码黄网站在线观看| 亚洲人成色在线观看| 可以免费观看的一级毛片| 四虎成人精品国产永久免费无码 | 国产精品亚洲午夜一区二区三区| 国产片AV片永久免费观看| 亚洲日韩看片无码电影| 免费成人av电影| a级精品九九九大片免费看| 久久亚洲国产视频| av无码免费一区二区三区| 亚洲精品456人成在线| 国产成人免费a在线视频app | 久久精品一本到99热免费| 亚洲三级视频在线观看| 在线观看亚洲免费| 一级毛片在线免费视频| 亚洲大片在线观看| 成人免费看片又大又黄| 一级毛片试看60分钟免费播放 | 亚洲AV成人噜噜无码网站| 国产大片线上免费看| 中文毛片无遮挡高清免费| 亚洲手机中文字幕| 免费国产小视频在线观看| 青柠影视在线观看免费高清| 亚洲中文无码a∨在线观看| www国产亚洲精品久久久| 亚洲免费精彩视频在线观看| 亚洲日本在线电影| 亚洲AV永久无码精品| 永久中文字幕免费视频网站|