您的位置:首页 > 编程语言

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: