мне нужно, чтобы обеспечить функциональность, чтобы преобразовать файл в формате 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) –