首页新闻找找看学习计划

使用jacob将word转换为PDF,转换前如何接受所有修订?

0
悬赏园豆:50 [待解决问题]

1.环境: JDK1.6 jacob-1.18
2.接受所有修订,尝试及对应错误信息

(1) Dispatch.call(doc, "AcceptAllRevisions");

(2) Dispatch.put(doc, "AcceptAllRevisions", true);

3.源代码:

import java.io.File;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**

  • Function: word转换pdf. <br/>
  • date: 2017年5月15日 下午4:53:16 <br/>
  • @author Qin-Haojun
  • @version
  • @since JDK 1.6
    */
    public class WordPdfUtil {

    private static final Log log = Logs.get();

    // 线程池
    private static ExecutorService executor = Executors.newFixedThreadPool(5);

    public static boolean word2PDFJacob(String inputFile,String pdfFile) {
    log.info("Word转PDF开始启动...");
    long start = System.currentTimeMillis();
    ActiveXComponent app = null;
    Dispatch doc = null;
    try {
    //多线程
    ComThread.InitMTA(true);
    //单线程
    //ComThread.InitSTA();
    app = new ActiveXComponent("Word.Application");
    // 设置word不可见
    app.setProperty("Visible", new Variant(false));
    // 打开word文件
    Dispatch docs = app.getProperty("Documents").toDispatch();
    log.info("打开文档:" + inputFile);
    doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {
    inputFile, new Variant(false),new Variant(true),new Variant(false),new Variant("pwd") }, new int[1]).toDispatch();
    File tofile = new File(pdfFile);
    if (tofile.exists()) {
    tofile.delete();
    }
    Dispatch.put(doc, "AcceptAllRevisions", true); //com.jacob.com.ComFailException: Invoke of: AcceptAllRevisions
    // Dispatch.call(doc, "AcceptAllRevisions"); //Invoke of: AcceptAllRevisions Source: Microsoft Word Description: 此方法或属性无效,因为 该命令不可用于读取
    // Dispatch.call(doc, "AcceptAllChangesInDoc"); //Can't map name to dispid: AcceptAllChangesInDoc
    // Dispatch.call(doc, "AcceptAllRevisions");
    //作为html格式保存到临时文件::参数 new Variant(8)其中8表示word转html;7表示word转txt;44表示Excel转html;17表示word转成pdf。。
    Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
    pdfFile, new Variant(17) }, new int[1]);
    long end = System.currentTimeMillis();
    log.info("转换完成..用时:" + (end - start) + "ms.");
    return true;
    } catch (Exception e) {
    e.printStackTrace();
    log.info("Error:文档转换失败:" + e.getMessage());
    return false;
    } finally {
    // 关闭word
    if (doc != null) {
    //false 关闭不保存; true 关闭并保存
    Dispatch.call(doc,"Close",new Variant(false));
    log.info("关闭文档");
    }
    if (app != null) {
    app.invoke("Quit",0);
    }
    //如果没有这句话,winword.exe进程将不会关闭
    ComThread.Release();
    }

    }

    private static class Word2PDFConvertor implements Callable<Boolean> {

     private String inputFilePath;
     private String pdfFilePath;
    
     public Word2PDFConvertor(String inputFilePath, String pdfFilePath) {
         this.inputFilePath = inputFilePath;
         this.pdfFilePath = pdfFilePath;
     }
    
     @Override
     public Boolean call() throws Exception {
         return word2PDFJacob(this.inputFilePath, this.pdfFilePath);
     }

    }

    // 线程池转换方法
    public static boolean word2PDF(String inputFile, String pdfFile){
    Future<Boolean> success = executor.submit(new Word2PDFConvertor(inputFile, pdfFile));
    try{
    return success.get(180*1000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
    return false;
    } catch (ExecutionException e) {
    e.printStackTrace();
    return false;
    } catch (TimeoutException e) {
    ProcessUtil.killAllFind(inputFile);
    return false;
    }
    }

    public static void main(String[] args) {
    String fileName="d:/test.docx";
    String pdfName="d:/test.pdf";
    word2PDF(fileName,pdfName);
    }
    }

JenMinZhang的主页 JenMinZhang | 初学一级 | 园豆:152
提问于:2019-05-17 09:38
< >
分享
所有回答(1)
0

Word接收修订,可以参考:http://www.e-iceblue.cn/spiredocforjavaoperating/word-trackchanges-using-java.html ,接受修订后再转换成PDF, 这里saveToFile方法可以直接保存为PDF格式

import com.spire.doc.*;

public class TrackChanges {
    public static void main(String[] args){
       //加载测试文档
        Document doc = new Document("test.docx");

        //判断文档是否有修改
        if (doc.hasChanges())

            //接受修订
            doc.acceptChanges();         

        //保存文档
        doc.saveToFile("result.pdf",FileFormat.PDF);
    }
}
KeepLearning_88 | 园豆:218 (菜鸟二级) | 2019-05-22 13:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册