Java中常用的几种DOCX转PDF方法

xiaoxiao2021-02-28  50

点击查看全文

DOCX2PDF

将DOCX文档转化为PDF是项目中常见的需求之一,目前主流的方法可以分为两大类,一类是利用各种Office应用进行转换,譬如Microsoft Office、WPS以及LiberOffice,另一种是利用各种语言提供的对于Office文档读取的接口(譬如Apache POI)然后使用专门的PDFGenerator库,譬如IText进行PDF构建。总的来说,从样式上利用Office应用可以保证较好的样式,不过相对而言效率会比较低。其中Microsoft Office涉及版权,不可轻易使用(笔者所在公司就被抓包了),WPS目前使用比较广泛,不过存在超链接截断问题,即超过256个字符的超链接会被截断,LiberOffice的样式排版相对比较随意。而利用POI接口进行读取与生成的方式性能较好,适用于对于格式要求不是很高的情况。另外还有一些封装好的在线工具或者命令行工具,譬如docx2pdf与OfficeToPDF。

MicroSoft Office

本部分的核心代码如下,全部代码参考这里:

private ActiveXComponent oleComponent = null; private Dispatch activeDoc = null; private final static String APP_ID = "Word.Application";  // Constants that map onto Word's WdSaveOptions enumeration and that // may be passed to the close(int) method public static final int DO_NOT_SAVE_CHANGES = 0; public static final int PROMPT_TO_SAVE_CHANGES = -2; public static final int SAVE_CHANGES = -1;  // These constant values determine whether or not tha application // instance will be displyed on the users screen or notpublic static final boolean VISIBLE = truepublic static final boolean HIDDEN = false;  /**  * Create a new instance of the JacobWordSearch class using the following  * parameters.  *  * @param visibility A primitive boolean whose value will determine whether  *                   or not the Word application will be visible to the user. Pass true  *                   to display Word, false otherwise.  */ public OfficeConverter(boolean visibility) {     this.oleComponent = new ActiveXComponent(OfficeConverter.APP_ID);     this.oleComponent.setProperty("Visible", new Variant(visibility)); }  /**  * Open ana existing Word document.  *  * @param docName An instance of the String class that encapsulates the  *                path to and name of a valid Word file. Note that there are a few  *                limitations applying to the format of this String; it must specify  *                the absolute path to the file and it must not use the single forward  *                slash to specify the path separator.  */ public void openDoc(String docName) {     Dispatch disp = null;     Variant var = null;     // First get a Dispatch object referencing the Documents collection - for     // collections, think of ArrayLists of objects.     var = Dispatch.get(this.oleComponent, "Documents");     disp = var.getDispatch();     // Now call the Open method on the Documents collection Dispatch object     // to both open the file and add it to the collection. It would be possible     // to open a series of files and access each from the Documents collection     // but for this example, it is simpler to store a reference to the     // active document in a private instance variable.     var = Dispatch.call(disp, "Open", docName);     this.activeDoc = var.getDispatch(); }  /**  * There is more than one way to convert the document into PDF format, you  * can either explicitly use a FileConvertor object or call the  * ExportAsFixedFormat method on the active document. This method opts for  * the latter and calls the ExportAsFixedFormat method passing the name  * of the file along with the integer value of 17. This value maps onto one  * of Word's constants called wdExportFormatPDF and causes the application  * to convert the file into PDF format. If you wanted to do so, for testing  * purposes, you could add another value to the args array, a Boolean value  * of true. This would open the newly converted document automatically.  *  * @param filename  */ public void publishAsPDF(String filename) {     // The code to expoort as a PDF is 17     //Object args = new Object{filename, new Integer(17), new Boolean(true)};     Object args = new Object {         filename, new Integer(17)     } ;     Dispatch.call(this.activeDoc, "ExportAsFixedFormat", args); }  /**  * Called to close the active document. Note that this method simply  * calls the overloaded closeDoc(int) method passing the value 0 which  * instructs Word to close the document and discard any changes that may  * have been made since the document was opened or edited.  */ public void closeDoc() {     this.closeDoc(JacobWordSearch.DO_NOT_SAVE_CHANGES); }  /**  * Called to close the active document. It is possible with this overloaded  * version of the close() method to specify what should happen if the user  * has made changes to the document that have not been saved. There are three  * possible value defined by the following manifest constants;  * DO_NOT_SAVE_CHANGES - Close the document and discard any changes  * the user may have made.  * PROMPT_TO_SAVE_CHANGES - Display a prompt to the user asking them  * how to proceed.  * SAVE_CHANGES - Save the changes the user has made to the document.  *  * @param saveOption A primitive integer whose value indicates how the close  *                   operation should proceed if the user has made changes to the active  *                   document. Note that no checks are made on the value passed to  *                   this argument.  */ public void closeDoc(int saveOption) {     Object args = {new Integer(saveOption)};     Dispatch.call(this.activeDoc, "Close", args); }  /**  * Called once processing has completed in order to close down the instance  * of Word.  */ public void quit() {     Dispatch.call(this.oleComponent, "Quit"); }  

WPS

Java调用WPS或pdfcreator的com接口实现doc转pdf

本文的核心代码如下,完整代码查看这里:

 @Override         public boolean convert(String word, String pdf) {             File pdfFile = new File(pdf);             File wordFile = new File(word);             boolean convertSuccessfully = false;              ActiveXComponent wps = null;             ActiveXComponent doc = null;               try {                 wps = new ActiveXComponent("KWPS.Application");  //                Dispatch docs = wps.getProperty("Documents").toDispatch(); //                Dispatch d = Dispatch.call(docs, "Open", wordFile.getAbsolutePath(), falsetrue).toDispatch(); //                Dispatch.call(d, "SaveAs", pdfFile.getAbsolutePath(), 17); //                Dispatch.call(d, "Close"false);                  doc = wps.invokeGetComponent("Documents")                         .invokeGetComponent("Open", new Variant(wordFile.getAbsolutePath()));                  try {                     doc.invoke("SaveAs",                             new Variant(new File("C:\\Users\\lotuc\\Documents\\mmm.pdf").getAbsolutePath()),                             new Variant(17));                     convertSuccessfully = true;                 } catch (Exception e) {                     logger.warning("生成PDF失败");                     e.printStackTrace();                 }                  File saveAsFile = new File("C:\\Users\\lotuc\\Documents\\saveasfile.doc");                 try {                     doc.invoke("SaveAs", saveAsFile.getAbsolutePath());                     logger.info("成功另存为" + saveAsFile.getAbsolutePath());                 } catch (Exception e) {                     logger.info("另存为" + saveAsFile.getAbsolutePath() + "失败");                     e.printStackTrace();                 }             } finally {                 if (doc == null) {                     logger.info("打开文件 " + wordFile.getAbsolutePath() + " 失败");                 } else {                     try {                         logger.info("释放文件 " + wordFile.getAbsolutePath());                         doc.invoke("Close");                         doc.safeRelease();                     } catch (Exception e1) {                         logger.info("释放文件 " + wordFile.getAbsolutePath() + " 失败");                     }                 }                  if (wps == null) {                     logger.info("加载 WPS 控件失败");                 } else {                     try {                         logger.info("释放 WPS 控件");                         wps.invoke("Quit");                         wps.safeRelease();                     } catch (Exception e1) {                         logger.info("释放 WPS 控件失败");                     }                 }             }              return convertSuccessfully;         }  

LiberOffice

点击查看全文

转载请注明原文地址: https://www.6miu.com/read-79573.html

最新回复(0)