将word、excel、ppt、html、txt转换成图片

         写这篇文章是因为在做项目的时候,需求里面提到了这么一个功能,android下面实现附件查看、下载、在线阅览的功能。对于下载没什么好说的。在线阅览是指将各种类型的附件转换成图片,然后手机在线打开图片,从而来实现阅览的效果。

          分享一下我的实现方式:首先利用openoffice将各种类型的文档转换成pdf,然后再利用imagsio将pdf转成图片。

          那么我们下面谈一下如何处理:

                去哪里下载openoffice?

                openoffice的下载地址你可以通过百度搜索openoffice下载,就就直接下载到。

                安装完成以后怎么用呢?

                安装完成以后找到你的安装目录,打开目录下面的program文件夹。该文件夹里面包含一个soffice.exe文件。当你进入这个目录以后,按住shift然后右击在此处打开命令提示符,然后输入命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 然后回车就ok了。这样就会启动openoffice的8100端口。

               项目中如何运用openoffice?

               首先你要准备好jar包。

                 commons-io-1.4.jar

                 icepdf-core.jar
                 jodconverter-2.2.2.jar
                 ridl-3.0.1.jar
                 slf4j-api-1.5.8.jar
                 slf4j-jdk14-1.5.6.jar
                 unoil-3.0.1.jar

               示例代码如下:

   public static void doc2Pdf(String docPath, String pdfPath) throws ConnectException {
                           File inputFile = new File(docPath);//预转文件
                           File outputFile = new File(pdfPath);//pdf文件
                           OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
                           connection.connect();//建立连接
                           DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                           DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();   
                           DocumentFormat txt = formatReg.getFormatByFileExtension("odt") ;//设定文件格式
                           DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf") ;//设定文件格式
                           converter.convert(inputFile, txt, outputFile, pdf);//文件转换
                           connection.disconnect();//关闭连接
                  }


                如何将pdf转换成图片?

                 示例代码如下:

              

   /**
     * 将pdf转换成图片
     *
     * @param pdfPath
     * @param imagePath
     * @return 返回转换后图片的名字
     * @throws Exception
     */
    public static List<String> pdf2Imgs(String pdfPath, String imgDirPath) throws Exception {
        Document document = new Document();
        document.setFile(pdfPath);

        float scale = 5f;//放大倍数
        float rotation = 0f;//旋转度数

        List<String> imgNames = new ArrayList<String>();
        int pageNum = document.getNumberOfPages();
        File imgDir = new File(imgDirPath);
        if (!imgDir.exists()) {
            imgDir.mkdirs();
        }
        for (int i = 0; i < pageNum; i++) {
            BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
                    Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            try {
                String filePath = imgDirPath + File.separator + i + ".jpg";
                File file = new File(filePath);
                ImageIO.write(rendImage, "jpg", file);
                imgNames.add(FilenameUtils.getName(filePath));
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            image.flush();
        }
        document.dispose();
        return imgNames;
    }
         我只说一下项目里面的这两个 scale、rotation。

        这两个参数的含义:

        scale:是指再转成成图片以后对图片的放大倍数,它影响字体清晰度。但是这个参数不能太大,如文件特别大的时候就会OOM的。这个参数要根据自己的项目来定,我建议在1~2之间。我测试在5.5的时候就直接oom了,我的电脑配置是i3 6G内存。

        rotation:是指的旋转的度数。0就是默认不旋转,当然还有360n。。

        如何将任意类型文件转成图片类型?

       代码如下:

 public static void doc2Imags(String docPath, String imgDirPath){
    	String pdfPath =String.format("%s%s.pdf",  FilenameUtils.getFullPath(docPath), FilenameUtils.getBaseName(docPath));
    	try {
			doc2Pdf(docPath, pdfPath);
			pdf2Imgs(pdfPath, imgDirPath);
			File pdf =  new File(pdfPath);
			if(pdf.isFile()){
				pdf.delete();
			}

		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
        源代码从哪里下载?

         http://download.csdn.net/download/woaixinxin123/6332321

                                                                                                                                                                                   本人web、android开发 、qq群255825960

猜你喜欢

转载自blog.csdn.net/woaixinxin123/article/details/12117877