pdf转图片

maven引入依赖:

<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.2</version>
</dependency>



package test.controller;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文件预览
 */
@RestController
@RequestMapping(value = "showPdfImg")
public class ShowPdfImgController {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(ShowPdfImgController.class);

    /**
     * pdf转img.
     * @param docId 文件id
     * @param response 响应对象
     */
    @CrossOrigin
    @RequestMapping(value = "/img", method = { RequestMethod.GET })
    public void img(@RequestParam(value = "docId") String docId, HttpServletResponse response) {
        try {
            long st = System.currentTimeMillis();
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpeg");
            InputStream in = new FileInputStream(new File("E:\\xx.pdf"));
            PDDocument pd = PDDocument.load(in);
            PDFRenderer pdfRenderer = new PDFRenderer(pd);
            BufferedImage combined = null;
            for (int page = 0; page < pd.getNumberOfPages(); ++page) {
                BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 146, ImageType.RGB);
                if (page == 0) {
                    combined = bim;
                } else {
                    combined = merge(combined, bim);
                }
            }
            pd.close();
            OutputStream out = response.getOutputStream();
            ImageIO.write(combined, "JPEG", out);
            long et = System.currentTimeMillis();
            System.out.print("执行时间:" + (et - st));
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成图像
     * @param image1 对象
     * @param image2 对象
     * @return 对象
     */
    private static BufferedImage merge(BufferedImage image1, BufferedImage image2) {
        BufferedImage combined = new BufferedImage(image1.getWidth(), image1.getHeight() + image2.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics gh = combined.getGraphics();
        gh.drawImage(image1, 0, 0, null);
        gh.drawImage(image2, 0, image1.getHeight(), null);
        gh.dispose();
        return combined;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AP PDF to IMAGE Batch Converter 是一个有用的 pdf工具,该换器能利用源 PDF 文档格式换全功能的带文字,图片,图表等图像。它不需要任何软件支持,如 Adobe Acrobat,Acrobat Reader 等。 AP PDF to IMAGE Batch Converter 支持自定义输出结果页面。你可以选择换几页文档,甚至自由换文档页面,如 "1,2,3","2-100" 等,要获得更多信息,请参考页面范围设置。它能处理多个文件,只要确认设置好每个文件的输出目录,这样所有处理完的文件都会放置到它们相应的目录中。 PDF to Image 功能 * 迅速PDF 文件到图像格式。 * 支持批量PDF 到图像。 * 支持加密的 PD F文件。 * 支持自定义输出结果页面。 * 支持矢量图形换到图像文件。 * 支持多个图像格式,如 'bmp','tiff','jpg','gif','png','pcx' 等。 * 支持生成多页面 tiff 文件。 * 支持生成任何分辨率的图像文件。 * 支持换为 1 位,4 位,8 位,4 位灰度,8 位灰度和 24 位格式图像。 * 支持 tiff 图像的多种压缩,如 LZW,JPEG,PACKBITS,CCITT Group3,CCITT Group4,RLE 等。 * 支持拖放文件换。 * 支持 Win98/ME/NT/2000/XP/2003/Vista 平台。 * 换后可以自动显示图像文件。 * 允许你保留源文档的布局。 * 容易使用,单独的应用程序。 * 不需要 Adobe Acrobat 或 Adobe Acrobat Reader 的支持。 * 自动安装和配置。 * 兼容 Adobe Acrobat 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 。 命令行功能 PDF to Image 换命令行程序也提供一个有正常PDF to Image 功能的控制台。 * 包括 PDF to Image 的所有功能。 * 它可以像一个 DOS 命令行应用程序一样简单使用。 * 支持文件夹。 * 批量换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值