Question: how does java convert word, Excel and PPT to pdf
My personal view: use Jacob under windows server; Use openoffice under Linux server.
PS:1. This article talks about the part of using jacob under windows server, and then the solution of openoffice.
2. The solution mentioned in this article supports the conversion of DOC, docx, xls, xlsx, PPT and pptx formats into pdf.
3. jacob can call windows com components, but there is not only one solution: you can search "java calls com" to see other solutions
1. Process of finding solutions:
In the project, there is a need to convert office(word,excel,ppt, later collectively referred to as mssoft document) to pdf.
At first, OpenOffice (OpenOffice.org is a cross platform office software suite) service was called with java to convert mssoft document into pdf, which was successful. At that time, I was very excited. I felt that OpenOffice was omnipotent. It can convert mssoft document, odt, ODS and ODP. However, later, after some mssoft documents were converted into pdf, there were problems of typography dislocation, unexplained bold font, and even some contents disappeared directly, so I couldn't solve it.
Later, I found a project to convert to pdf on github and tried to convert several word s. I found that the effect of converting to pdf was not good in the past. After the subsequent mssoft document s were converted to pdf, there was still a problem of typographical dislocation, which made me crazy.
If you think about it, mssoft document is owned by Microsoft. It is certain that other third-party tools can not achieve 100% effect. Therefore, consider whether msoffice can do the conversion operation by itself, so as to ensure the effect after conversion. And the server is windows server, and then you find jacob(java com bridge).
2. Preconditions:
This machine only needs windows system to run. If it is a web project, the server used must be windows server. Reason: jacob needs to use msoffice software for pdf conversion, so msoffice 07/10/13 needs to be installed on the server.
My local environment:
jdk 1.6
tomcat 1.6
maven 3.1 (you can also directly create ordinary java projects without maven)
msoffice 2010
3. Preparation:
Download Jacob Zip, address: https://sourceforge.net/projects/jacob-project/
Click download in the figure. The downloaded compressed package contains the following contents:
x64 dll is used for 64 bit system and x86 dll is used for 32-bit system. Put the dll file into the jdk/bin directory, as shown in the following figure:
PS: my local version is 1.18-M2. In this article, the version in the screenshot compressed package is 1.18, so the version in the screenshot is different, which does not affect the operation of the program.
4. Compress Jacob in the package Jar import project
How to introduce ordinary java projects (guava toolkit can be downloaded by yourself) will not be discussed in detail.
maven project, my local POM XML is configured as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>indi.johnny</groupId> <artifactId>jacob-convert</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> <dependency> <groupId>com.jacob</groupId> <artifactId>jacob</artifactId> <version>1.18-M2</version> <scope>system</scope> <systemPath>E:/.m2/repository/jacob-1.18-M2/jacob.jar</systemPath> </dependency> </dependencies> </project>
The value of the < SystemPath > tag of the second < dependency > in the above configuration file is Jacob The specific path of jar. Just change this to your own.
5. Upper code
The code below also refers to the blogs written by several bloggers and has been sorted out a little. I'm ashamed to say. Now I can't find all the reference links. If bloggers see them, they can tell me. I'll add the reference links.
package indi.johnny.convert; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class Demo { private static final Integer WORD_TO_PDF_OPERAND = 17; private static final Integer PPT_TO_PDF_OPERAND = 32; private static final Integer EXCEL_TO_PDF_OPERAND = 0; public void doc2pdf(String srcFilePath, String pdfFilePath) throws Exception { ActiveXComponent app = null; Dispatch doc = null; try { ComThread.InitSTA(); app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", false); Dispatch docs = app.getProperty("Documents").toDispatch(); Object[] obj = new Object[]{ srcFilePath, new Variant(false), new Variant(false),//Read only new Variant(false), new Variant("pwd") }; doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch(); // Dispatch.put(doc, "Compatibility", false); // Compatibility check, false for specific value is incorrect Dispatch.put(doc, "RemovePersonalInformation", false); Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND); // word is saved as a pdf macro with a value of 17 }catch (Exception e) { e.printStackTrace(); throw e; } finally { if (doc != null) { Dispatch.call(doc, "Close", false); } if (app != null) { app.invoke("Quit", 0); } ComThread.Release(); } } public void ppt2pdf(String srcFilePath, String pdfFilePath) throws Exception { ActiveXComponent app = null; Dispatch ppt = null; try { ComThread.InitSTA(); app = new ActiveXComponent("PowerPoint.Application"); Dispatch ppts = app.getProperty("Presentations").toDispatch(); /* * call * param 4: ReadOnly * param 5: Untitled Specifies whether the file has a title * param 6: WithWindow Specifies whether the file is visible * */ ppt = Dispatch.call(ppts, "Open", srcFilePath, true,true, false).toDispatch(); Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND); // ppSaveAsPDF is a specific value of 32 } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (ppt != null) { Dispatch.call(ppt, "Close"); } if (app != null) { app.invoke("Quit"); } ComThread.Release(); } } public void excel2Pdf(String inFilePath, String outFilePath) throws Exception { ActiveXComponent ax = null; Dispatch excel = null; try { ComThread.InitSTA(); ax = new ActiveXComponent("Excel.Application"); ax.setProperty("Visible", new Variant(false)); ax.setProperty("AutomationSecurity", new Variant(3)); // Disable macros Dispatch excels = ax.getProperty("Workbooks").toDispatch(); Object[] obj = new Object[]{ inFilePath, new Variant(false), new Variant(false) }; excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch(); // Conversion format Object[] obj2 = new Object[]{ new Variant(EXCEL_TO_PDF_OPERAND), // PDF format = 0 outFilePath, new Variant(0) //0 = standard (the generated PDF image will not become blurred); 1 = minimum file }; Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method,obj2, new int[1]); } catch (Exception es) { es.printStackTrace(); throw es; } finally { if (excel != null) { Dispatch.call(excel, "Close", new Variant(false)); } if (ax != null) { ax.invoke("Quit", new Variant[] {}); ax = null; } ComThread.Release(); } } public static void main(String[] args) throws Exception { String path = "C:/Users/johnny/Desktop/file/20170427/test/001/"; new Demo().doc2pdf(path + "1.docx", path+ "1.pdf"); // new Demo().doc2pdf(path + "1.docx", path+ "1x.pdf"); } }