问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

java代码doc转pdf提高效率的方法

发布网友 发布时间:2022-04-23 09:35

我来回答

3个回答

热心网友 时间:2023-09-18 05:07

使用了jacob.jar来调用activex控件,本机需安装WPS或pdfcreator
001 package experiments;
002
003 import com.jacob.activeX.ActiveXComponent;
004 import com.jacob.com.Dispatch;
005 import com.jacob.com.DispatchEvents;
006 import com.jacob.com.Variant;
007 import java.io.File;
008 import java.util.logging.Level;
009 import java.util.logging.Logger;
010
011 public class Doc2Pdf {
012
013 public static Converter newConverter(String name) {
014 if (name.equals("wps")) {
015 return new Wps();
016 } else if (name.equals("pdfcreator")) {
017 return new PdfCreator();
018 }
019 return null;
020 }
021
022 public synchronized static boolean convert(String word, String pdf) {
023 return newConverter("pdfcreator").convert(word, pdf);
024 }
025
026 public static interface Converter {
027
028 public boolean convert(String word, String pdf);
029 }
030
031 public static class Wps implements Converter {
032
033 public synchronized boolean convert(String word, String pdf) {
034 File pdfFile = new File(pdf);
035 File wordFile = new File(word);
036 ActiveXComponent wps = null;
037 try {
038 wps = new ActiveXComponent("wps.application");
039 ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", newVariant(wordFile.getAbsolutePath()));
040 doc.invoke("ExportPdf", new Variant(pdfFile.getAbsolutePath()));
041 doc.invoke("Close");
042 doc.safeRelease();
043 } catch (Exception ex) {
044 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
045 return false;
046 } catch (Error ex) {
047 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
048 return false;
049 } finally {
050 if (wps != null) {
051 wps.invoke("Terminate");
052 wps.safeRelease();
053 }
054 }
055 return true;
056 }
057 }
058
059 public static class PdfCreator implements Converter {
060
061 public static final int STATUS_IN_PROGRESS = 2;
062 public static final int STATUS_WITH_ERRORS = 1;
063 public static final int STATUS_READY = 0;
064 private ActiveXComponent pdfCreator;
065 private DispatchEvents dispatcher;
066 private volatile int status;
067 private Variant defaultPrinter;
068
069 private void init() {
070 pdfCreator = new ActiveXComponent("PDFCreator.clsPDFCreator");
071 dispatcher = new DispatchEvents(pdfCreator, this);
072 pdfCreator.setProperty("cVisible", new Variant(false));
073 pdfCreator.invoke("cStart", new Variant[]{newVariant("/NoProcessingAtStartup"), new Variant(true)});
074 setCOption("UseAutosave", 1);
075 setCOption("UseAutosaveDirectory", 1);
076 setCOption("AutosaveFormat", 0); // 0 = PDF
077 defaultPrinter = pdfCreator.getProperty("cDefaultPrinter");
078 status = STATUS_IN_PROGRESS;
079 pdfCreator.setProperty("cDefaultprinter", "PDFCreator");
080 pdfCreator.invoke("cClearCache");
081 pdfCreator.setProperty("cPrinterStop", false);
082 }
083
084 private void setCOption(String property, Object value) {
085 Dispatch.invoke(pdfCreator, "cOption", Dispatch.Put, new Object[]{property, value}, new int[2]);
086 }
087
088 private void close() {
089 if (pdfCreator != null) {
090 pdfCreator.setProperty("cDefaultprinter", defaultPrinter);
091 pdfCreator.invoke("cClearCache");
092 pdfCreator.setProperty("cPrinterStop", true);
093 pdfCreator.invoke("cClose");
094 pdfCreator.safeRelease();
095 pdfCreator = null;
096 }
097 if (dispatcher != null) {
098 dispatcher.safeRelease();
099 dispatcher = null;
100 }
101 }
102
103 public synchronized boolean convert(String word, String pdf) {
104 File pdfFile = new File(pdf);
105 File wordFile = new File(word);
106 try {
107 init();
108 setCOption("AutosaveDirectory", pdfFile.getParentFile().getAbsolutePath());
109 if (pdfFile.exists()) {
110 pdfFile.delete();
111 }
112 pdfCreator.invoke("cPrintfile", wordFile.getAbsolutePath());
113 int seconds = 0;
114 while (isInProcess()) {
115 seconds++;
116 if (seconds > 30) { // timeout
117 throw new Exception("convertion timeout!");
118 }
119 Thread.sleep(1000);
120 }
121 if (isWithErrors()) return false;
122 // 由于转换前设置cOption的AutosaveFilename不能保证输出的文件名与设置的相同(pdfcreator会加入/修改后缀名)
123 // 所以这里让pdfcreator使用自动生成的文件名进行输出,然后在保存后将其重命名为目标文件名
124 File outputFile = newFile(pdfCreator.getPropertyAsString("cOutputFilename"));
125 if (outputFile.exists()) {
126 outputFile.renameTo(pdfFile);
127 }
128 } catch (InterruptedException ex) {
129 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
130 return false;
131 } catch (Exception ex) {
132 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
133 return false;
134 } catch (Error ex) {
135 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
136 return false;
137 } finally {
138 close();
139 }
140 return true;
141 }
142
143 private boolean isInProcess() {
144 return status == STATUS_IN_PROGRESS;
145 }
146
147 private boolean isWithErrors() {
148 return status == STATUS_WITH_ERRORS;
149 }
150
151 // eReady event
152 public void eReady(Variant[] args) {
153 status = STATUS_READY;
154 }
155
156 // eError event
157 public void eError(Variant[] args) {
158 status = STATUS_WITH_ERRORS;
159 }
160 }
161
162 public static void main(String[] args) {
163 convert("e:\\test.doc", "e:\\output.pdf");
164 }
165 }追问我用的是word

热心网友 时间:2023-09-18 05:07

用原始的 doc下的命令。

热心网友 时间:2023-09-18 05:07

jquery插件
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
我要买车,本人男,30出头,预购三厢车,价格11-14之间,高人推荐一下? 瘦肚子瘦最快方法 怎么瘦肚子 新手买车咨询,男生,身高185cm,10万元的预算,包括各种税险等,想购置三厢... 对方把朋友圈设置了部分人可见,但我见不到,是不是不把我当成好朋友了... 想买车了,10万左右的三厢手动挡,POLO劲取.奔腾B50.新宝来.哪个好呢... 2024年阴历8月哪天适合提车 外甥结婚第一年还给压岁钱,第二年不给了好吗 压岁钱可以第二年比前一年发的少吗?比如第一年给三千,后来给一千或者五 ... 2023年农历正月属羊人最吉利的提车日子本月哪天宜买车上牌? 结婚黄道吉日2023年8月适合属羊人办喜事的日子查询? 关于批处理、notepad++和任务管理器的问题 如何使UltraEdit支援Verilog语法显示 如何用ultraedit高亮语法显示verilog 窝窝怎么做如何做好吃 “断桥残雪”一词有何而来,其中的断桥在什么地方,有什么典故嘛?_百度问一问 请问用jacob将html转word中文乱码怎么解决? 粗粮窝窝的做法,粗粮窝窝怎么做好吃,粗粮窝窝 东北的“窝窝”是南瓜吗? 西方音乐史上排名前五十位的古典作曲家最经典作品 方言窝窝是什么意思 windows bat读取txt文件 窝窝软件是干嘛用吗? 批处理下用某一程序打开某一文件。。 唯美、小清新的一个字有哪些? 男人给女人说窝窝是什么意思 ultraedit的system verilog的wordfile谁有? 窝窝代表什么意思 微信把好友删了还能查到聊天记录吗? 女生活泼开朗的网名 qq网名 12岁 开朗的女生 求 活泼浪漫、唯美和好听的网名谢谢 请问java中用jacob将html转word中文乱码怎么解决? 如何设置是UltraEdit支持Shell脚本的语法高亮 抖音小店开通需要什么资质?有什么条件? 抖音电商赚‏钱吗?开抖店麻不麻烦? 邝伟忠艺术达字签名怎么写好看如何写才好看 教我怎么写连笔字签名(贾春明) 王宏达,请帮助设计一个艺术签名 连笔签名字设计 “P”指的是什么? “大”字能组成哪些词? 以“大”为偏旁的字组成的词语有哪些? 大字头能组成什么上下结构的字? ‘大’做偏旁能组成什么字 "大"加部首组成的字 大 字加偏旁 组成新字 有没有《大小大》组成的字 两个大字组成是什么字 三个大字组成什么字 “口”字和“大”字可以组成什么字? 注册一个商贸公司有什么好处