1.如何用纯java代码实现word转pdf
几种方案:
方法一:用apache pio 读取doc文件,然后转成html文件用Jsoup格式化html文件,最后用itext将html文件转成pdf。
方法2:使用jdoctopdf来实现,这是一个封装好的包,可以把doc转换成pdf,html,xml等格式,调用很方便
地址:
安装完后要启动openOffice的服务,具体启动方法请自行google
方法4:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf)
Office版本是2007,因为SaveAsPDFandXPS是微软为office2007及以上版本开发的插件
SaveAsPDFandXPS下载地址:/zh-cn/download/details.aspx?id=7
jacob 包下载地址:.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; 。
3.java怎么实现将word文档转换为pdf文件
WORD转换到PDF
WORD转换到PDF的步骤就相对简单了,我们只需要安装一款虚拟打印机软件“Virtual Pdf Printer”即可,它可以将你编辑好的WORD文档直接输出为PDF文件格式。(下载地址:
安装后在WORD中点击“打开”→“打印”,在“打印机名称”中会看到多了一个“Virtual Printer”选项,选中它,点击确定后(未注册版本有10秒的等待时间),弹出“保存PDF文件”对话框,先选择文件的保存路径,再点击“生成设置”按钮,弹出“系统设置”面板(如图3),对输出后的PDF文件进行加密和字体等设置,点击两次确定,当前的WORD文档就被转换成PDF格式的文件了
转载请注明出处51数据库 » java源码word转pdf