最近做个项目需要将word转成PDF文件,实现方法如下
服务器上要安装转PDF插件和office2007,插件下载地址:
http://www.microsoft.com/zh-cn/download/details.aspx?id=7
C#代码:
using Word=Microsoft.Office.Interop.Word;
private void SaveAsPdf(string filepath)
{
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = false;
object fileName = filepath;
oDoc = oWord.Documents.Open(ref fileName,
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);
object newfileName = filepath.Remove(filepath.LastIndexOf("."));
object o = Word.WdSaveFormat.wdFormatPDF;
oDoc.SaveAs(ref newfileName, ref o,
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);
object save = false;
if (oDoc != null)
{
oDoc.Close(ref save, ref oMissing, ref oMissing);
oDoc = null;
}
if (oWord != null)
{
oWord.Quit(ref save, ref oMissing, ref oMissing);
oWord = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
本文介绍了一种使用C#将Word文档转换为PDF的方法。该方法需要在服务器上安装Office 2007及特定插件,并通过C#代码调用Word应用程序将文档保存为PDF格式。
874

被折叠的 条评论
为什么被折叠?



