package mqdms.com.other;
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/*
* 注意word转pdf要安装虚拟打印机,且要配置
* 使用jacob框架,把dll文件放到jre/bin目录下
*/
public class Test {
private ActiveXComponent wordCom = null;
private Object wordDoc = null;
private final Variant False = new Variant(false);
private final Variant True = new Variant(true);
public static final int EXCEL_HTML = 44;
public static final int WORD_HTML = 8;
/**
* 打开word文档
*
*
@param filePath word文档
*
@return 返回word文档对象
*/
public boolean openWord(String filePath) {
//建立ActiveX部件
wordCom = new ActiveXComponent("Word.Application");
try {
//返回wrdCom.Documents的Dispatch
Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();
//调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc
wordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method,
new Object[] { filePath }, new int[1]).toDispatch();
return true;
}
catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
/**
* 关闭word文档
*/
public void closeWord(boolean saveOnExit) {
if (wordCom!=null) {
//关闭word文件
//Dispatch.call(wordDoc, "Close", new Variant(saveOnExit));
wordCom.invoke("Quit",new Variant[]{});
//wordCom.invoke("Quit",new Variant[0]);
wordCom=null;
//释放在程序线程中引用的其它com,比如Adobe PDFDistiller
ComThread.Release();
}
}
/**
* 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件
*
@param sourceFilePath
* 源文件路径
*
@param destinPSFilePath
* 首先生成的PS文件路径
*
@param destinPDFFilePath
* 生成PDF文件路径
*/
public void docToPDF(String sourceFilePath, String destinPSFilePath,
String destinPDFFilePath) {
if (!openWord(sourceFilePath)) {
closeWord(true);
return;
}
//建立Adobe Distiller的com对象
ActiveXComponent distiller = new ActiveXComponent("PDFDistiller.PDFDistiller.1");
try {
//设置当前使用的打印机,我的Adobe Distiller打印机名字为 "Adobe PDF"
wordCom.setProperty("ActivePrinter", new Variant("Adobe PDF"));
//设置printout的参数,将word文档打印为postscript文档。现在只使用了前5个参数,假如要使用更多的话可以参考MSDN的office开发相关api
//是否在后台运行
Variant Background = False;
//是否追加打印
Variant Append = False;
//打印所有文档
int wdPrintAllDocument = 0;
Variant Range = new Variant(wdPrintAllDocument);
//输出的postscript文件的路径
Variant OutputFileName = new Variant(destinPSFilePath);
Dispatch.callN((Dispatch) wordDoc,
"PrintOut", new Variant[] {
Background, Append, Range, OutputFileName });
System.out.println("由word文档打印为ps文档成功!");
//调用Distiller对象的FileToPDF方法所用的参数,具体内容参考Distiller Api手册
//作为输入的ps文档路径
Variant inputPostScriptFilePath = new Variant(destinPSFilePath);
//作为输出的pdf文档的路径
Variant outputPDFFilePath = new Variant(destinPDFFilePath);
//定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件
Variant PDFOption = new Variant("");
//调用FileToPDF方法将ps文档转换为pdf文档
Dispatch.callN(distiller, "FileToPDF", new Variant[] {inputPostScriptFilePath, outputPDFFilePath, PDFOption });
System.out.println("由ps文档转换为pdf文档成功!");
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
closeWord(true);
}
}
public static void excelToHtml(String xlsfile, String htmlfile)
{
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word
try
{
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
}
public static void wordToHtml(String docfile, String htmlfile)
{
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try
{
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
}
public static void main(String[] argv) {
// Test d2p = new Test();
//d2p.docToPDF("D:\\员工假期申请单(员工).doc", "D:\\员工假期申请单(员工).ps", "D:\\员工假期申请单.pdf");
//excelToHtml("D:\\通讯录2011.02.21.xls","D:\\通讯录.html");
// wordToHtml("F:\\资料\\员工登记表.doc", "F:\\资料\\员工登记表.html");
//boolean success = (new File("D:\\test.ps")).delete();
//if(success){
// System.out.println("删除打印机文件成功");
// }
}
}
分享到:
相关推荐
Jacob Word excel 转PDF 合并PDF文件,需要安装微软的转换插件,压缩包内含插件
资源中包括工具类、jacob.jar和jacob-1.18-x64.dll(64位)、jacob-1.18-x86.dll(32位) 在使用jacob时,我们需要将jacob-1.18-x64.dll或jacob-1.18-x86.dll放到jdk的bin目录下或者jdk/jre的bin目录下才可以使用。
jacob实例word excel pdf
jacob将word、excel转为pdf
使用jacob转换office为PDF文件,需要将jacob.dll放到jdk\bin目录下
jacob操作WPS、Office对应Word、Excel、PPT生成PDF,采用版本为jaco1.7X。
基于jacob插件实现 word,excel,ppt 转换 pdf,如下所示: public void excel2pdf(String source, String target) { System.out.println("启动Excel"); long start = System.currentTimeMillis(); ...
Java使用jacob将word和excel转为pdf,确保运行机器上安装了office或wps软件
jacob将word转pdf,它是直接调用Microsoft Office 或 WPS组件实现word、excel、PPT转换成PDF。
支持word、excel、ppt、wps、txt等多种格式转pdf、并支持pdf转word。个人感觉非常好用。
该项目通过jacob将word转换成pdf图片,在jdk中需要导入jacob的.dll文件,然后将项目导入到工具中直接运行即可,项目中有详细文档
NULL 博文链接:https://liumayulingyan.iteye.com/blog/1900122
jacob: 读取word;jxl:读取excel; 其中jxl可运行与windows和Linux下,jacob读取word完美读取, 不会有图片读取不了或者颜色样式丢失问题, 采用的支持jar基本都是最新版, 献给和我一样苦苦追寻的人
使用itextpdf实现txt转pdf操作;使用jacob实现word/excel转pdf文件操作(对于excel的实现并不理想)
使用jacob实现word\excel转pdf,excel可实现整个工作簿转换,并设置成服务固定刷新某个路径下的文件。包里同时提供所需JAR包、DLL文件、BAT文件和源代码。
java实现excel转pdf,无水印,破解版,多sheet合并pdf,可以一个sheet完全显示在一个pdf页上,也可excel原宽度不变,超出换页。
1、Windows下的几近完美的word、ppt、excel 转为pdf,pdf转为高清图片的解决方案。 2、包含完全实现的demo代码,经过多次调试。 3、使用技术jacob、GhostScript。
主要为大家详细介绍了Java使用jacob将微软office中word、excel、ppt转成pdf,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
word或者excel转各种格式,包括doc,Dot,txt,Docx,Pdf,htm等。jacob_1.9.zip压缩包内有所需要的jar包、jacob.dll,示例代码简单明了。
SaveAsPDFandXPS.exe和jacob word、ppt、excel利用jacob转换pdf工具