AIR调用外部程序代码
2013-07-12 11:05
134 查看
希望能在AIR应用程序中调用外部的可执行程序,完成一些其它功能。比如在AIR应用中调用PDF Password Remover移除加密PDF的密码,如调用PDFTK将指定文档进行合并。解决思路:1 AIR调用外程序方法
使用的是AIR2中提供的NativeProcess来调用外部程序,具体的资料可参考Oreilly的《Flex 4 Cookbook》第18章19,20节。在此只列出关键点。
1.1为使程序能支持外部调用,先要修改配置文件xxx-app.xml,在<application>内,比如<id>节点之后添加:
<supportedProfiles>extendedDesktop desktop </supportedProfiles>
其中extendedDesktop desktop顺序不能变换,extendedDesktop需在前。2 代码示例:
2.1先声明应用程序变量:private var process:NativeProcess;
2.2可通过NativeProcess.isSupported来判断是否支持外部调用如:
if(NativeProcess.isSupported) {
callButton.enabled = true;
} else {
textReceived.text = "NativeProcess not supported.";
}
2.3调用的代码如:
private function callNativeApp():void {
var file:File = File.applicationDirectory;
file = file.resolvePath("NativeApps");
file = file.resolvePath("pdfdecrypt.exe");//外部程序名
var nativeProcessStartupInfo:NativeProcessStartupInfo =new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = file;
var v:Vector.<String> = new Vector.<String>();//外部应用程序需要的参数
v.push("-i");
v.push(txtSplit.text+"*.pdf"); //例:pdfdecrypt.exe -i C:\\NLCDownload\\*.pdf
nativeProcessStartupInfo.arguments = v;
process = new NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA,onStandardOutputData);//处理外部程序输出的内容
process.start(nativeProcessStartupInfo);
process.closeInput();
}
private function onStandardOutputData(event:ProgressEvent):void {
textReceived.text = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable );
}3 外部程序要放在应用程序文件夹的NativeApps文件下,在执行时会自动拷贝到debug下,或是在发布时,也会自动打包到AIR安装包中(使用FLEX)。
附:
1、pdf password remover去除密码命令行参数:(参考:http://www.verypdf.com/pwdremover/document/help.htm)
dfdecrypt -i C:\sample.pdf
-i [pdf file name] : decrypt PDF filename or directory.
-o [pdf file name] : PDF file will be generated.
If you not specify the -o parameter, the default output file will overwrite the input PDF file.
2、pdftk合并文档命令行参数:(参考:http://www.pdflabs.com/docs/pdftk-cli-examples/)
pdftk c:\NLCDownload\*.pdf output combined.pdf
在AIR中调用,如:
file = file.resolvePath("NOTEPAD.EXE");
var nativeProcessStartupInfo:NativeProcessStartupInfo=new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = file;
var v:Vector.<String> = new Vector.<String>();
//pdftk *.pdf output all.pdf
v.push(txtSplit.text+"*.pdf");
v.push("output");
v.push(txtSplit.text+"all.pdf");
nativeProcessStartupInfo.arguments = v;
process = new NativeProcess();
process.start(nativeProcessStartupInfo);
使用的是AIR2中提供的NativeProcess来调用外部程序,具体的资料可参考Oreilly的《Flex 4 Cookbook》第18章19,20节。在此只列出关键点。
1.1为使程序能支持外部调用,先要修改配置文件xxx-app.xml,在<application>内,比如<id>节点之后添加:
<supportedProfiles>extendedDesktop desktop </supportedProfiles>
其中extendedDesktop desktop顺序不能变换,extendedDesktop需在前。2 代码示例:
2.1先声明应用程序变量:private var process:NativeProcess;
2.2可通过NativeProcess.isSupported来判断是否支持外部调用如:
if(NativeProcess.isSupported) {
callButton.enabled = true;
} else {
textReceived.text = "NativeProcess not supported.";
}
2.3调用的代码如:
private function callNativeApp():void {
var file:File = File.applicationDirectory;
file = file.resolvePath("NativeApps");
file = file.resolvePath("pdfdecrypt.exe");//外部程序名
var nativeProcessStartupInfo:NativeProcessStartupInfo =new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = file;
var v:Vector.<String> = new Vector.<String>();//外部应用程序需要的参数
v.push("-i");
v.push(txtSplit.text+"*.pdf"); //例:pdfdecrypt.exe -i C:\\NLCDownload\\*.pdf
nativeProcessStartupInfo.arguments = v;
process = new NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA,onStandardOutputData);//处理外部程序输出的内容
process.start(nativeProcessStartupInfo);
process.closeInput();
}
private function onStandardOutputData(event:ProgressEvent):void {
textReceived.text = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable );
}3 外部程序要放在应用程序文件夹的NativeApps文件下,在执行时会自动拷贝到debug下,或是在发布时,也会自动打包到AIR安装包中(使用FLEX)。
附:
1、pdf password remover去除密码命令行参数:(参考:http://www.verypdf.com/pwdremover/document/help.htm)
dfdecrypt -i C:\sample.pdf
-i [pdf file name] : decrypt PDF filename or directory.
-o [pdf file name] : PDF file will be generated.
If you not specify the -o parameter, the default output file will overwrite the input PDF file.
2、pdftk合并文档命令行参数:(参考:http://www.pdflabs.com/docs/pdftk-cli-examples/)
pdftk c:\NLCDownload\*.pdf output combined.pdf
在AIR中调用,如:
file = file.resolvePath("NOTEPAD.EXE");
var nativeProcessStartupInfo:NativeProcessStartupInfo=new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = file;
var v:Vector.<String> = new Vector.<String>();
//pdftk *.pdf output all.pdf
v.push(txtSplit.text+"*.pdf");
v.push("output");
v.push(txtSplit.text+"all.pdf");
nativeProcessStartupInfo.arguments = v;
process = new NativeProcess();
process.start(nativeProcessStartupInfo);
相关文章推荐
- AIR调用外部程序代码
- 实现调用外部EXE程序的代码
- C#winform调用外部程序,等待外部程序执行完毕才执行下面代码
- java调用外部脚本或者程序,看了别人的代码,算是转载吧。
- winform c#调用外部EXE程序,并等待外部EXE程序执行完毕才执行下面的代码的方法。
- Java借助Runtime调用外部程序阻塞的代码
- vs2012 调用外部代码问题 打开项目一直加载外部dll是程序中断!!
- C#调用外部程序关键代码
- oracle 调用外部java程序代码
- 【转】oracle调用外部java程序代码
- flex air应用程序在windows环境下调用外部exe程序的一种方法
- asp.net调用外部exe程序
- java程序如何调用C++代码
- Python调用外部程序
- C#中利用Process类调用外部程序以及执行dos命令(转)
- C#客户端程序调用外部程序的3种实现方法
- Python调用(运行)外部程序
- C#程序调用外部程序
- 关于Asp.net调用外部程序的拒绝访问错误
- 从Ant调用系统命令或外部程序