1.打开CMD
cscript //nologo js文件路径 待转文件 转换存贮路径
2.启动MS-DOS,输入如下命令:
cscript //nologo c:\ConvertDoc2PDF.js c:\test.doc c:\
c#代码
strJS:脚步字符串
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.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.StandardInput.WriteLine(strJS);
proc.StandardInput.WriteLine(“exit”);
proc.WaitForExit();
proc.Close();
ConvertDoc2PDF.js 代码:
var files = WScript.Arguments;
var fso = new ActiveXObject(“Scripting.FileSystemObject”);
var word = new ActiveXObject(“Word.Application”);
var PDF = new ActiveXObject(“PDFDistiller.PDFDistiller6”);
word.ActivePrinter = “Adobe PDF”;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.PrintOut(false, false, 0, psfile);
doc.Close(0);PDF.FileToPDF2(psfile,pdffile,””,true);
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);
}