//调用fso.GetBaseName(files(0))后,为无路径,无扩展名,的文件名 //files.length为文件参数的个数,使用循环可以支持多个WORD文档的转换
var docfile = files(0); var psfile = files(1) + fso.GetBaseName(files(0)) + ".ps"; var pdffile = files(1) + fso.GetBaseName(files(0)) + ".pdf"; var logfile = files(1) + fso.GetBaseName(files(0)) + ".log";
try{ var doc = word.Documents.Open(docfile); //WORD文件转成PS文件; word.PrintOut(false, false, 0, psfile); doc.Close(0);
//PS文件转成PDF文件; PDF.FileToPDF(psfile,pdffile,"");
fso.GetFile(psfile).Delete();//删除PS脚本文件 fso.GetFile(logfile).Delete();//删除转换的日志文件
word.Quit(); WScript.Echo("isuccess");//成功 WScript.Quit(0); } catch(x) { word.Quit(); WScript.Echo("isfail");//失败 WScript.Quit(0); }
然后测试该脚本程序
启动MS-DOS,输入如下命令:
c:\>cscript //nologo c:\ConvertDoc2PDF.js c:\test.doc c:\
说明:
运行成功后将看到test.pdf文档了
c:\test.doc参数对应的是脚本程序中的files(0)
c:\参数对应的是脚本程序中的files(1)
你可以安照该脚本改写成,支持多个参数,使用FOR循环,一次转换多个WORD文档,此处没有使用多个文件转换功能,是考虑到,该段脚本放在C#的线程中执行,这样一来也可以转换多个WORD文档.
四:使用C#调用ConvertDoc2PDF.js脚本
新建一个C#的WINDOWS应用程序,添加一个按钮button1
添加一个函数,函数名StartConvertPDF
public void StartConvertPDF() { Process proc = new Process(); proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.WorkingDirectory = @"c:\"; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; //输入重定向
proc.Start(); proc.StandardInput.WriteLine(@"cscript //nologo c:\ConvertDoc2PDF.js c:\test.doc c:\"); proc.StandardInput.WriteLine("exit"); proc.WaitForExit(); }
然后在按钮的CLICK事件中添加调用线程的代码 上一页 [1] [2] [3] [4] [5] 下一页 |