将PDF的第一页显示为图像

Fah*_*kar 8 html pdf iframe image jsf-2

我正在创建Web应用程序,我以缩略图格式显示images/pdf.点击相应的图片/ pdf即可在新窗口中打开.

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

<iframe src="images/testes.pdf" width="800" height="200" />
Run Code Online (Sandbox Code Playgroud)

使用此我可以在Web浏览器中看到所有PDF.但是出于缩略图的目的,我想只显示PDF的第一页作为图像.

我试过了

 <h:graphicImage value="images/testes.pdf" width="800" height="200" />
Run Code Online (Sandbox Code Playgroud)

但它不起作用.知道怎么做到这一点?

更新1

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

<iframe src="#{PersonalInformationDataBean.myAttachmentString}" width="800" height="200" />
Run Code Online (Sandbox Code Playgroud)

更新2

为了缩略图,我正在使用的是

 <h:graphicImage height=200 width=200 value="...."> 
Run Code Online (Sandbox Code Playgroud)

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

希望我清楚我所期待的......

Kur*_*fle 7

我不确定所有浏览器是否都能<h:graphicImage value="some.pdf" ... /> 同样好地显示您嵌入的 PDF(通过 完成)。

将第一页提取为 PDF

如果您坚持使用 PDF,我建议您使用以下 2 个命令行工具中的一个来提取任何 PDF 的第一页:

  1. pdftk
  2. 幽灵脚本

两者都适用于 Linux、Mac OS X 和 Windows。

pdftk 命令

pdftk input.pdf cat 1 output page-1-of-input.pdf
Run Code Online (Sandbox Code Playgroud)

Ghostscript 命令

gs -o page-1-of-input.pdf -sDEVICE=pdfwrite -dPDFLastPage=1 input.pdf
Run Code Online (Sandbox Code Playgroud)

(在 Windows 上使用gswin32c.exegswin64c.exe代替gs。)

pdftk在页面提取方面比 Ghostscript 略快,但对于单个页面,这种差异可能可以忽略不计。 从最新发布的版本 v9.05 开始,前面的句子不再正确。我发现 Ghostscript(包括所有启动开销)需要约 1 秒才能从 756 页 PDF 规范中提取第一页,而 PDFTK 需要约 11 秒。

将第一页转换为 JPEG

如果您想确保即使是较旧的浏览器也能很好地显示您的第一页,请将其转换为 JPEG。Ghostscript 是你的朋友(ImageMagick 不能自己做,它无论如何都需要 Ghostscript 的帮助):

gs -o page-1-of-input-PDF.jpeg -sDEVICE=jpeg -dLastPage=1 input.pdf
Run Code Online (Sandbox Code Playgroud)

如果你需要第 33 页,你可以这样做:

gs -o page-33-of-input-PDF.jpeg -sDEVICE=jpeg -dFirstPage=33 -dLastPage33 input.pdf
Run Code Online (Sandbox Code Playgroud)

如果您需要一系列 PDF,例如第 17-23 页,请尝试以下操作:

gs -o page-16+%03d-of-input-PDF.jpeg -sDEVICE=jpeg -dFirstPage=17 -dLastPage23 input.pdf
Run Code Online (Sandbox Code Playgroud)

请注意,%03d符号随着处理的每个页面而增加,从 1 开始。因此您的第一个 JPEG 名称将是page-16+001-of-input-PDF.jpeg.

也许PNG更好?

请注意,JPEG 不是一种非常适合包含高黑白对比度和像文本页面那样锐利边缘的图像的格式。PNG 对此要好得多。

使用 Ghostscript 从第一个 PDF 页面创建 PNG 很容易:

gs -o page-1-of-input-PDF.png -sDEVICE=pngalpha -dLastPage=1 input.pdf
Run Code Online (Sandbox Code Playgroud)

在提取页面范围时,与 JPEG 一样的模拟选项是正确的。


Fah*_*kar 1

这是我用的

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();
}
Run Code Online (Sandbox Code Playgroud)