最近做一个云打印项目,需要一个word、ppt、excel转化为pdf的功能然后再计算出,于是自己通过在网上找各种资料。利用 poi、iText、Jsoup、jdoctopdf、使利用 poi、iText、Jsoup、jdoctopdf、使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全,要么可是可能有问题,使用这个 jacob 的方法我最开始是最不想用的,因为它要导入 dll 文件,但最后我还是选择了使用该方法,原因是感觉转换后的pdf 文件简直就是完美。
jacob 缺点:需要 window 环境,而且速度是最慢的需要安装 msofficeWord 以及 SaveAsPDFandXPS.exe
SaveAsPDFandXPS.exe和jacob下载地址https://download.csdn.net/download/a664347225/10307109
jacob需要把jar引入项目,dll如果是64位放C:\Windows\System32下,32位放C:\Windows\SysWOW64下,
再放到jdk/bin下和jdk/jre/lib下
然后我们就可以转换了
package net.wingsky.file.util;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WordToPdf {
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 static void word2pdf(String srcFilePath, String pdfFilePath){
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),//是否只读
new Variant(false),
new Variant("pwd")
};
doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch();
// Dispatch.put(doc, "Compatibility", false); //兼容性检查,为特定值false不正确
Dispatch.put(doc, "RemovePersonalInformation", false);
Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND); // word保存为pdf格式宏,值为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 static void ppt2pdf(String srcFilePath, String pdfFilePath){
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指定文件是否有标题
* param 6: WithWindow指定文件是否可见
* */
ppt = Dispatch.call(ppts, "Open", srcFilePath, true,true, false).toDispatch();
Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND); // ppSaveAsPDF为特定值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 static void excel2pdf(String inFilePath, String outFilePath){
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)); // 禁用宏
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();
// 转换格式
Object[] obj2 = new Object[]{
new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0
outFilePath,
new Variant(0) //0=标准 (生成的PDF图片不会变模糊) ; 1=最小文件
};
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();
}
}
}
excel转换需要装虚拟打印机:https://download.csdn.net/download/a664347225/10307132,不然会报错说没打印机
如果只需要转换功能,这里有demo:https://download.csdn.net/download/a664347225/10307124
转换pdf后读取pdf页数需要用到一个jar包:https://download.csdn.net/download/a664347225/10307134
package net.wingsky.file.util;
import java.io.IOException;
import com.itextpdf.text.pdf.PdfReader;
public class GetPdfpage {
public static int getPdfPage(String filepath){
int pagecount = 0;
PdfReader reader;
try {
reader = new PdfReader(filepath);
pagecount= reader.getNumberOfPages();
} catch (IOException e) {
e.printStackTrace();
}
return pagecount;
}
}
package net.wingsky.file.util;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
String srcFilePath = "e:\\test.doc";
String pdfFilePath = "e:\\test.pdf";
WordToPdf.word2pdf(srcFilePath, pdfFilePath);
int pdfPage = GetPdfpage.getPdfPage(pdfFilePath);
System.out.println("页码数为:"+pdfPage);
}
}
完整项目Demo:https://download.csdn.net/download/a664347225/10307140