2017-08-17 185 views

回答

0

你可以先得到默認的打印機名稱(開題報告查看器前):

System.Drawing.Printing.PrinterSettings settings = new System.Drawing.Printing.PrinterSettings(); 
defaultPrinterName = settings.PrinterName; 

打印時,指定您的打印機名稱的PrintDocumentPrinterName屬性:

LocalReport rep = new LocalReport(); 
//set your data and parameters here 
//... 
rep.Refresh(); 
ExportLandscape(rep); 

PrintDocument printDoc = new PrintDocument(); 
PrinterSettings ps = new PrinterSettings(); 
ps.PrinterName = defaultPrinterName; 
printDoc.PrinterSettings = ps; 

printDoc.PrintPage += new PrintPageEventHandler(PrintPage); 
m_currentPageIndex = 0; 
printDoc.Print(); 

的事件來處理實際的打印:

//this has to declared somewhere at the "top": 
private IList<Stream> m_streams; 
private int m_currentPageIndex; 


private void PrintPage(object sender, PrintPageEventArgs ev) { 
     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); 
    } 

這裏是創建打印文檔的代碼:

private void ExportPortrait(LocalReport report) { 
      string deviceInfo = 
       @"<DeviceInfo> 
       <OutputFormat>EMF</OutputFormat> 
       <PageWidth>8.5in</PageWidth> 
       <PageHeight>11in</PageHeight> 
       <MarginTop>0.5in</MarginTop> 
       <MarginLeft>0.5in</MarginLeft> 
       <MarginRight>0.5in</MarginRight> 
       <MarginBottom>0.5in</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 void ExportLandscape(LocalReport report) { 
     string deviceInfo = 
      @"<DeviceInfo> 
      <OutputFormat>EMF</OutputFormat> 
      <PageWidth>11in</PageWidth> 
      <PageHeight>8.5in</PageHeight> 
      <MarginTop>0.5in</MarginTop> 
      <MarginLeft>0.5in</MarginLeft> 
      <MarginRight>0.5in</MarginRight> 
      <MarginBottom>0.5in</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 Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { 
      Stream stream = new MemoryStream(); 
      m_streams.Add(stream); 
      return stream; 
} 

你可以使用像this這樣的代碼轉換爲vb.net是必要的。

相關問題