2012-04-21 61 views
2

我需要提供一個功能到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時可以使用此代碼。

+0

的可能的複製補丁[如何將RTF文件轉換爲pdf文件?](http://stackoverflow.com/questions/1853314/how-can-i-convert-an-rtf-file-to-a-pdf-file) – 2016-12-15 15:59:24

回答

1

是的,它在2010年的工作,我最近使用過它,但我相信這是對2007添加保存爲PDF格式的功能太

也許嘗試這個http://msdn.microsoft.com/en-us/library/bb412305(v=office.12).aspx

+0

I我正在試試這個方法。非常感謝你。 – 2012-04-21 12:15:43

+0

檢索具有CLSID {000209FF-0000-0000-C000-000000000046}的組件的COM類工廠失敗,原因是出現以下錯誤:80070005 .......我在應用程序中遇到此錯誤,之前我做了一個演示將docx轉換爲pdf的項目。你有什麼關於硫的知識.. ?? – 2012-04-24 09:54:32