2013-07-19 55 views
2

enter image description here將MS Word文檔轉換爲PDF後。如果我發送相同的路徑轉換爲pdf。即PDF。而文件提取我會得到一個錯誤的訪問被拒絕,同時在服務器中發佈的網站。它在本地機器的作品。有什麼我留在代碼中或有什麼我需要安裝。Word轉PDF轉換PDF有錯誤 - 訪問被拒絕錯誤

public string WordtoPdf_Input(string wordFileName_input) 
    { 

     try 
     { 

      Microsoft.Office.Interop.Word.Application appWord_input = new Microsoft.Office.Interop.Word.Application(); 
      object _MissingValue_ip = System.Reflection.Missing.Value; 
      //filename_doc = System.IO.Path.GetFileName(LblFleip.Text); 
      //wordFileName = LblFleip.Text; 
      string pdfFileName = string.Empty; 
      appWord_input.Visible = false; 
      appWord_input.ScreenUpdating = false; 

      // Cast as Object for word Open method 
      object filename = (object)wordFileName_input; 

      // Use the dummy value as a placeholder for optional arguments 
      Microsoft.Office.Interop.Word.Document doc = appWord_input.Documents.Open(ref filename, ref _MissingValue_ip, 
      ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, 
      ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, 
      ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip); 
      doc.Activate(); 

      InputFilename = pdfFileName = Path.ChangeExtension(wordFileName_input, ".pdf"); 
      object fileFormat = WdSaveFormat.wdFormatPDF; 

      //All is well until here, Save thinks the excelfile is readonly 
      object tmpName = Path.GetTempFileName(); 

      File.Delete(tmpName.ToString()); 

      // Save document into PDF Format 
      doc.SaveAs(ref tmpName, 
      ref fileFormat, ref _MissingValue_ip, ref _MissingValue_ip, 
      ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, 
      ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, 
      ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip); 

      // 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.  

      object saveChanges = WdSaveOptions.wdDoNotSaveChanges; 
      ((_Document)doc).Close(ref saveChanges, ref _MissingValue_ip, ref _MissingValue_ip); 
      doc = null; 

      // word has to be cast to type _Application so that it will find 
      // the correct Quit method. 
      ((_Application)appWord_input).Quit(ref _MissingValue_ip, ref _MissingValue_ip, ref _MissingValue_ip); 
      appWord_input = null; 


      File.Delete(InputFilename.ToString()); 

      File.Move(tmpName.ToString(), InputFilename.ToString()); 
      //File.Move(tmpName,InputFilename.ToString()) 
      filePath_input = InputFilename.ToString(); 


      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 

     } 


     catch (Exception ex) 
     { 

     } 
     finally 
     { 
      GC.Collect(); // force final cleanup! 
      GC.WaitForPendingFinalizers(); 
     } 
     return filePath_input; 
    } 

Public void PDFnumofPagecount() 
{ 
LblFleip.Text = filePath_input; 





       PdfReader readerPages_ip = new PdfReader(LblFleip.Text); 
    NumberofPages_ip = readerPages_ip.NumberOfPages; 
       txtbNumberofPages_ip.Text = NumberofPages_ip.ToString(); 
       readerPages_ip.Close(); 

} 

i have used itextsharp for reading pages and extraction of PDF 
+0

你能發佈異常的堆棧跟蹤嗎? –

回答

0

您是否檢查過發生錯誤的用戶的權限(以確保它們具有所需的讀/寫)?

+0

它給出。我已經給用戶的許可 – pdp

+0

我不明白爲什麼這個答案是downvoted。我贊成,因爲這是一個很好的反問題(但也許它應該被添加爲評論)。我很確定「訪問被拒絕」警告與權限有關。文件被鎖定,或者Web服務器作爲無權訪問文件的用戶運行。 –

+0

感謝布魯諾。 :) 我想評論,但不能由於沒有足夠的聲譽!所以,必須做它作爲答案... – Nostradamus