Categories
html iframe image jsf-2 pdf

Display first page of PDF as Image

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 code of the new window)

<iframe src="https://www.faqcode4u.com/faq/412523/images/testes.pdf" width="800" height="200" />

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.

I tried

 <h:graphicImage value="https://www.faqcode4u.com/faq/412523/images/testes.pdf" width="800" height="200" />

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

Update 1

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" />

Update 2

For sake of thumbnail, what I am using is

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

however I need to achieve same for PDF also.

Hope I am clear what I am expecting…

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