pdf操作图片

1、pdf转成图片

    public List<String> pdfTopng(String fileAddress, String filename, String type) {
        //fileAddress是文件夹地址    filename是文件地址    type是图片类型 如jpg
        List<String> list = new ArrayList<String>();
        // 将pdf装图片 并且自定义图片得格式大小
        File file = new File(fileAddress + File.separator + filename);
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
                String pngPath = fileAddress + File.separator + filename + "_" + (i + 1) + "." + type;
                ImageIO.write(image, type, new File(pngPath));
                list.add(pngPath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

2、在pdf文件后加图片

public void createPdf(String rootPath, String gsName, String gsCode, List<String> pngList) {
		//pngList 图片地址url的集合
        String fileName = CalendarHelper.formatDate(new Date(), "yyyyMMddHHmmss");
        String pdfName = "upload" + File.separator + fileName;
        String pdfPath = rootPath + File.separator + pdfName + ".pdf";

        Document document = new Document(PageSize.A4, 10, 10, 10, 10);
        try {
            // 定义输出位置并把文档对象装入输出对象中
            PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
            // 打开文档对象
            document.open();
            // 设置中文字体
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);

            String contentHtml = "<html><head></head><body>" + "标段名称:" + gsName + "标段编号:" + gsCode + "</body></html>";
            Paragraph tt = new Paragraph(contentHtml, FontChinese);
            document.add(tt);
            // 加入图片
            for (int i = 1; i <= pngList.size(); i++) {
            	//缩放图片
                String result = this.picUtils(pngList.get(i));
                Image jpg = Image.getInstance(result);
                jpg.setAlignment(Image.ALIGN_CENTER);
                document.add(jpg);
            }
            // 关闭文档对象,释放资源
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	/**
     * 图片比例缩放
     *
     * @param srcFile
     * @return
     * @throws IOException
     */
    public static String picUtils(String srcFile) throws IOException {
        //得到最后一个.的位置
        int index = srcFile.lastIndexOf(".");
        //获取被缩放的图片的格式
        String ext = srcFile.substring(index + 1);
        //获取目标路径(和原始图片路径相同,在文件名后添加了一个_s)
        String destFile = srcFile.substring(0, index) + "_s." + ext;
        //读取图片,返回一个BufferedImage对象
        BufferedImage img = ImageIO.read(new File(srcFile));
        //获取图片的长和宽
        int width = img.getWidth();
        int height = img.getHeight();

        //获取缩放后的长和宽
        int _width = (int) (0.5 * width);
        int _height = (int) (0.5 * height);
        //获取缩放后的Image对象
        java.awt.Image _img = img.getScaledInstance(_width, _height, java.awt.Image.SCALE_SMOOTH);
        //新建一个和Image对象相同大小的画布
        BufferedImage image = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
        //获取画笔
        Graphics2D graphics = image.createGraphics();
        //将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空
        graphics.drawImage(_img, 0, 0, null);
        //释放资源
        graphics.dispose();
        //使用ImageIO的方法进行输出,记得关闭资源
        OutputStream out = new FileOutputStream(destFile);
        ImageIO.write(image, ext, out);
        out.close();
        return  destFile;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值