A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 朱婵 中级黑马   /  2014-2-20 17:56  /  1080 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.net.ConnectException;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7. import java.util.HashSet;
  8. import java.util.List;
  9. import java.util.Set;
  10. import java.util.SortedMap;
  11. import java.util.SortedSet;
  12. import java.util.concurrent.ConcurrentSkipListMap;
  13. import java.util.concurrent.ConcurrentSkipListSet;

  14. import pde.ict.common.DmsTools;
  15. import pde.ict.service.utils.AttachTempDirClearUtil;
  16. import pde.ict.service.utils.ClientConverterUtil;
  17. import pde.ict.service.utils.PDEConstant;

  18. import com.artofsolving.jodconverter.DocumentConverter;
  19. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  20. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
  21. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;


  22. public class OfficeToPdfUtil {
  23.         private  String openOffice_HOME = "C:\\Program Files\\OpenOffice.org 3";
  24.        
  25.         private static final  int CONVERT_TYPE = 2; //PDF转换方式 1:openOffice服务转换 , 2:Office服务转换
  26.        
  27.         private String host= "127.0.0.1";
  28.        
  29.         private String port = "8100";
  30.        
  31.         private PdfUtil pdfUtil = null;
  32.        
  33.         public PdfUtil getPdfUtil() {
  34.                 if(pdfUtil==null){
  35.                         pdfUtil = new PdfUtil();
  36.                 }
  37.                 return pdfUtil;
  38.         }


  39.         public void setPdfUtil(PdfUtil pdfUtil) {
  40.                 this.pdfUtil = pdfUtil;
  41.         }
  42.        
  43.         /**
  44.          * pdf文件缓存列表
  45.          * <源文件,目标PDF文件>
  46.          */
  47.         private SortedMap<String,PdfFile> pdfchche = new ConcurrentSkipListMap<String,PdfFile>();
  48.        
  49.         /**
  50.          * pdf文件缓存排序列表
  51.          * <源文件,目标PDF文件>
  52.          */
  53.         private SortedSet<PdfFile> pdfchcheSorte = new ConcurrentSkipListSet<PdfFile>();
  54.        

  55.         /**
  56.          * 生成PDF文件且使用缓存管理
  57.          * @param inputFile
  58.          * @return
  59.          */
  60.         @SuppressWarnings("all")
  61.         public synchronized PdfFile toPdf(String inputFile){
  62.                 PdfFile pdfFile = null;
  63.                 String _root = PDEConstant.PDFATTACH_TEMP_DIR_PATH;
  64.                 //检查PDF缓存文件夹是否存在
  65.                 File folder = new File(_root);
  66.                 if(!folder.isDirectory()){
  67.                         if(!folder.mkdirs()){
  68.                                 return null;
  69.                         }
  70.                 }
  71.                 File _inputFile = new File(inputFile);
  72.                 if(!_inputFile.isFile()){
  73.                         return null;
  74.                 }
  75.                 if(pdfchche.containsKey(inputFile)){
  76.                         pdfFile = pdfchche.get(inputFile);
  77.                         pdfchcheSorte.remove(pdfFile);
  78.                 } else {
  79.                         AttachTempDirClearUtil.execute(folder);//清理临时目录
  80.                         int deletenum = 0;
  81.                         //如果pdf文件缓存数大于PDFATTACH_MAXNUM时
  82.                         long pdfAttachMaxNum = 0L;
  83.                         try {
  84.                                 pdfAttachMaxNum = Long.parseLong(DmsTools.getValueFromConfigFileByKey("pdf_attach_max_num"));
  85.                         }catch(Exception e) {
  86.                                 e.printStackTrace();
  87.                                 pdfAttachMaxNum = 1000L;
  88.                         }
  89.                         if(pdfchche.size() > pdfAttachMaxNum){
  90.                                 deletenum = 100;
  91.                         }
  92.                         //删除空闲的PDf文件
  93.                         for(int i=0;i<deletenum && i<pdfchche.size();i++){
  94.                                 PdfFile _tempPdfFile = pdfchcheSorte.first();
  95.                                 if(null!=_tempPdfFile && null!=_tempPdfFile.getPdfFile()){
  96.                                         File _delFile = new File(_tempPdfFile.getPdfFile());
  97.                                         if(_delFile.isFile()){
  98.                                                 if(!_delFile.delete()){
  99.                                                         continue;
  100.                                                 }
  101.                                         }
  102.                                 }
  103.                                 pdfchche.remove(_tempPdfFile.getInputFile());
  104.                                 pdfchcheSorte.remove(_tempPdfFile);
  105.                         }
  106.                         pdfFile = new PdfFile(inputFile,_root +  "/"+_inputFile.getName() + ".pdf");
  107.                 }
  108.                 if(pdfFile!=null){
  109.                         if(!new File(pdfFile.getPdfFile()).isFile()){
  110.                                 if(CONVERT_TYPE == 1) {//调用OpenOffice服务转换文件
  111.                                         if(!this.getPdfUtil().convertPDF(inputFile,pdfFile.getPdfFile())){
  112.                                                 return null;
  113.                                         }
  114.                                 }else { //调用Office服务转换文件
  115.                                         ClientConverterUtil.convert(inputFile, pdfFile.getPdfFile());
  116.                                         if(!new File(pdfFile.getPdfFile()).exists()) {
  117.                                                 return null;
  118.                                         }
  119.                                        
  120.                                 }
  121.                                
  122.                         }
  123.                         pdfFile.setDate(new Date());
  124.                         pdfFile.setUserNum(pdfFile.getUserNum()+1);
  125.                         pdfchche.put(inputFile,pdfFile);
  126.                         pdfchcheSorte.add(pdfFile);
  127.                 }
  128.                 return pdfFile;
  129.         }

  130.        
  131.         public SortedMap<String, PdfFile> getPdfchche() {
  132.                 return pdfchche;
  133.         }


  134.         public void setPdfchche(SortedMap<String, PdfFile> pdfchche) {
  135.                 this.pdfchche = pdfchche;
  136.         }


  137.         public SortedSet<PdfFile> getPdfchcheSorte() {
  138.                 return pdfchcheSorte;
  139.         }


  140.         public void setPdfchcheSorte(SortedSet<PdfFile> pdfchcheSorte) {
  141.                 this.pdfchcheSorte = pdfchcheSorte;
  142.         }


  143.         public int offieToPdf(String sourceFile, String destFile){
  144.                 try {
  145.                         File inputFile = new File(sourceFile);
  146.                         if (!inputFile.exists()) {
  147.                                 return -1;// 找不到源文件, 则返回-1
  148.                         }

  149.                         // 假如目标路径不存在, 则新建该路径
  150.                         File outputFile = new File(destFile);
  151.                         if (!outputFile.getParentFile().exists()) {
  152.                                 outputFile.getParentFile().mkdirs();
  153.                         }

  154.                         Process pro = startService();

  155.                         // connect to an OpenOffice.org instance running on port 8100
  156.                         OpenOfficeConnection connection = new SocketOpenOfficeConnection(
  157.                                         host,Integer.parseInt(port));
  158.                         connection.connect();

  159.                         // convert
  160.                         DocumentConverter converter = new OpenOfficeDocumentConverter(
  161.                                         connection);
  162.                         converter.convert(inputFile, outputFile);

  163.                         // close the connection
  164.                         connection.disconnect();
  165.                         // 封闭OpenOffice服务的进程
  166.                         pro.destroy();

  167.                         return 0;
  168.                 } catch (FileNotFoundException e) {
  169.                         e.printStackTrace();
  170.                         return -1;
  171.                 } catch (ConnectException e) {
  172.                         e.printStackTrace();
  173.                 } catch (IOException e) {
  174.                         e.printStackTrace();
  175.                 }

  176.                 return 1;
  177.         }

  178.         private Process startService() throws IOException {
  179.                 // 假如从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
  180.                 if (openOffice_HOME.charAt(openOffice_HOME.length() - 1) != '\\') {
  181.                         openOffice_HOME += "\\";
  182.                 }
  183.                 // 启动OpenOffice的服务
  184.                 String command = openOffice_HOME
  185.                                 + "program\\soffice.exe -headless -accept=\"socket,host=" + host + ",port=" + port + ";urp;\"";
  186.                 Process pro = Runtime.getRuntime().exec(command);
  187.                 return pro;
  188.         }

  189.         public String getOpenOffice_HOME() {
  190.                 return openOffice_HOME;
  191.         }

  192.         public void setOpenOffice_HOME(String openOffice_HOME) {
  193.                 this.openOffice_HOME = openOffice_HOME;
  194.         }

  195.         public String getHost() {
  196.                 return host;
  197.         }

  198.         public void setHost(String host) {
  199.                 this.host = host;
  200.         }

  201.         public String getPort() {
  202.                 return port;
  203.         }

  204.         public void setPort(String port) {
  205.                 this.port = port;
  206.         }
  207.        
  208.        
  209.         /**
  210.          * 转换文件至PDF且加水印
  211.          * @param inputFile 输入文件地址
  212.          * @param outputFile 输出文件地址
  213.          * @param waterMarkPath 水印图片地址
  214.          * @param rotation 旋转角度  -90---90度
  215.          * @param percent 大小百分比  100为原始大小
  216.          * @param fillOpacity 透明度 0f - 1f
  217.          * @return
  218.          */
  219.         public boolean ConvertToPdfAddWaterMark(String inputFile, String outputFile,
  220.                         String waterMarkPath, float rotation, int percent,
  221.                         float fillOpacity) {
  222.                 boolean result = false;
  223.                 PdfFile pdfFile = null;
  224.                 if(PdfUtil.getExtension(inputFile, "").equalsIgnoreCase("PDF")){
  225.                         pdfFile =  new PdfFile(inputFile,inputFile);
  226.                 } else {
  227.                         pdfFile = this.toPdf(inputFile);
  228.                 }
  229.                 if(pdfFile!=null){
  230.                         List<Watermark> watermarks = new ArrayList<Watermark>();
  231.                         Set<Integer> pages = new HashSet<Integer>();
  232.                         pages.add(1);
  233.                         watermarks.add(new WatermarkImage(10,0,0,rotation,0,null,waterMarkPath,percent));
  234.                         result = this.getPdfUtil().addWatermark(pdfFile.getPdfFile(), outputFile, fillOpacity, watermarks);
  235.                 }
  236.                 return result;
  237.         }
  238.        
  239.         /**
  240.          * 得到文件扩展名
  241.          * @param filename 文件名
  242.          * @param defExt 默认扩展名
  243.          * @return
  244.          */
  245.         public static String getExtension(String filename, String defExt) {
  246.                 if ((filename != null) && (filename.length() > 0)) {
  247.                         int i = filename.lastIndexOf('.');

  248.                         if ((i > -1) && (i < (filename.length() - 1))) {
  249.                                 return filename.substring(i + 1);
  250.                         }
  251.                 }
  252.                 return defExt;
  253.         }
  254.        
  255.         public static void main(String[]strs){
  256.                 OfficeToPdfUtil o = new OfficeToPdfUtil();
  257.                 o.ConvertToPdfAddWaterMark("f:/2.txt","f:/2.txt.all.pdf", "f:/P1000983.JPG", 0, 80, 0.5f);
  258.                 System.out.println(((PdfFile)o.getPdfchcheSorte().first()).getUserNum());
  259.         }
  260. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

1 个回复

倒序浏览
一年_Hei 来自手机 中级黑马 2014-2-20 19:05:50
沙发
楼主太强大了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马