服务器端打开office然后采用虚拟打印

服务器端打开office然后采用虚拟打印

服务器端打开office然后采用虚拟打印 转换成pdf

 [WebMethod]
public bool ConvertWordTOPDF(string WordPath)
{
bool ret=false;
#region
//string dataName = collection[i].FileName;//本地文件名称(带路径)
//string ip=Request.UserHostAddress;
//string wordname=WordPath;
string wordPath=WordPath;
string pdfPath=wordPath.Split('.')[]+".pdf";
oWord._Document m_Document = null;
oWord._Application m_wordApplication = null;
object oMissing = Type.Missing;
oWord.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
try
{
object obj = System.Reflection.BindingFlags.InvokeMethod;
Type wordType = word.GetType();
oWord.Documents docs = word.Documents;
Type docsType = docs.GetType();
object objDocName = wordPath;
oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });
//打印输出到指定文件
Type docType = doc.GetType();
object printFileName =wordPath.Split('.')[]+".ps";
docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
object saveasPath =wordPath.Split('.')[]+"new.doc";
//必须另存为!
doc.SaveAs(ref saveasPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//必须关闭
doc.Close(ref savechanges, ref oMissing, ref oMissing);
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null,word, null);
//删除另存为文件
try
{
System.IO.File.Delete(saveasPath.ToString());
}
catch
{
}
string o1 = printFileName.ToString();//与生产的PS文件同步
string o2 = pdfPath;
string o3 = "";
//引用将PS转换成PDF的对象
try
{
ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
Type pdfType = pdf.GetType();
pdfType.InvokeMember("FileToPDF", System.Reflection.BindingFlags.InvokeMethod, null, pdf, new object[] { o1, o2, o3 });
pdf = null;
//System.IO.File.Delete( "123.log");//清除转换日志文件
}
catch
{ //throw new Exception("PS转PDF处出错!");
}
System.IO.File.Delete( printFileName.ToString());//清除ps文件 System.IO.File.Delete( printFileName.ToString().Split('.')[]+".log");//清除转换日志文件 if(System.IO.File.Exists(pdfPath))
{
ret=true;
}
//为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
foreach (Process proc in System.Diagnostics.Process.GetProcesses())
{
int begpos;
int endpos; string sProcName = proc.ToString();
begpos = sProcName.IndexOf("(") + ;
endpos = sProcName.IndexOf(")"); sProcName = sProcName.Substring(begpos, endpos - begpos); if (sProcName.ToLower().CompareTo("acrodist") == )
{
try
{
proc.Kill();
}
catch { }
break;
}
}
}
catch(Exception ex)
{ } return ret;
#endregion
}
04-28 05:26