使用Java将PDF文件转成图片
import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; /** * Created by David on 2016/11/15. */ public class Pdf2Pic { /** * 根据pdf文件拆分图片 * @param ossKey * @param file * @return * @throws IOException */ public static String splitPic(String ossKey,File file) throws IOException { // load a pdf from a byte buffer RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel .size()); PDFFile pdffile = new PDFFile(buf); System.out.println("页数: " + pdffile.getNumPages()); String getPdfFilePath = CheckResultConstances.PATH_PDF_PIC+ossKey; //目录不存在,则创建目录 File p=new File(getPdfFilePath); if(!p.exists()){ p.mkdir(); } System.out.println("getPdfFilePath is :"+getPdfFilePath); for (int i = 1; i <= pdffile.getNumPages(); i++) { // draw the first page to an image PDFPage page = pdffile.getPage(i); // get the width and height for the doc at the default zoom Rectangle rect = new Rectangle(0, 0, (int) page.getBBox() .getWidth(), (int) page.getBBox().getHeight()); // generate the image Image img = page.getImage(rect.width, rect.height, // width & // height rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); BufferedImage tag = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height, null); String dstName=getPdfFilePath+File.separator + i + ".jpg"; FileOutputStream out = new FileOutputStream(dstName); // 输出到文件流 System.out.println("成功保存图片到 : " +getPdfFilePath+File.separator + i + ".jpg"); // // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); // encoder.encode(tag); // JPEG编码 String formatName = dstName.substring(dstName.lastIndexOf(".") + 1); //FileOutputStream out = new FileOutputStream(dstName); //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); //encoder.encode(dstImage); ImageIO.write(tag, /*"GIF"*/ formatName /* format desired */ , new File(dstName) /* target */ ); out.close(); } return getPdfFilePath; // show the image in a frame // JFrame frame = new JFrame("PDF Test"); // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // frame.add(new JLabel(new ImageIcon(img))); // frame.pack(); // frame.setVisible(true); } public static void main(final String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { File file = new File("/Users/David/Downloads/test.pdf"); new Pdf2Pic().splitPic("123",file); } catch (IOException ex) { ex.printStackTrace(); } } }); } }
pom.xml依赖配置(将附件中的该jar包上传到nexus内部仓库):
<dependency> <groupId>com.sun.pdfview</groupId> <artifactId>pdfrenderer</artifactId> <version>0.9.0</version> </dependency>
参考:
相关推荐
java使用了itext7将PDF文件转成png图片格式,里面有demo可以直接运行,希望给需要的朋友带来帮助,谢谢支持!
java URL转PDF文件,网页内容转PDF文件
java 把PDF转换成BASE64,java 把PDF转换成BASE64,java 把PDF转换BASE64,java 把PDF转换成BASE64java 把PDF转换成BASE64
在java中通过ImageMagick安装包和im4java.jar把其他格式图片如pdf转换为PNG,JPEG等常用格式图片之间的相互转换。转换效率还可以。内有安装操作步骤及编码内容。亲测有效。 场景运用:在前端图片格式上传中,需要在...
参考大家学习,使用步骤和例子,压缩包中有,可以将大量图片转成pdf文件,用扫描仪扫到的图片转成pdf必用
Java将PDF生成图片,想向这方面了解的可以下载看看
java显示PDF
可以把pdf文件转换成jpg, gif,bmp,tiff,png 的图片格式. 可以把jpg, gif,bmp,tiff,png 的图片格式转换成pdf文件. pdf to jpg的主要特性: 1.把pdf文件转换成jpg、bmp、tiff、gif、png等等图片格式. 2.把jpg、bmp、...
把PDF文件转换成jpg和png格式文件 等 (PDF转图片Java实现工程,含代码及所用的jar包) 1. 解压后把目录下面的 1.pdf 文件 放到D盘 根目录 或修改 PdfToJpgTest.java 文件中的相关文件路径 2. 导入此工程到 Eclipse ...
包含icepdf-core-4.3.3.jar包及后端代码,将多张PDF转为一张图片,水印、图章无法转出,更改代码中文件路径,加入JAR包运行即可。
Java 生成pdf文件加盖公章图片,上传阿里云,代码加jar包
将代码保存为Java文件(例如,PDFToImageConverter.java)。然后,在命令行中使用Java编译器编译...脚本将使用Apache PDFBox库打开PDF文件并将每个页面转换为JPEG格式的图像。转换后的图像将保存在指定的输出文件夹中。
itexpdf在文件上添加图片,也可动态添加图片,非常实用
本篇上传的资源提供将图片JPG转PDF文件,代码详见附件
Entry.java这个类的原理是先通过OfficeToPDF.java把文档转成pdf,然后再通过Pdf2Jpg.java这个类把PDF转成JPG或者PNG,想转成什么格式图片在Pdf2Jpg.java这个类当中设置一下,代码经过测试,完全没有问题,如果碰到...
java生成pdf,java生成pdf,pdf的demo,java生成pdf,java生成pdf,pdf的demo
完美转换
Java 将图片列表转换合并为pdf完整示例,含代码、示例图片、依赖包
有时候我们客户需求需要将Excel文件转成图片推送给客户,由于Excel貌似不能直接转成图片,该工具包实现思路为先将Excel转成pdf,再由PDF转成图片。内附效果图,jar包,代码