1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| public class JacobToPdfUtil {
private static final int WORD_FORMAT_PDF = 17; private static final int EXCEL_FORMAT_PDF = 0; private static final int PPT_FORMAT_PDF = 32;
private static final String PROGRAM_WORD_OFFICE = "Word.Application"; private static final String PROGRAM_WORD_WPS = "KWPS.Application"; private static final String PROGRAM_EXCEL_OFFICE = "Excel.Application"; private static final String PROGRAM_EXCEL_WPS = "KET.Application"; private static final String PROGRAM_PPT_OFFICE = "PowerPoint.Application"; private static final String PROGRAM_PPT_WPS = "KWPP.Application";
public static void wordToPdfByMsOffice(String inputFile, String pdfFile) throws IOException { wordToPdf(inputFile, pdfFile, PROGRAM_WORD_OFFICE); }
public static void wordToPdfByWps(String inputFile, String pdfFile) throws IOException { wordToPdf(inputFile, pdfFile, PROGRAM_WORD_WPS); }
private static void wordToPdf(String inputFile, String pdfFile, String program) throws IOException { ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent(program); app.setProperty("Visible", new Variant(false)); app.setProperty("AutomationSecurity", new Variant(3)); Dispatch docs = app.getProperty("Documents").toDispatch(); doc = Dispatch.call(docs, "Open", inputFile).toDispatch(); Dispatch.call(doc, "SaveAs", pdfFile, WORD_FORMAT_PDF); } catch (Throwable t) { throw new IOException("word转pdf失败", t); } finally { if (doc != null) { Dispatch.call(doc, "Close", false); } if (app != null) { app.invoke("Quit"); } ComThread.Release(); } }
public static void excelToPdfByMsOffice(String inputFile, String pdfFile) throws IOException { excelToPdf(inputFile, pdfFile, PROGRAM_EXCEL_OFFICE); }
public static void excelToPdfByWps(String inputFile, String pdfFile) throws IOException { excelToPdf(inputFile, pdfFile, PROGRAM_EXCEL_WPS); }
private static void excelToPdf(String inputFile, String pdfFile, String program) throws IOException { ActiveXComponent app = null; Dispatch excel = null; try { app = new ActiveXComponent(program); app.setProperty("Visible", new Variant(false)); app.setProperty("AutomationSecurity", new Variant(3)); Dispatch workbooks = app.getProperty("Workbooks").toDispatch(); excel = Dispatch.call(workbooks, "Open", inputFile).toDispatch(); Dispatch.call(excel, "ExportAsFixedFormat", EXCEL_FORMAT_PDF, pdfFile); } catch (Throwable t) { throw new IOException("excel转pdf失败", t); } finally { if (excel != null) { Dispatch.call(excel, "Close", false); } if (app != null) { app.invoke("Quit"); } ComThread.Release(); } }
public static void pptToPdfByMsOffice(String inputFile, String pdfFile) throws IOException { pptToPdf(inputFile, pdfFile, PROGRAM_PPT_OFFICE); }
public static void pptToPdfByWps(String inputFile, String pdfFile) throws IOException { pptToPdf(inputFile, pdfFile, PROGRAM_PPT_WPS); }
private static void pptToPdf(String inputFile, String pdfFile, String program) throws IOException { ActiveXComponent app = null; Dispatch ppt = null; try { app = new ActiveXComponent(program); app.setProperty("AutomationSecurity", new Variant(3)); Dispatch presentations = app.getProperty("Presentations").toDispatch(); ppt = Dispatch.call(presentations, "Open", inputFile, true, false, false ).toDispatch(); Dispatch.call(ppt, "SaveAs", pdfFile, PPT_FORMAT_PDF); } catch (Throwable t) { throw new IOException("ppt转换pdf失败", t); } finally { if (ppt != null) { Dispatch.call(ppt, "Close"); } if (app != null) { app.invoke("Quit"); } ComThread.Release(); } } }
|