我现在有个pdf文件,是由文字和图片组成的,我要实现在线浏览这个pdf文件。我用的是itext读取的pdf文件,不过我只能读纯文字,读不了图片。。怎么解决??不用itext也行,pdfbox、xpdf什么的都可以 只要能实现就行。。

解决方案 »

  1.   

    你可以把pdf转换成图片啊,用sun的PDFRenderer.jar
    public static void pdfToImage(File srcFile,String fileLocation,int no){
    RandomAccessFile raf;
    try {
    logger.debug("srcFile:" + srcFile.getAbsolutePath() + ",no:" + no);
    raf = new RandomAccessFile(srcFile, "r"); FileChannel channel = raf.getChannel();
    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    PDFFile pdffile = new PDFFile(buf);
    if(no < 1){
    no = 1;
    }
    if(no > pdffile.getNumPages()){
    throw new RuntimeException("无数据,已到达末页!");
    }
    // draw the first page to an image
    PDFPage page = pdffile.getPage(no);

    //get the width and height for the doc at the default zoom
    int width = (int)page.getBBox().getWidth();
    int height =(int)page.getBBox().getHeight();

    Rectangle rect = new Rectangle(0,0,width,height);
    int rotation=page.getRotation();
    Rectangle rect1=rect;
    if(rotation==90 || rotation==270)
    rect1=new Rectangle(0,0,rect.height,rect.width);

    //generate the image
    BufferedImage img = (BufferedImage)page.getImage(
    rect.width, rect.height, //width & height
    rect1, // clip rect
    null, // null for the ImageObserver
    true, // fill background with white
    true  // block until drawing is done
    ); ImageIO.write(img, "png", new File(fileLocation));
    clean(buf);
    channel.close();
    raf.close();

    catch (FileNotFoundException e1) {
    logger.error("file not found",e1);
    } catch (IOException e) {
    logger.error("io error",e);

    }
      

  2.   

    有一个Flase的插件,可以自接调用,把PDF显示出来。