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

分享一下我的实现方式:首先利用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 plain copy
  1. public static void doc2Pdf(String docPath, String pdfPath) throws ConnectException {  
  2.                         File inputFile = new File(docPath);//预转文件  
  3.                         File outputFile = new File(pdfPath);//pdf文件  
  4.                         OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);  
  5.                         connection.connect();//建立连接  
  6.                         DocumentConverter converter = new OpenOfficeDocumentConverter(connection);  
  7.                         DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();     
  8.                         DocumentFormat txt = formatReg.getFormatByFileExtension("odt") ;//设定文件格式  
  9.                         DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf") ;//设定文件格式  
  10.                         converter.convert(inputFile, txt, outputFile, pdf);//文件转换  
  11.                         connection.disconnect();//关闭连接  
  12.                }  


                如何将pdf转换成图片?

                 示例代码如下:

              

[java]  view plain copy
  1. /** 
  2.   * 将pdf转换成图片 
  3.   * 
  4.   * @param pdfPath 
  5.   * @param imagePath 
  6.   * @return 返回转换后图片的名字 
  7.   * @throws Exception 
  8.   */  
  9.  public static List<String> pdf2Imgs(String pdfPath, String imgDirPath) throws Exception {  
  10.      Document document = new Document();  
  11.      document.setFile(pdfPath);  
  12.   
  13.      float scale = 5f;//放大倍数  
  14.      float rotation = 0f;//旋转度数  
  15.   
  16.      List<String> imgNames = new ArrayList<String>();  
  17.      int pageNum = document.getNumberOfPages();  
  18.      File imgDir = new File(imgDirPath);  
  19.      if (!imgDir.exists()) {  
  20.          imgDir.mkdirs();  
  21.      }  
  22.      for (int i = 0; i < pageNum; i++) {  
  23.          BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,  
  24.                  Page.BOUNDARY_CROPBOX, rotation, scale);  
  25.          RenderedImage rendImage = image;  
  26.          try {  
  27.              String filePath = imgDirPath + File.separator + i + ".jpg";  
  28.              File file = new File(filePath);  
  29.              ImageIO.write(rendImage, "jpg", file);  
  30.              imgNames.add(FilenameUtils.getName(filePath));  
  31.          } catch (IOException e) {  
  32.              e.printStackTrace();  
  33.              return null;  
  34.          }  
  35.          image.flush();  
  36.      }  
  37.      document.dispose();  
  38.      return imgNames;  
  39.  }  
         我只说一下项目里面的这两个 scale、rotation。

        这两个参数的含义:

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

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

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

       代码如下:

[java]  view plain copy
  1. public static void doc2Imags(String docPath, String imgDirPath){  
  2.     String pdfPath =String.format("%s%s.pdf",  FilenameUtils.getFullPath(docPath), FilenameUtils.getBaseName(docPath));  
  3.     try {  
  4.         doc2Pdf(docPath, pdfPath);  
  5.         pdf2Imgs(pdfPath, imgDirPath);  
  6.         File pdf =  new File(pdfPath);  
  7.         if(pdf.isFile()){  
  8.             pdf.delete();  
  9.         }  
  10.   
  11.     } catch (ConnectException e) {  
  12.         e.printStackTrace();  
  13.     } catch (Exception e) {  
  14.         e.printStackTrace();  
  15.     }  
  16.    }  
        源代码从哪里下载?

         http://download.csdn.net/detail/oaimeng12/7686733

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值