您的位置:首页 > Web前端 > HTML

将word、excel、ppt、html、txt,pdf转换成图片源代码

2014-07-28 13:31 716 查看
分享一下我的实现方式:首先利用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包。

                 

               示例代码如下:

[java] view
plaincopy

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转换成图片?

                 示例代码如下:

              

[java] view
plaincopy

/** 

  * 将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。。

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

       代码如下:

[java] view
plaincopy

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/detail/oaimeng12/7686733
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息