zoukankan      html  css  js  c++  java
  • 富文本框上传pdf文件并预览

    以百度编辑器ueditor为例

    上传图片调用的action为 uploadimage 

    在config.json中开放pdf的格式校验

    然后在 BinaryUploader 中修改代码,判断如果是pdf文件,则调用pdf转图片的方法,把图片保存下来,然后返回相应地址

    package test;
     
     
     
    import java.awt.image.BufferedImage;
     
    import java.io.File;
     
    import java.io.IOException;
     
     
     
    import javax.imageio.ImageIO;
     
     
     
    import org.apache.pdfbox.pdmodel.PDDocument;
     
    import org.apache.pdfbox.rendering.PDFRenderer;
     
     
     
    import com.lowagie.text.pdf.PdfReader;
     
     
     
    public class PDF2IMAGE {
     
        public static void main(String[] args) {
     
            pdf2Image("D:/1.pdf", "D:/pdf", 300);
     
        }
     
     
     
        /***
         * PDF文件转PNG图片,全部页数
         * 
         * @param PdfFilePath pdf完整路径
         * @param imgFilePath 图片存放的文件夹
         * @param dpi dpi越大转换后越清晰,相对转换速度越慢
         * @return
         */
     
        public static void pdf2Image(String PdfFilePath, String dstImgFolder, int dpi) {
     
            File file = new File(PdfFilePath);
     
            PDDocument pdDocument;
     
            try {
     
                String imgPDFPath = file.getParent();
     
                int dot = file.getName().lastIndexOf('.');
     
                String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
     
                String imgFolderPath = null;
     
                if (dstImgFolder.equals("")) {
     
                    imgFolderPath = imgPDFPath + File.separator + imagePDFName;// 获取图片存放的文件夹路径
     
                } else {
     
                    imgFolderPath = dstImgFolder + File.separator + imagePDFName;
     
                }
     
     
     
                if (createDirectory(imgFolderPath)) {
     
     
     
                    pdDocument = PDDocument.load(file);
     
                    PDFRenderer renderer = new PDFRenderer(pdDocument);
     
                    /* dpi越大转换后越清晰,相对转换速度越慢 */
     
                    PdfReader reader = new PdfReader(PdfFilePath);
     
                    int pages = reader.getNumberOfPages();
     
                    StringBuffer imgFilePath = null;
     
                    for (int i = 0; i < pages; i++) {
     
                        String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
     
                        imgFilePath = new StringBuffer();
     
                        imgFilePath.append(imgFilePathPrefix);
     
                        imgFilePath.append("_");
     
                        imgFilePath.append(String.valueOf(i + 1));
     
                        imgFilePath.append(".png");
     
                        File dstFile = new File(imgFilePath.toString());
     
                        BufferedImage image = renderer.renderImageWithDPI(i, dpi);
     
                        ImageIO.write(image, "png", dstFile);
     
                    }
     
                    System.out.println("PDF文档转PNG图片成功!");
     
     
     
                } else {
     
                    System.out.println("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");
     
                }
     
     
     
            } catch (IOException e) {
     
                e.printStackTrace();
     
            }
     
        }
     
     
     
        private static boolean createDirectory(String folder) {
     
            File dir = new File(folder);
     
            if (dir.exists()) {
     
                return true;
     
            } else {
     
                return dir.mkdirs();
     
            }
     
        }
     
     
     
    }
  • 相关阅读:
    方法要求ref object参数, 如果传递double值?
    注册表获取exe位置+ Process类启动exe+参数让exe打开指定文件
    ArcEngine 实现SurfaceAnalysis中的Cut/Fill功能
    ArcEngine 3D extension has not been enabled .
    ArcEngine 实现 SurfaceAnalysis的Area/Volumn功能
    安装部署程序,将安装目录写入注册表
    About Death _Island
    73 Left(Lake DT) . Neophocaena phoconoides(江豚) 20120419
    判断是否安装客户端,没有安装则进行下载
    ArcEngine 没有Esri.ArcGis.GeoAnalyst 命名空间
  • 原文地址:https://www.cnblogs.com/forthelichking/p/11821444.html
Copyright © 2011-2022 走看看