2017-03-16 62 views
1

我在MVC中使用rdlc報告,在Visual Studio中運行時,打印操作完美工作,但發佈到設置在同一臺機器上的iis時,打印操作未發生。但是當我將報告作爲pdf返回時,它就在那裏,並且我可以使用javascript打印文件。但實際上我並不需要顯示報告,但想從服務器上打印。感謝提前給予幫助託管到iis時無法訪問打印機

public ActionResult GenerateOrder() 
    { 


     try 
     { 

      LocalReport report = new LocalReport(); 
      report.ReportPath = (Server.MapPath("~/Reports/Report1.rdlc")); 
      Export(report); 
      Print(); 
     } 
     catch (Exception Ex) 
     { 

     } 

     return View(); 
    } 

    private Stream CreateStream(string name, 
    string fileNameExtension, Encoding encoding, 
    string mimeType, bool willSeek) 
    { 
     Stream stream = new MemoryStream(); 
     m_streams.Add(stream); 
     return stream; 
    } 
    private void Export(LocalReport report) 
    { 
     string deviceInfo = 
      @"<DeviceInfo> 
      <OutputFormat>EMF</OutputFormat> 
      <PageWidth>8.5in</PageWidth> 
      <PageHeight>11in</PageHeight> 
      <MarginTop>0.25in</MarginTop> 
      <MarginLeft>0.25in</MarginLeft> 
      <MarginRight>0.25in</MarginRight> 
      <MarginBottom>0.25in</MarginBottom> 
     </DeviceInfo>"; 
     Warning[] warnings; 
     m_streams = new List<Stream>(); 
     report.Render("Image", deviceInfo, CreateStream, 
      out warnings); 
     foreach (Stream stream in m_streams) 
      stream.Position = 0; 
    } 
    private int m_currentPageIndex; 
    private IList<Stream> m_streams; 

    private void PrintPage(object sender, PrintPageEventArgs ev) 
    { 
     try 
     { 
      Metafile pageImage = new 
         Metafile(m_streams[m_currentPageIndex]); 

      // Adjust rectangular area with printer margins. 
      Rectangle adjustedRect = new Rectangle(
       ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX, 
       ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY, 
       ev.PageBounds.Width, 
       ev.PageBounds.Height); 

      // Draw a white background for the report 
      ev.Graphics.FillRectangle(Brushes.White, adjustedRect); 

      // Draw the report content 
      ev.Graphics.DrawImage(pageImage, adjustedRect); 

      // Prepare for the next page. Make sure we haven't hit the end. 
      m_currentPageIndex++; 
      ev.HasMorePages = (m_currentPageIndex < m_streams.Count); 
     } 
     catch (Exception ex) 
     { 

     } 

    } 
    private void Print() 
    { 
     if (m_streams == null || m_streams.Count == 0) 
      throw new Exception("Error: no stream to print."); 
     PrintDocument printDoc = new PrintDocument(); 
     if (!printDoc.PrinterSettings.IsValid) 
     { 
      throw new Exception("Error: cannot find the default printer."); 
     } 
     else 
     { 

      PrinterSettings pset = new PrinterSettings(); 
      printDoc.PrintPage += new PrintPageEventHandler(PrintPage); 
      m_currentPageIndex = 0; 
      printDoc.PrinterSettings.PrinterName = pset.PrinterName; 
      printDoc.Print(); 
     } 
    } 
+0

這可能會幫助你https://msdn.microsoft.com/en-us/library/aa290045(v=vs.71).aspx –

+1

感謝您的幫助@ pranav,我都試過,但沒有解決我的問題.. :( – sarath

+0

可以請你分享你的打印代碼嗎? –

回答

2

我想你想從服務器本身打印頁面。檢查您的應用程序正在運行的標識。如果它在默認帳戶下運行,請將其更改爲您的帳戶其他帳戶有打印機訪問權限。

轉到IIS, (一)首先找到應用程序池您的應用使用 (B),然後去應用程序池細節,並找到身份它正在使用。 (c)將此標識更改爲您的/某些具有打印機訪問權限的其他帳戶。

+0

謝謝你的幫助,我試過了爲管理員帳戶,但不工作.. :( – sarath

+0

你正在禁止所有的異常(空catch塊)。你可以在你的異常塊中添加一些日誌並找出它正在拋出的確切錯誤? –

+0

它改變了我改變管理員用戶正常用戶..謝謝你.. :) – sarath