我需要提供一个功能到RTF/WORD文件转换为PDF并将其作为附件在电子邮件中,为了这个,我试过代码如下所示:转换RTF文件,PDF在C#
// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
Document doc;
protected void Page_Load(object sender, EventArgs e)
{
ConvertToPDF("test.doc");
}
void ConvertToPDF(string sFileName)
{
// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
Document doc;
try
{
word.Visible = false;
word.ScreenUpdating = false;
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(".") + "\\TempDoc");
FileInfo[] wordFile = dirInfo.GetFiles(sFileName);
if (wordFile.Length > 0)
{
Object filename = (Object)wordFile[0].FullName;
// Use the dummy value as a placeholder for optional arguments
doc = word.Documents.Open2000(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
doc.Activate();
object outputFileName = wordFile[0].FullName.Replace(".doc", "");
object fileFormat = WdSaveFormat.wdFormatPDF;
// Save document into PDF Formats
doc.SaveAs2000(ref outputFileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
doc = null;
// word has to be cast to type _Application so that it will find
// the correct Quit method.
word = null;
}
}
但是它给出了 doc.SaveAs2000(ref outputFileName,ref fileFormat,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing); 声明。
这可能有理由,我们有Microsoft Office 2007,在这里,没有任何选项可以保存为PDF文件。在Microsoft Office 2010中,它具有该选项,以便在服务器上安装Microsoft Office 2010时可以使用此代码。
的可能的复制补丁[如何将RTF文件转换为pdf文件?](http://stackoverflow.com/questions/1853314/how-can-i-convert-an-rtf-file-to-a-pdf-file) – 2016-12-15 15:59:24