Sarathi J

Sarathi J

  • 0
  • 30
  • 812

C# WPF - RDLC Print directly to Printer - Collpases

Sep 27 2018 3:50 AM
I am using a ssrs client rdlc report (3-inch printer) in our WPF application. when the report rendered to.PDF and save it to a temporary path, and then prints the PDF means it works perfectly. But when I try to directly print the report to the printer, the report renders as Image (code for printing suggested from MSDN site), but prints only the half of the report in the printer and that too ugly to see as it looks like a drag.
 
Report Width : 3.5in Report Height: 7in
 
please take a look at the code below I use and suggest your opinions.
  1. // RDLC InvoicePrintReport - Report Ready with Data Source and Parameters  
  2. Export(InvoicePrintReport);  
  3. Print();  
  4. //Report Printing Section  
  5. private int m_currentPageIndex;  
  6. private IList<Stream> m_streams;  
  7. private Stream CreateStream(string name, string fileNameExtension, System.Text.Encoding encoding,  
  8. string mimeType, bool willSeek)  
  9. {  
  10. Stream stream = new MemoryStream();  
  11. m_streams.Add(stream);  
  12. return stream;  
  13. }  
  14. private void Export(LocalReport report)  
  15. {  
  16. string deviceInfo = @"<DeviceInfo><OutputFormat>EMF</OutputFormat><PageWidth>3.5in</PageWidth><MarginTop>0.01in</MarginTop><MarginLeft>0.01in</MarginLeft><MarginRight>0.1in</MarginRight><MarginBottom>0.01in</MarginBottom></DeviceInfo>";  
  17. Microsoft.Reporting.WinForms.Warning[] warnings;  
  18. m_streams = new List<Stream>();  
  19. report.Render("Image", deviceInfo, CreateStream, out warnings);  
  20. foreach (Stream stream in m_streams) stream.Position = 0;  
  21. }  
  22. private void Print()  
  23. {  
  24. if (m_streams == null || m_streams.Count == 0) throw new Exception("Error: no stream to print.");  
  25. PrintDocument printDoc = new PrintDocument();  
  26. if (!printDoc.PrinterSettings.IsValid)  
  27. {  
  28. throw new Exception("Error: cannot find the default printer.");  
  29. }  
  30. else  
  31. {  
  32. printDoc.PrintPage += new PrintPageEventHandler(PrintPage);  
  33. m_currentPageIndex = 0;  
  34. //PaperSize pkCustomSize1 = new PaperSize("First custom size", 100, 200);  
  35. //printDoc.DefaultPageSettings.PaperSize = pkCustomSize1;  
  36. printDoc.Print();  
  37. }  
  38. }  
  39. private void PrintPage(object sender, PrintPageEventArgs ev)  
  40. {  
  41. System.Drawing.Imaging.Metafile pageImage =  
  42. new System.Drawing.Imaging.Metafile(m_streams[m_currentPageIndex]);  
  43. // Adjust rectangular area with printer margins.  
  44. System.Drawing.Rectangle adjustedRect = new System.Drawing.Rectangle(  
  45. ev.PageBounds.Left - (int) ev.PageSettings.HardMarginX,  
  46. ev.PageBounds.Top - (int) ev.PageSettings.HardMarginY, ev.PageBounds.Width, ev.PageBounds.Height);  
  47. // Draw a white background for the report  
  48. ev.Graphics.FillRectangle(System.Drawing.Brushes.White, adjustedRect);  
  49. // Draw the report content  
  50. ev.Graphics.DrawImage(pageImage, adjustedRect);  
  51. // Prepare for the next page. Make sure we haven't hit the end.  
  52. m_currentPageIndex++;  
  53. ev.HasMorePages = (m_currentPageIndex < m_streams.Count);  
  54. }  

Answers (1)