前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pdf 转 图片 - 崔笑颜的博客

pdf 转 图片 - 崔笑颜的博客

作者头像
崔笑颜
发布2021-02-02 18:02:37
8330
发布2021-02-02 18:02:37
举报

这里使用到的jar包 是 // https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox compile group: ‘org.apache.pdfbox’, name: ‘fontbox’, version: ‘1.8.16’ // https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox compile group: ‘org.apache.pdfbox’, name: ‘pdfbox’, version: ‘1.8.16’

pdf转单页图片 合并一张的工具库 前往aspose-words word转图片 下载使用

/**
	 * 将pdf转成一张图片
	 * @param pdffile
	 * @return
	 * @throws IOException
	 */
	public static String getPDFBinary(String pdffile) throws IOException {
		//文件流
		InputStream inputStream = new FileInputStream(pdffile);
		//文件 获取文件名字
		File file = new File(pdffile);
		String name = file.getName();
		//截取不带后缀名的字段
		String fileName = name.substring(0, name.lastIndexOf("."));
 
		//文件上传路径
		String parent = file.getParent();
 
		PDDocument doc = PDDocument.load(inputStream);
		//获取pdf文件 页数
		List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
 
		List<BufferedImage> imageList = new ArrayList<BufferedImage>();
		BufferedImage image = null;
		for (int i = 0; i < pages.size(); i++) {
			PDPage page = (PDPage)pages.get(i); // 获取第i页
			image = page.convertToImage();
			imageList.add(image);
			//合并一张
			image = MergeImage.mergeImage(false, imageList);
		}
 
		boolean png = ImageIO.write(image, "png", new File(parent +"/"+ fileName + ".png"));
		if(png == false){
			return "转换失败";
		}
 
		//关闭流
		inputStream.close();
		return "转换成功";
 
 
	}

pdf 每页转图片 放到文件夹内 转换成压缩包 也可以不放压缩包 随意 压缩包工具类 java实现文件压缩,文件夹压缩,以及文件和文件夹的混合压缩

// 将pdf 转化为 图片分页 压缩包
 	public static String getPDFBinarys(String pdffile) throws IOException {
		//文件流
		InputStream inputStream = new FileInputStream(pdffile);
		PDDocument doc = PDDocument.load(inputStream);
 
		//文件 获取文件名字
		File file = new File(pdffile);
		String name = file.getName();
		//截取不带后缀名的字段
		String fileName = name.substring(0, name.lastIndexOf("."));
		//文件上传路径
		String parent = file.getParent();
 
		//获取pdf文件 页数
		List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
 
		BufferedImage image = null;
 
		//创建同名文件夹
		new File(parent+"/"+ fileName).mkdir();
 
		for (int i = 0; i < pages.size(); i++) {
			PDPage page = (PDPage) pages.get(i); // 获取第i页
			image = page.convertToImage();
			ImageIO.write(image, "png", new File(parent +"/"+ fileName +"/"+ "第"+ i +"页" + fileName + ".png"));
 
		}
 
		//压缩同名文件夹
		    File f = new File(parent  +"/" + fileName );
            new CompactAlgorithm(new File( parent+ "/",f.getName()+".zip")).zipFiles(f);
 
		//关闭流
		inputStream.close();
 
 
		return "转换成功"+pages.size()+"页";
 
	}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021年1月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档