<
groupId
>
org.icepdf.os
</
groupId
>
<
artifactId
>
icepdf-core
</
artifactId
>
<
version
>
6.2.2
</
version
>
<
exclusions
>
<
exclusion
>
<
groupId
>
javax.media
</
groupId
>
<
artifactId
>
jai_core
</
artifactId
>
</
exclusion
>
</
exclusions
>
</
dependency
>
排除
jai_core
是因为在导入包时会报错:jai_core missing,直接排除即可。
import org.icepdf.core.pobjects.Document
import org.icepdf.core.pobjects.Page
import org.icepdf.core.util.GraphicsRenderingHints
import javax.imageio.IIOImage
import javax.imageio.ImageIO
import javax.imageio.ImageWriter
import javax.imageio.stream.ImageOutputStream
import java.awt.image.BufferedImage
import java.io.FileOutputStream
import java.util.Iterator
* 截取PDF文档第一页生成缩略图
public class CapturePdfFirstPageThumbnailUtil {
* 生成缩略图
* @param inputFile 需要生成缩略图的PDF文件路径
* @param outputFile 生成缩略图的存储路径
public static void generateThumbnail(String inputFile, String outputFile) {
long start = System.currentTimeMillis()
Document document
try {
float rotation = 0f
// 缩略图显示倍数,1表示不缩放,0.5表示缩小到50%
float zoom = 0.5f
document = new Document()
document.setFile(inputFile)
BufferedImage image = (BufferedImage) document.getPageImage(0,
GraphicsRenderingHints.SCREEN,
Page.BOUNDARY_CROPBOX, rotation, zoom)
Iterator<ImageWriter> iter = ImageIO.getImageWritersBySuffix("jpg")
ImageWriter writer = iter.next()
FileOutputStream out = new FileOutputStream(outputFile)
ImageOutputStream outImage = ImageIO.createImageOutputStream(out)
writer.setOutput(outImage)
writer.write(new IIOImage(image, null, null))
System.out.println("耗时:" + (System.currentTimeMillis() - start) + "ms")
} catch (Exception e) {
e.printStackTrace()
public static void main(String[] args) {
String inputFilePath = "C:\\test.pdf"
String outputFilePath = "C:\\test.jpg"
generateThumbnail(inputFilePath, outputFilePath)
警告信息不用管,好像是因为用的不是PRO版本吧!
看了一下还挺耗时的。