如题,,由于做的是政府相关部门的项目,,他们要支持国产... 都是安装金山的WPS。。 请教下高手,,有没有关于C#调用WPS打开word和excel模板(word和excel都是做好了的模板),将读取的数据替换到这些模板的标签中,然后再转成PDF文件保存在服务器端.... 有实例代码最好.. 谢谢了..
解决方案 »
- 请问一下,一个表多个字段排序的问题?
- 这样级联建表如何实现级联删除
- 求助GridView控件和TreeView控件两个问题(解决了包括这个贴还有另外几个贴的一共120分一并相送)
- ASP.NET 2.0问题——Webpart
- 帮我详细解释一段asp代码谢谢了这两天急用
- 用Login控件登录后 怎么获取用户信息?
- asp.net问题
- 关于vs签入签出的问题
- DataGrid的EditCommandColumn中的更新按钮提交时,因为有Form中有RequiredFieldValidator,且没有填写而不能提交,DataGrid又没有CausesV
- 请问:我的asp.net想在网页的根目录生成一个文件,但是程序报错说没有权限。怎么办??
- 大家帮忙看下这是什么问题,应该怎么解决!
- 有关HttpRequestWeb, Post提交传参数问题 oAuth, MSN账号登录
private string adobeDisPrint = "Acrobat Distiller";
private string regRoot = "SOFTWARE//Adobe//Acrobat Distiller//";
private string printerFileName = "acrodist.exe";
private string regName = "InstallPath";
/// <summary>
/// 获取acrodist.exe的安装路径
/// </summary>
/// <returns></returns>
private string GetAdobeDisFilePath()
{
RegistryKey regKey = null;
RegistryKey acrodistKey = null;
string printerName = string.Empty;
int i;
string regRootVersion = string.Empty; regKey = Registry.LocalMachine;
// acrodist的4-8版本适用
for (i = 4; i < 9; i++)
{
regRootVersion = string.Format("{0}{1}.0", regRoot, i);
acrodistKey = regKey.OpenSubKey(regRootVersion); if (acrodistKey != null)
{
printerName = acrodistKey.GetValue(regName, "") + "//" + printerFileName;
if (File.Exists(printerName))
{
return printerName;
}
}
}
throw new Exception("Acrobat Distiller printer not found!");
} /// <summary>
/// 获取Adobe Printer
/// "Adobe PDF" 或 "Acrobat Distiller"
/// </summary>
/// <returns></returns>
private string GetAdobePrinter()
{
foreach (string printername in PrinterSettings.InstalledPrinters)
{
if (printername.ToUpper().IndexOf(adobePdfPrint.ToUpper()) != -1 || printername.ToUpper().IndexOf(adobeDisPrint.ToUpper()) != -1)
{
return printername;
}
}
return string.Empty;
} public void ConvertWord2Pdf(string sourceFile)
{
if (PrinterSettings.InstalledPrinters.Count == 0)
throw new Exception("Did not find the printer, please install the printer."); if (!File.Exists(sourceFile))
throw new Exception(string.Format("File not found:{0}", sourceFile)); string strDir = Path.GetDirectoryName(sourceFile); string strName = Path.GetFileNameWithoutExtension(sourceFile); string prnFile = string.Format("{0}//{1}.prn",strDir,strName);
string pdfFile = string.Format("{0}//{1}.pdf",strDir,strName);
object objPrnFile = (object)prnFile;
object background = true;
object printToFile = true;
object collate = true;
object append = false;
object manualDuplexPrint = false;
object copies = false;
object range = Word.WdPrintOutRange.wdPrintAllDocument;
object missing = System.Reflection.Missing.Value;
string msg = string.Empty;
string adobePrinter = GetAdobePrinter(); if (adobePrinter.Length == 0)
throw new Exception("Did not find Adobe PDF printer or Acrobat Distiller printer, please install the printer.");
IWord word = new Word2003Factory().CreateWord();
// 打开Word文档
Word.Document doc = word.OpenDocument(sourceFile);
string oldPrint = doc.Application.ActivePrinter;
// 将Adobe Printer设置为默认打印机
doc.Application.ActivePrinter = adobePrinter;
DateTime start = DateTime.Now;
DateTime end = start.AddSeconds(20);
// Word文档打印到Prn文件
doc.PrintOut(ref background, ref append, ref range, ref objPrnFile, ref missing, ref missing,
ref missing, ref missing,ref missing, ref printToFile, ref collate, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
while (!File.Exists(prnFile))
{
if (DateTime.Now > end)
{
throw new Exception("Word document print to prn document overtime");
}
else
{
Application.DoEvents();
}
} doc.Application.ActivePrinter = oldPrint;
word.Close(false);
doc = null;
word = null; // Prn装PDF
Process objProcess = new Process();
objProcess.StartInfo.CreateNoWindow = true;
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.FileName = GetAdobeDisFilePath();
objProcess.StartInfo.Arguments = prnFile; start = DateTime.Now;
end = start.AddSeconds(20);
objProcess.Start();
while (!File.Exists(pdfFile))
{
if (DateTime.Now > end)
{
throw new Exception("Word document print to prn document overtime");
}
else
{
Application.DoEvents();
}
} objProcess.CloseMainWindow(); //if (File.Exists(prnFile))
//{
// File.Delete(prnFile);
//} /*
ACRODISTXLib.PdfDistiller pdfDis = new ACRODISTXLib.PdfDistiller(); pdfDis.FileToPDF(prnFile, pdfFile, string.Empty);
start = DateTime.Now;
end = start.AddSeconds(20);
while(!File.Exists(pdfFile))
{
if (DateTime.Now > end)
{
throw new Exception("prn document print to pdf document overtime");
}
else
{
Application.DoEvents();
}
}
pdfDis = null;
*/
}WORD转PDF
我就是要在服务器端转换成PDF啊,,我是想用WPS打开office,因为用office的word和excel做好了模板,,需要将数据库读取出的数据替换到模板中的标签,,然后再转成PDF到服务器磁盘上..
麻烦你给我贴下代码,,谢谢了啊..
to 子夜:
谢谢你的回答,,可是,貌似不是用WPS哦,,用户那边,服务端是不装office的.. - -
var app = WordContainer.OpenWord();
var word=WordContainer.OpenDocument(strFileName);
var seltn=app.Selection;
try
{
seltn.GoTo(-1,0,0,"书签名");
seltn.TypeText("值");
}catch(e){}
try
{
seltn.GoTo(-1,0,0,"receiveDate");
seltn.TypeText(getAttrValue(childNode,"recieveDate"));
}catch(e){}
document.all.WordContainer.WordDocument.Save();
word? pdf??
Aspose.Cell
Aspose.Pdf参考FortuneBase中的模板例子
参考地址www.cnblogs.com/mail-ricklee
wps的书签好不到.....俺以前弄过,用脚本找不到书签。 转而用office设置书签,就可以
现在是服务器端office,客户端wps这样没问题
至于转换pdf,俺这是用的在服务器端打开office然后采用虚拟打印,应该能借鉴,给你贴下吧,你试试
[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('.')[0]+".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('.')[0]+".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('.')[0]+"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('.')[0]+".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("(") + 1;
endpos = sProcName.IndexOf(")"); sProcName = sProcName.Substring(begpos, endpos - begpos); if (sProcName.ToLower().CompareTo("acrodist") == 0)
{
try
{
proc.Kill();
}
catch { }
break;
}
}
}
catch(Exception ex)
{ } return ret;
#endregion
}