zoukankan      html  css  js  c++  java
  • 转 Java将PDF转换成图片

    这里介绍两种将PDF转换成图片的方式

    一、使用icepdf

         下载导入jar包,jar包地址:http://download.csdn.net/download/u010782875/10041519

          代码实现:

    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    import javax.imageio.ImageIO;

    import org.icepdf.core.pobjects.Document;
    import org.icepdf.core.util.GraphicsRenderingHints;

    /**
    * @ClassName:PdfToImageIcePdf
    * @Description:
    * @author:
    * @data:2017/10/25
    */
    public class PdfToImageIcePdf {
    public static void main(final String[] args) {
    String filePath = "D://Test/xxx.pdf"
    List<String> imageList = getPdfToImage(filePath);
    Iterator<String> iterator = imageList.iterator();
    while(iterator.hasNext()){
    System.out.println(iterator.next());
    }
    }
    //返回生成图片的路径
    public static List<String> getPdfToImage(String filePath) {
    String fileName = filePath.substring(0,filePath.lastIndexOf("."));//获取去除后缀的文件路径
    List<String> list = new ArrayList<>();
    String imagePath;
    Document document = new Document();
    try{
    document.setFile(filePath);
    }catch (Exception e){
    e.printStackTrace();
    }

    float scale = 1.3f;// 缩放比例
    float rotation = 0f;// 旋转角度

    for (int i = 0; i < document.getNumberOfPages(); i++) {
    BufferedImage image = (BufferedImage) document.getPageImage(i,
    GraphicsRenderingHints.SCREEN,
    org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation,
    scale);
    RenderedImage rendImage = image;
    try {
    int n = i + 1;
    File f = new File(fileName);
    if(!f.exists()){
    f.mkdir();
    }
    imagePath = fileName + "/image"+ n + ".jpg";//生成图片的路径
    File file = new File(imagePath);
    ImageIO.write(rendImage, "jpg", file);
    // 这里png作用是:格式是jpg但有png清晰度
    // ImageIO.write(rendImage, "png", file);
    list.add(imagePath);
    } catch (IOException e) {
    e.printStackTrace();
    }
    image.flush();
    }
    document.dispose();
    return list;
    }

             该方法在main()可以正常运行,但是放入action中会报错 “Java.lang.NoClassDefFoundError:org/icepdf/core/util/content/b”

    二、使用pdfbox

            下载导入jar包,jar包地址:http://download.csdn.net/download/u010782875/10041527

             代码实现:

    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.rendering.PDFRenderer;

    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    /**
    * @ClassName:PdfToImagePdfBox
    * @Description:
    * @author:
    * @data:2017/10/27
    */
    public class PdfToImagePdfBox {
    public static void main(String[] args){
    String filePath = "D://Test/xxx.pdf";
    List<String> imageList = pdfToImagePath(filePath);
    Iterator<String> iterator = imageList.iterator();
    while(iterator.hasNext()){

    System.out.println(iterator.next());
    }
    // pdfToImage(filePath);
    }
    public static List<String> pdfToImagePath(String filePath){
    List<String> list = new ArrayList<>();
    String fileDirectory = filePath.substring(0,filePath.lastIndexOf("."));//获取去除后缀的文件路径

    String imagePath;
    File file = new File(filePath);
    try {
    File f = new File(fileDirectory);
    if(!f.exists()){
    f.mkdir();
    }
    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);
    int pageCount = doc.getNumberOfPages();
    for(int i=0; i<pageCount; i++){
    // 方式1,第二个参数是设置缩放比(即像素)
    // BufferedImage image = renderer.renderImageWithDPI(i, 296);
    // 方式2,第二个参数是设置缩放比(即像素)
    BufferedImage image = renderer.renderImage(i, 1.25f); //第二个参数越大生成图片分辨率越高,转换时间也就越长
    imagePath = fileDirectory + "/"+i + ".jpg";
    ImageIO.write(image, "PNG", new File(imagePath));
    list.add(imagePath);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    return list;
    }
    该方法在action中可以正常运行

  • 相关阅读:
    SQL80001: Incorrect syntax near ':'
    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
    log4net 日志配置及使用
    在C#中将String转换成Enum:
    未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序解决办法
    五大编程算法
    CodeSmith 使用说明
    PowerDesigner使用教程
    一个七年程序员的经验
    未能正确加载包“Microsoft.Data.Entity.Design.Package.MicrosoftDataEntityDesignPackage
  • 原文地址:https://www.cnblogs.com/ZJ0065/p/10711988.html
Copyright © 2011-2022 走看看