office转pdf java

代码如下:

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.Variant;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Office2Pdf {


	    public static Converter newConverter(String name) {
	        if (name.equals("wps")) {
	            return new Wps();
	        } else if (name.equals("pdfcreator")) {
	            return new PdfCreator();
	        }
	        return null;
	    }

	    public synchronized static boolean convert(String word, String pdf) {
	        return newConverter("wps").convert(word, pdf);
	    }

	    public static interface Converter {

	        public boolean convert(String word, String pdf);
	    }

	    public static class Wps implements Converter {

	        public synchronized boolean convert(String word, String pdf) {
	            File pdfFile = new File(pdf);
	            File wordFile = new File(word);
	            ActiveXComponent wps = null;
	            try {
	                wps = new ActiveXComponent("wps.application");
	                ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", new Variant(wordFile.getAbsolutePath()));
	                doc.invoke("ExportPdf", new Variant(pdfFile.getAbsolutePath()));
	                doc.invoke("Close");
	                doc.safeRelease();
	            } catch (Exception ex) {
	                Logger.getLogger(Office2Pdf.class.getName()).log(Level.SEVERE, null, ex);
	                return false;
	            } catch (Error ex) {
	                Logger.getLogger(Office2Pdf.class.getName()).log(Level.SEVERE, null, ex);
	                return false;
	            } finally {
	                if (wps != null) {
	                    wps.invoke("Terminate");
	                    wps.safeRelease();
	                }
	            }
	            return true;
	        }
	    }

	    public static class PdfCreator implements Converter {

	        public static final int STATUS_IN_PROGRESS = 2;
	        public static final int STATUS_WITH_ERRORS = 1;
	        public static final int STATUS_READY = 0;
	        private ActiveXComponent pdfCreator;
	        private DispatchEvents dispatcher;
	        private volatile int status;
	        private Variant defaultPrinter;

	        private void init() {
	            pdfCreator = new ActiveXComponent("PDFCreator.clsPDFCreator");
	            dispatcher = new DispatchEvents(pdfCreator, this);
	            pdfCreator.setProperty("cVisible", new Variant(false));
	            pdfCreator.invoke("cStart", new Variant[]{new Variant("/NoProcessingAtStartup"), new Variant(true)});
	            setCOption("UseAutosave", 1);
	            setCOption("UseAutosaveDirectory", 1);
	            setCOption("AutosaveFormat", 0);  // 0 = PDF
	            defaultPrinter = pdfCreator.getProperty("cDefaultPrinter");
	            status = STATUS_IN_PROGRESS;
	            pdfCreator.setProperty("cDefaultprinter", "PDFCreator");
	            pdfCreator.invoke("cClearCache");
	            pdfCreator.setProperty("cPrinterStop", false);
	        }

	        private void setCOption(String property, Object value) {
	            Dispatch.invoke(pdfCreator, "cOption", Dispatch.Put, new Object[]{property, value}, new int[2]);
	        }

	        private void close() {
	            if (pdfCreator != null) {
	                pdfCreator.setProperty("cDefaultprinter", defaultPrinter);
	                pdfCreator.invoke("cClearCache");
	                pdfCreator.setProperty("cPrinterStop", true);
	                pdfCreator.invoke("cClose");
	                pdfCreator.safeRelease();
	                pdfCreator = null;
	            }
	            if (dispatcher != null) {
	                dispatcher.safeRelease();
	                dispatcher = null;
	            }
	        }

	        public synchronized boolean convert(String word, String pdf) {
	            File pdfFile = new File(pdf);
	            File wordFile = new File(word);
	            try {
	                init();
	                setCOption("AutosaveDirectory", pdfFile.getParentFile().getAbsolutePath());
	                if (pdfFile.exists()) {
	                    pdfFile.delete();
	                }
	                pdfCreator.invoke("cPrintfile", wordFile.getAbsolutePath());
	                int seconds = 0;
	                while (isInProcess()) {
	                    seconds++;
	                    if (seconds > 30) { // timeout
	                        throw new Exception("convertion timeout!");
	                    }
	                    Thread.sleep(1000);
	                }
	                if (isWithErrors()) return false;
	                // 由于转换前设置cOption的AutosaveFilename不能保证输出的文件名与设置的相同(pdfcreator会加入/修改后缀名)
	                // 所以这里让pdfcreator使用自动生成的文件名进行输出,然后在保存后将其重命名为目标文件名
	                File outputFile = new File(pdfCreator.getPropertyAsString("cOutputFilename"));
	                if (outputFile.exists()) {
	                    outputFile.renameTo(pdfFile);
	                }
	            } catch (InterruptedException ex) {
	                Logger.getLogger(Office2Pdf.class.getName()).log(Level.SEVERE, null, ex);
	                return false;
	            } catch (Exception ex) {
	                Logger.getLogger(Office2Pdf.class.getName()).log(Level.SEVERE, null, ex);
	                return false;
	            } catch (Error ex) {
	                Logger.getLogger(Office2Pdf.class.getName()).log(Level.SEVERE, null, ex);
	                return false;
	            } finally {
	                close();
	            }
	            return true;
	        }

	        private boolean isInProcess() {
	            return status == STATUS_IN_PROGRESS;
	        }

	        private boolean isWithErrors() {
	            return status == STATUS_WITH_ERRORS;
	        }

	        // eReady event
	        public void eReady(Variant[] args) {
	            status = STATUS_READY;
	        }

	        // eError event
	        public void eError(Variant[] args) {
	            status = STATUS_WITH_ERRORS;
	        }
	    }

	    public static void main(String[] args) {
	        convert("D:\\project\\rscms\\WebContent\\upload\\123.doc", "D:\\project\\rscms\\WebContent\\upload\\123.pdf");
	    }
	}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

275261506

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值