doc转pdf实用程序

    在web项目中,尤其是在带有工作流的应用中,通常需要把doc文件转成pdf文件供领导审核。我的设计思路是在服务器上运行一个job,每隔一定的时间,去扫描指定的文件夹,发现有doc文件,则调用开源的"PDFCreator”软件进行转换,转换完成,删除该doc文件。具体实现如下:

  1. 下载安装"PDFCreator"。
  2.  配制"PDFCreator": 在选项中,选中自动保存,再选择一个目录做为自动保存的目录。
  3.  程序如下:

 public class PdfCreater {

 private static final String CLASSNAME = PdfCreater.class.getName();

 public void docToPdf() {

  String strIn = "";
  String strOut = "";
  try {
   String docFolder = Resources.getProperties("docFolder");
   String errorFolder = Resources.getProperties("errorFolder");
   String strProcess = Resources.getProperties("pdfCreateProcess");

   File fileDirectory = new File(docFolder);

   if (!fileDirectory.isDirectory()) {
    throw new Exception("not a directory: " + fileDirectory.getName());
   }

   // Getting all files and directories in the current directory
   File[] entries = fileDirectory.listFiles(new FileFilter() {
    public boolean accept(File pathname) {
     return pathname.getName().endsWith("doc");
    }
   });

   for (int i = 0; i < entries.length; ++i) {

    String strParameter = "/"" + entries[i].getAbsolutePath() + "/"";
    Process process = Runtime.getRuntime().exec(strProcess + strParameter);

    int c = process.waitFor();

    Thread.sleep(Integer.valueOf(Resources.getProperties("millisecond")));

    strIn = entries[i].getAbsolutePath();
    strOut = errorFolder + entries[i].getAbsolutePath().substring(entries[i].getAbsolutePath().lastIndexOf("//"));

    // fail
    if (c != 0) {
     String errFileName = entries[i].getAbsolutePath().substring(entries[i].getAbsolutePath().lastIndexOf("//"));
     LogRecorder.error(CLASSNAME, "covert pdf error, file name is :" + strParameter);
     FileUtil.CopyFile(entries[i].getAbsolutePath(), errorFolder + errFileName);
     FileUtil.deleteFile(entries[i].getAbsolutePath());

    } else {

     FileUtil.deleteFile(entries[i].getAbsolutePath());

    }

   }

  } catch (Exception e) {

   LogRecorder.error(CLASSNAME, "covert pdf error, file name is :" + strIn);
   try {
    FileUtil.CopyFile(strIn, strOut);
    FileUtil.deleteFile(strIn);

   } catch (IOException e1) {
    e1.printStackTrace();
   }
  }
 }

}

 

上述代码是转换的核心代码,Process process = Runtime.getRuntime().exec(strProcess + strParameter); 这行代码开始调用开源软件;Thread.sleep(Integer.valueOf(Resources.getProperties("millisecond"))) 这行代码的意思是每转完一个DOC文件,要让程序sleep一段时间,以便让doc的内存彻底释放。

 

public class DocToPdfMain {
 
 public static void main(String[] arg) {

  Timer timer = new Timer();

  try {
   int startDelay = Integer.valueOf(Resources.getProperties("startDelay"));
   int space = Integer.valueOf(Resources.getProperties("space"));
   timer.schedule(new RunJob(), startDelay, space);
  } catch (Exception e) {
   e.printStackTrace();
   
  }

 }

}


public class RunJob extends TimerTask {

 public void run() {
  try {

   new PdfCreater().docToPdf();

  } catch (Exception e) {
   e.printStackTrace();
   LogRecorder.error(RunJob.class.getName(), "error", e);
  }
 }

}

 

以上两例是自定义的job程序。

 

pdfCreateProcess=C://Program Files//PDFCreator//PDFCreator.exe /PF
#this folder place doc
docFolder=E://doc2pdf//docFolder
#this folder place pdf
pdfFolder=E://doc2pdf//pdfFolder
#this folder place doc converted error
errorFolder=E://doc2pdf//errorFolder
#sleep millisecond
millisecond=5000
#start action after porgram run
startDelay = 10000
#how ofter action
space=60000

 

以上是配制文件信息。

 

set PATH=%PATH%
set classpath=%classpath%;
java  -Xms64M -Xmx512M  xxx.DocToPdfMain

 

以上是批处理信息。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值