将PDF的第一页显示为图像 [英] Display first page of PDF as Image

查看:388
本文介绍了将PDF的第一页显示为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Web应用程序,在其中我以缩略图格式显示图像/pdf.单击相应的图像/pdf后,它将在新窗口中打开.

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

对于PDF,我有(这是新窗口的代码)

For PDF, I have (this is code of the new window)

<iframe src="images/testes.pdf" width="800" height="200" />

使用此工具,我可以在网络浏览器中查看所有PDF.但是出于缩略图的目的,我只想将PDF的第一页显示为图像.

Using this I can see all PDF in web browser. However for thumbnail purpose, I want to display only first page of PDF as an Image.

我尝试了

 <h:graphicImage value="images/testes.pdf" width="800" height="200" />

但是它不起作用.知道如何完成这项工作吗?

however it is not working. Any idea how to get this done?

我出于示例目的提供pdf文件的路径.但是我在数据库中有图像.实际上,我有如下代码.

I am providing path of pdf file for example purpose. However I have images in Database. In actual I have code as below.

<iframe src="#{PersonalInformationDataBean.myAttachmentString}" width="800" height="200" />

更新2

出于缩略图的目的,我使用的是

Update 2

For sake of thumbnail, what I am using is

 <h:graphicImage height=200 width=200 value="...."> 

但是我也需要为PDF实现相同的功能.

however I need to achieve same for PDF also.

希望我很清楚我的期望...

Hope I am clear what I am expecting...

推荐答案

这就是我所使用的

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();
}

这篇关于将PDF的第一页显示为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆