AIR调用外部程序代码

希望能在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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值