Display first page of PDF as Image

后端 未结 3 1539
攒了一身酷
攒了一身酷 2021-02-01 10:03

I am creating web application where I am displaying images/ pdf in thumbnail format. Onclicking respective image/ pdf it get open in new window.

For PDF, I have (this is

3条回答
  •  一整个雨季
    2021-02-01 10:15

    This is what I used

    Document document = new Document();
    try {
        document.setFile(myProjectPath);
        System.out.println("Parsed successfully...");
    } catch (PDFException ex) {
        System.out.println("Error parsing PDF document " + ex);
    } catch (PDFSecurityException ex) {
        System.out.println("Error encryption not supported " + ex);
    } catch (FileNotFoundException ex) {
        System.out.println("Error file not found " + ex);
    } catch (IOException ex) {
        System.out.println("Error handling PDF document " + ex);
    }
    
    
    
    // save page caputres to file.
    float scale = 1.0f;
    float rotation = 0f;
    
    System.out.println("scale == " + scale);
    
    // Paint each pages content to an image and write the image to file
    InputStream fis2 = null;
    File file = null;
    for (int i = 0; i < 1; i++) {
        BufferedImage image = (BufferedImage) document.getPageImage(i,
        GraphicsRenderingHints.SCREEN,
        Page.BOUNDARY_CROPBOX, rotation, scale);
        RenderedImage rendImage = image;
        // capture the page image to file
        try {
            System.out.println("\t capturing page " + i);
            file = new File(myProjectActualPath + "myImage.png");
            ImageIO.write(rendImage, "png", file);
            fis2 = new BufferedInputStream(new FileInputStream(myProjectActualPath + "myImage.png"));
    
        } catch (IOException ioe) {
            System.out.println("IOException :: " + ioe);
        } catch (Exception e) {
            System.out.println("Exception :: " + e);
        }
        image.flush();
    }
    

提交回复
热议问题