2 u011235913 u011235913 于 2015.06.16 15:24 提问

求大神 帮我看看问题 Java中 调用虚拟打印机 把word转存为PDF
 public class SaveWordAspdf {


    public static void saveWordAspdf(String filePath, String outFile) throws Exception {
            ComThread.InitSTA();
            ActiveXComponent actcom = new ActiveXComponent("Word.Application");
            Dispatch documents = null;
            Dispatch document = null;

            try {           
                actcom.setProperty("Visible", new Variant(false));

                documents = actcom.getProperty("Documents").toDispatch();
                document = Dispatch.invoke(documents,"Open", Dispatch.Method, new Object[] {
                        filePath,  new Variant(false), new Variant(false) 
                 }, new int[1]).toDispatch();

                Dispatch.invoke(document, "SaveAs", Dispatch.Method, new Object[] {
                        outFile,new Variant(false)},new int[1]);

                Dispatch.call(document, "Close", new Object[] { new Variant(false) });
                if (actcom != null) {
                    actcom.invoke("Quit", new Variant[] {});
                    actcom = null;
                }
                ComThread.Release();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (actcom != null) {
                    actcom.invoke("Quit", new Variant[] {});
                    actcom = null;
                }
                ComThread.Release();
            }
        }


    public  static void  print(String path)throws Exception {   
            String PRINT_NAME="pdfFactory Pro";//打印机名称
            //初始化COM线程
            ComThread.InitSTA();
            ActiveXComponent doc=new ActiveXComponent("Word.Application");
            try { 

                //设置是否显示打开word  
                Dispatch.put(doc, "Visible", new Variant(true));
                //打开具体的工作簿
                Dispatch documents = doc.getProperty("Documents").toDispatch(); 
                Dispatch word = Dispatch.call(documents,"Open",path).toDispatch(); 

                //设置打印属性并打印
                Dispatch.callN(word,"PrintOut",new Object[]{Variant.VT_MISSING, Variant.VT_MISSING, new Integer(1),
                        new Boolean(false),PRINT_NAME, new Boolean(true),Variant.VT_MISSING, ""});

                //关闭文档
               Dispatch.call(word, "Close", new Variant(false));  
            } catch (Exception e) { 
                e.printStackTrace();
                throw new Exception();
            } finally{
                doc.invoke("Quit",new Variant[0]);
                //始终释放资源 
                ComThread.Release(); 
            } 
        }
    public static  void main(String[] args) {
        try {
            System.out.println(0);
            saveWordAspdf("d:/1234.doc","d:/1234.pdf");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

2个回答

qq_20099317
qq_20099317   2016.11.25 19:11

你好,我试了你的代码,请问你这里用了哪些jar包呢,可否发给我。谢谢

qq_20099317
qq_20099317   2016.11.25 20:08

你好,我使用了你的代码,转换出来了一个pdf文件,但是这个pdf文件是损毁的,打不开啊...请问你是怎么解决的?

Csdn user default icon
上传中...
上传图片
插入图片