word 上传后转pdf,pdf又转swf,工具openoffice和pdf2swf.exe

String filePath = VarFactory.getVarValue("DOC_IA") +person.getDeptCode() + "/" + isoIaAnnualPlan.getPlanName() +".doc";
  System.out.println("filePath:"+ filePath);
  
  if (!(file == null ||file.isEmpty())) {
   StringfileName = file.getOriginalFilename();
   StringfileFormat = fileName.substring(fileName.length() - 4,fileName.length());
   
   System.out.println("!!fileName:" + fileName);
   System.out.println("!!fileFormat:" + fileFormat);
   
   if(!fileFormat.equals(".doc")) {
    response.setContentType("text/html;charset=UTF-8");
    try{
     response.getWriter().write("{success:false,msg:'文件格式错误!请使用office2003Word文件格式!'}");
    }catch (IOException e) {
     e.printStackTrace();
    }
    returnnull;
   }
   
   FileexsitFile = new File(filePath);
   if(exsitFile.exists()) {
    exsitFile.delete();
   }
   
   try {
    
    file.transferTo(createFile(isoIaAnnualPlan.getPlanName(),person.getDeptCode()));
    
    
    System.out.println("!!filePath:" + filePath);
    ConvertorUtil.convertWord2SWF(filePath);
    
   } catch(IllegalStateException e) {
    e.printStackTrace();
   } catch(IOException e) {

    e.printStackTrace();
   }
  } else {
   filePath ="";
  }

 

 

///----------------------------------------------------------------

public class ConvertorUtil {
 public static void FlashPaperConvert(Stringf1,String f2){
  //ProcessBuilder p=newProcessBuilder("flashprinter.exe",f1,"-o",f2);
  ProcessBuilder p=newProcessBuilder("pdf2swf.exe ",f1,"-o ",f2+" -T 9");
  
  try {
   Processp1=p.start();
  } catch (IOException e) {
   
   e.printStackTrace();
  }
 }


 安装完swftools-0.9.2.exe,将pdf2swf.exe拷贝到c:\windows\system32下面,就能调用下面程序来将pdf转成swf
 public static void convertWord2SWF(String word){
  //String swfFile = "cmd /cflashprinter.exe " + word + " -o " + StringUtils.removeEnd(word,".doc") + ".swf";
  String tempDirStr =VarFactory.getVarValue("DOC_TEMP");
  File tempDir = newFile(tempDirStr);
  if (!tempDir.exists()) {
   tempDir.mkdirs();
  }
  File file = new File(tempDirStr+ Math.random() * 10 + ".pdf");
  JOD4DocToPDF.docToPdf(newFile(word), file);
  
  
  String swfFile = "pdf2swf.exe "+ file.getAbsolutePath() + " -o " + StringUtils.removeEnd(word,".doc") + ".swf -T 9";
  
  System.out.println("!!!swfFile: " + swfFile);
  
  try {
   Runtime.getRuntime().exec(swfFile);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

 

 

///----------------------------------------------------------

 

public class JOD4DocToPDF extends Thread {
 protected final Log logger =LogFactory.getLog(getClass());
 private File inputFile;

 private JOD4DocToPDF() {
 }

 public JOD4DocToPDF(File inputFile) {
  this.inputFile =inputFile;
 }

 public static void docToPdf(File inputFile,File outputFile) {

  // connect to anOpenOffice.org instance running on port 8100
  // 用cmd进入目录后 敲命令 soffice-headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard
  
  Date start = new Date();
  OpenOfficeConnection connection= new SocketOpenOfficeConnection(8100);
  
  try {
   connection.connect();
   DocumentConverterconverter = new OpenOfficeDocumentConverter(connection);
   converter.convert(inputFile,outputFile);
  } catch (ConnectException cex){
   throw newRuntimeException();
  } finally {
   if(connection != null) {
    connection.disconnect();
    connection= null;
   }
  }
  long l = (start.getTime() - newDate().getTime());
  long day = l / (24 * 60 * 60 *1000);
  long hour = (l / (60 * 60 *1000) - day * 24);
  long min = ((l / (60 * 1000)) -day * 24 * 60 - hour * 60);
  long s = (l / 1000 - day * 24 *60 * 60 - hour * 60 * 60 - min * 60);
  //logger.info("生成" +outputFile.getAbsolutePath() + "耗费:" + min + "分" + s + "秒");
 }

 @Override
 public void run() {
  JOD4DocToPDF t = newJOD4DocToPDF();
  StringBuffer pdfName = newStringBuffer(inputFile.getParent()).append(File.separator).append(StringUtils.removeEnd(inputFile.getName(),".doc")).append(".pdf");
  t.docToPdf(inputFile, newFile(pdfName.toString()));
 }
 public static void main(String[] args) {
  docToPdf(newFile("c:/aaaa.doc"), new File("c:/aaaa.pdf"));

 }
}

 

//-----------------------------------------------------------------------------------------

public static String convertWord2SWF(String word) {
  //String swfFile = "cmd /cflashprinter.exe " + word + " -o " + StringUtils.removeEnd(word,".doc") + ".swf";
  String tempDirStr =VarFactory.getVarValue("DOC_TEMP");
  File tempDir = newFile(tempDirStr);
  String pdfFile = "";
  
  if (!tempDir.exists()) {
   tempDir.mkdirs();
  }
  File file = new File(tempDirStr+ Math.random() * 10 + ".pdf");
  JOD4DocToPDF.docToPdf(newFile(word), file);
  
  
  pdfFile =file.getAbsolutePath();
  
  String swfFile = "pdf2swf.exe "+ file.getAbsolutePath() + " -o " + StringUtils.removeEnd(word,".doc") + ".swf -T 9"; 
  try {
   Runtime.getRuntime().exec(swfFile);
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  return pdfFile;
 }


评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值