Example #1
0
  public static boolean ppt2PDF(String inputFile, String pdfFile) {
    try {
      ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
      // app.setProperty("Visible", msofalse);
      Dispatch ppts = app.getProperty("Presentations").toDispatch();

      Dispatch ppt =
          Dispatch.call(
                  ppts,
                  "Open",
                  inputFile,
                  true, // ReadOnly
                  true, // Untitled指定文件是否有标题
                  false // WithWindow指定文件是否可见
                  )
              .toDispatch();

      Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);

      Dispatch.call(ppt, "Close");

      app.invoke("Quit");
      return true;
    } catch (Exception e) {
      return false;
    }
  }
Example #2
0
 public static boolean word2PDF(String inputFile, String pdfFile) {
   try {
     // 打开word应用程序
     ActiveXComponent app = new ActiveXComponent("Word.Application");
     // 设置word不可见
     app.setProperty("Visible", false);
     // 获得word中所有打开的文档,返回Documents对象
     Dispatch docs = app.getProperty("Documents").toDispatch();
     // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
     Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
     // 调用Document对象的SaveAs方法,将文档保存为pdf格式
     /*
     Dispatch.call(doc,
                 "SaveAs",
                 pdfFile,
                 wdFormatPDF     //word保存为pdf格式宏,值为17
                 );
                 */
     Dispatch.call(
         doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF // word保存为pdf格式宏,值为17
         );
     // 关闭文档
     Dispatch.call(doc, "Close", false);
     // 关闭word应用程序
     app.invoke("Quit", 0);
     return true;
   } catch (Exception e) {
     return false;
   }
 }
Example #3
0
  @Override
  public void run() {
    for (int i = 0; i < 10; i++) {
      Dispatch range = Dispatch.get(dispatch, "Content").toDispatch(); // 取得当前文档的内容
      Dispatch.call(range, "Copy");

      Dispatch copyTo = Dispatch.call(wordAppCopyTo, "Selection").toDispatch();

      Dispatch textRange = Dispatch.get(copyTo, "Range").toDispatch();
      Dispatch.call(textRange, "Paste");
    }
  }
Example #4
0
 public static boolean excel2PDF(String inputFile, String pdfFile) {
   try {
     ActiveXComponent app = new ActiveXComponent("Excel.Application");
     app.setProperty("Visible", false);
     Dispatch excels = app.getProperty("Workbooks").toDispatch();
     Dispatch excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
     Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
     Dispatch.call(excel, "Close", false);
     app.invoke("Quit");
     return true;
   } catch (Exception e) {
     return false;
   }
 }
Example #5
0
  /**
   * @param path 打印路径地址,形如 \\XX\\YY.xls
   * @param copies 打印份数
   */
  public static void printExcel(String path, int copies) {
    if (path.isEmpty() || copies < 1) {
      return;
    }
    // 初始化COM线程
    ComThread.InitSTA();
    // 新建Excel对象
    ActiveXComponent xl = new ActiveXComponent("Excel.Application");
    Dispatch excel = null;
    try {
      System.out.println("Version=" + xl.getProperty("Version"));
      // 设置是否显示打开Excel
      Dispatch.put(xl, "Visible", new Variant(true));
      // 打开具体的工作簿
      Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
      excel = Dispatch.call(workbooks, "Open", path).toDispatch();

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

    } catch (RuntimeException e) {
      LOG.info("服务器运行时异常:{}", e.getMessage());
    } catch (Exception e) {
      LOG.info("处理打印出现异常:{}", e.getMessage());
      LOG.error("处理打印出现异常:{}.{}", e.getMessage(), e);
    } finally {
      if (excel != null) {
        // 关闭文档
        Dispatch.call(excel, "Close", new Variant(false));
      }
      if (xl != null) {
        xl.invoke("Quit", new Variant[0]);
      }
      // 始终释放资源
      ComThread.Release();
    }
  }
Example #6
0
  public void testScript2() {
    try {
      ComThread.InitSTA();
      ScriptTestSTA script = new ScriptTestSTA();
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ie) {
        // should we get this?
      }

      String scriptCommand = getSampleVPScriptForEval();
      // get a thread-local Dispatch from sCon
      Dispatch sc = script.sCon.toDispatch();

      // call a method on the thread-local Dispatch obtained
      // from the DispatchProxy. If you try to make the same
      // method call on the sControl object - you will get a
      // ComException.
      Variant result = Dispatch.call(sc, "Eval", scriptCommand);
      System.out.println("eval(" + scriptCommand + ") = " + result);
      script.quit();
      System.out.println("called quit");
    } catch (ComException e) {
      e.printStackTrace();
      fail("caught exception" + e);
    } finally {
      Integer I = null;
      for (int i = 1; i < 1000000; i++) {
        I = new Integer(i);
      }
      System.out.println(I);
      ComThread.Release();
    }
  }
Example #7
0
 /**
  * EXCEL转HTML
  *
  * @param xlsfile EXCEL文件全路径
  * @param htmlfile 转换后HTML存放路径
  */
 public void excelToHtml(String xlsfile, String htmlfile) {
   ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel
   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);
     System.out.println("wordtohtml转换成功");
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     app.invoke("Quit", new Variant[] {});
   }
 }
Example #8
0
 /**
  * WORD转HTML
  *
  * @param docfile WORD文件全路径
  * @param htmlfile 转换后HTML存放路径
  */
 public 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[] {});
   }
 }
Example #9
0
 private void closeDocument() {
   // 0 not save
   // -1 save
   // -2 prompt and require a confirmation(yes/no)
   Dispatch.call(document, "Close", new Variant(-2));
   document = null;
 }
Example #10
0
 /**
  * 向图形集区域中插入一张新的图片
  *
  * @param picPath
  * @return 插入的图片的InLineShape对象
  * @throws WordException
  */
 public InLineShape addPicture(String picPath) throws WordException {
   Dispatch dis = null;
   InLineShape shape = null;
   try {
     dis = Dispatch.call(dispatch, "AddPicture", picPath).toDispatch();
     shape = new InLineShape(dis);
   } catch (Exception ex) {
     throw new WordException("插入图形失败,原因:" + ex.getMessage());
   }
   return shape;
 }
Example #11
0
 /**
  * 根据序号获取文档中的内嵌图形对象
  *
  * <p>当对Word文档中的图形进行了删除、剪切、增加操作后文档中实际的图形数量已发生变化, 索引号将不再确定,使用时需要注意。
  *
  * @param index 内嵌图形对象的序号,从1开始计数。
  * @return 内嵌图形对象
  * @throws WordException
  */
 public InLineShape getInLineShape(int index) throws WordException {
   if (index < 1) {
     return null;
   }
   InLineShape shape = null;
   try {
     Dispatch shapeItem = Dispatch.call(dispatch, "Item", new Variant(index)).toDispatch();
     shape = new InLineShape(shapeItem);
   } catch (Exception ex) {
     throw new WordException("获取内嵌图形对象失败,原因:" + ex.getMessage());
   }
   return shape;
 }
Example #12
0
 @JRubyMethod(name = "[]", required = 1)
 public IRubyObject getPropertyWithBrackets(ThreadContext cxt, IRubyObject property) {
   return jacobToRuby(cxt, Dispatch.call(getDispatch(), "item", property.asJavaString()));
 }
Example #13
0
  public static void testMain(Object[] args) {

    objectmap map = new objectmap();

    axeInit("C:\\AXE_Projects\\TestHarnessAPI\\TestHarnessAPI.tpj", "debug");

    Dispatch.call(
        axe,
        "TestBegin",
        "Test4",
        "Try Database connect 1",
        "C:/AXE_Projects/TestHarnessAPI/data/Tests.xml",
        "Tests",
        "C:/AXE_Projects/TestHarnessAPI/results/debug/results.xml",
        "");
    try {
      //
      // C:/AXE_Projects/TestHarnessAPI/data/Tests.xml:SQLUser Test01
      Dispatch.call(
          axe,
          "SubtestBegin",
          "Test01",
          "",
          "C:/AXE_Projects/TestHarnessAPI/data/Tests.xml",
          "SQLUser");
      Dispatch.call(
          axe, "StepBegin", "[]SQLUser", "set", "select * from accounts where Id in (1,2)");
      Dispatch.put(axe, "ResultCode", 0);
      Dispatch.call(
          harness,
          "DbInitialiseQuery",
          Dispatch.call(axe, "GetRunCategoryOption", "connection", "MySqlLocal"),
          Dispatch.call(
              axe, "GetRunCategoryOptionProperty", "connection", "MySqlLocal", "provider"),
          "select * from accounts where Id in (1,2)",
          "System.Data.DataSet");
      axeStepEnd();

      Dispatch.call(axe, "StepBegin", "[]SQLUser", "Invoke", "");
      Dispatch.put(axe, "ResultCode", 0);
      Dispatch.call(harness, "DbInvoke");
      axeStepEnd();

      Dispatch.call(axe, "StepBegin", "Surname", "get", "");
      Dispatch.put(axe, "ResultCode", 0);
      Dispatch.put(axe, "Value", Dispatch.call(harness, "DbGetResultNodeText", "//Surname", false));
      axeStepEnd();

      Dispatch.call(axe, "StepBegin", "Surname", "val", "");
      Dispatch.put(axe, "ResultCode", 0);
      Dispatch.put(
          axe,
          "ResultCode",
          Dispatch.call(axe, "StepValidateEqual", "", Dispatch.get(axe, "Value")));
      axeStepEnd();
      Dispatch.call(axe, "StepBegin", "ReturnValue", "get", "");
      Dispatch.put(axe, "ResultCode", 0);
      Dispatch.put(axe, "Value", Dispatch.call(harness, "DbGetResult"));
      axeStepEnd();

      Dispatch.call(axe, "StepBegin", "ReturnValue", "val", "");
      Dispatch.put(axe, "ResultCode", 0);
      Dispatch.put(
          axe,
          "ResultCode",
          Dispatch.call(axe, "StepValidateEqual", "", Dispatch.get(axe, "Value")));
      axeStepEnd();
      Dispatch.call(axe, "StepBegin", "IdReturn", "get", "");
      Dispatch.put(axe, "ResultCode", 0);
      Dispatch.put(axe, "Value", Dispatch.call(harness, "DbGetResultNodeText", "//Id", false));
      axeStepEnd();

      Dispatch.call(axe, "StepBegin", "IdReturn", "val", "");
      Dispatch.put(axe, "ResultCode", 0);
      Dispatch.put(
          axe,
          "ResultCode",
          Dispatch.call(axe, "StepValidateEqual", "", Dispatch.get(axe, "Value")));
      axeStepEnd();
      axeSubtestEnd();
      //
      //
    } catch (Exception ex) {
      axeTestAbort(ex.getMessage());
      return;
    }
    Dispatch.call(axe, "TestEnd");
    unregisterAll();
  }
Example #14
0
 public WordTest(String pathForm, String pathTo) {
   wordApp.setProperty("Visible", new Variant(true));
   dispatch = Dispatch.call(dispatchWord, "Open", pathForm).toDispatch();
   wordAppCopyTo.setProperty("Visible", new Variant(true));
   dispatchCopyTo = Dispatch.call(dispatchWordCopyTo, "Open", pathTo).toDispatch();
 }
Example #15
0
 /**
  * Convenience method for calling a method.
  *
  * @param name method name
  * @return variant return value
  */
 public Variant call(String name) {
   return (Dispatch.call(m_dispatch, name));
 }
Example #16
0
 /**
  * Convenience method for calling a method.
  *
  * @param name method name
  * @param arg mathod argument
  * @return variant return value
  */
 public Variant call(String name, Object arg) {
   return (Dispatch.call(m_dispatch, name, arg));
 }
 /**
  * Wrapper for calling the ActiveX-Method with input-parameter(s).
  *
  * @param name an input-parameter of type String
  * @return the result is of type IITPlaylist
  */
 public IITPlaylist getItemByName(String name) {
   return new IITPlaylist(Dispatch.call(this, "ItemByName", name).toDispatch());
 }
 /**
  * Wrapper for calling the ActiveX-Method with input-parameter(s).
  *
  * @param highID an input-parameter of type int
  * @param lowID an input-parameter of type int
  * @return the result is of type IITPlaylist
  */
 public IITPlaylist getItemByPersistentID(int highID, int lowID) {
   return new IITPlaylist(
       Dispatch.call(this, "ItemByPersistentID", new Variant(highID), new Variant(lowID))
           .toDispatch());
 }
Example #19
0
 // create new word doc
 private void createNewDocument() {
   // get doc lists
   Dispatch documents = Dispatch.get(msWordApp, "Documents").toDispatch();
   document = Dispatch.call(documents, "Add").toDispatch();
 }
 /**
  * Wrapper for calling the ActiveX-Method with input-parameter(s).
  *
  * @param index an input-parameter of type int
  * @return the result is of type IITPlaylist
  */
 public IITPlaylist getItem(int index) {
   return new IITPlaylist(Dispatch.call(this, "Item", new Variant(index)).toDispatch());
 }
Example #21
0
 public void saveFileAs(String filename) {
   //		Dispatch.call(document, "SaveAs", filename);
   Dispatch.call(document, filename);
 }
Example #22
0
 public Variant GetChunk(int Length) {
   return Dispatch.call(this, "GetChunk", new Variant(Length));
 }
Example #23
0
 public void copy(Dispatch dispatchDoc, Dispatch selection) {
   Dispatch range = Dispatch.get(dispatchDoc, "Content").toDispatch(); // 取得当前文档的内容
   Dispatch.call(range, "Copy");
   Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
   Dispatch.call(textRange, "Paste");
 }
Example #24
0
 private void printFile() {
   // use the default printer
   Dispatch.call(document, "PrintOut");
 }
Example #25
0
 private void closeWord() {
   Dispatch.call(msWordApp, "Quit");
   msWordApp = null;
   document = null;
 }
Example #26
0
 public void AppendChunk(Variant Data) {
   Dispatch.call(this, "AppendChunk", Data);
 }
  public void returnNetworkState() {
    NativeResourceLoad.loadJacob();
    NetworkSettingsSave ret = NetworkSettingsSave.read(NetworkSavesConstants.ORIGINAL_STATE_FILE);
    Dispatch atEnd = null;
    String adapterQuery =
        "SELECT * FROM Win32_NetworkAdapter Where PhysicalAdapter='True' AND NetConnectionID=\"Local Area Connection\" ";
    String adaptConfigQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration ";

    ret.printData();

    if (ret != null) {
      //// Used to make ActiveX wmi component////
      String host = "localhost";
      String conn = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
      ActiveXComponent mActiveXWMI = new ActiveXComponent(conn);
      //////////////////////////////////////////

      String index = "";

      Variant vCollection = mActiveXWMI.invoke("ExecQuery", new Variant(adapterQuery));

      EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
      Dispatch item = null;
      while (enumVariant.hasMoreElements()) {
        item = enumVariant.nextElement().toDispatch();
        Iterator<String> it = WMI_NetworkAdapter.getIterator();
        while (it.hasNext()) {
          String tmp = it.next();
          if (tmp.endsWith("Caption")) {
            index = Dispatch.call(item, tmp).toString();
          }
        }

        if (ret.isAddapterEnabled()) {
          Dispatch.call(item, "Enable");
        } else {
          atEnd = item;
        }
      }

      vCollection =
          mActiveXWMI.invoke(
              "ExecQuery", new Variant(adaptConfigQuery + " Where Caption = \"" + index + "\""));
      enumVariant = new EnumVariant(vCollection.toDispatch());
      item = null;
      while (enumVariant.hasMoreElements()) {
        Variant var = enumVariant.nextElement();
        item = var.toDispatch();

        if (ret.isDhcpEnabled()) {
          Dispatch.call(item, "EnableDHCP");
        } else {
          Dispatch.call(
              item, "EnableStatic", new String[] {ret.getStaticIP()}, new String[] {ret.getMask()});
          Dispatch.call(item, "SetGateways", new String[] {ret.getDefauldGateway()}, null);
        }
        ArrayList<String> dnsOrder = new ArrayList<String>();
        if (ret.getDnsIP1() != null) {
          dnsOrder.add(ret.getDnsIP1());
        }
        if (ret.getDnsIP2() != null) {
          dnsOrder.add(ret.getDnsIP2());
        }
        if (!dnsOrder.isEmpty()) {
          Dispatch.call(item, "SetDNSServerSearchOrder", dnsOrder.toArray(), new String[] {});
        } else {
          Dispatch.call(item, "SetDNSServerSearchOrder", new String[] {}, new String[] {});
        }
      }

      if (atEnd != null) {
        Dispatch.call(atEnd, "Disable");
      }
    }
  }