Just wanna share with u all about the direct printing in the C# windows application. This is little bit tricky for me at first because there are so many things about printing in C# that I need to learn. So, here is my code for direct printing. You can design your report in rdlc.
private void Print_Click(object sender, EventArgs e)
{
BasePrint printForm = new BasePrint();
ReportDataSource reportDataSource = new ReportDataSource();
printForm.Report.Reset();
printForm.Report.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportDataSource.Name = "General_PPT_pptAcception"; <--link to the object for data (the namespace: General.PPT.pptAcception
DataTable dt = Util.ConvertDataTable(pptTerima);
reportDataSource.Value = dt;
ReportViewer ReportViewer = new ReportViewer();
ReportViewer.LocalReport.DataSources.Clear();
ReportViewer.LocalReport.ReportEmbeddedResource = "PrintForm.rdlc";
ReportViewer.LocalReport.DataSources.Add(reportDataSource);
Export(ReportViewer.LocalReport);
Print(Convert.ToInt16(Copy.Text.Trim())); <--number of copy
}
Also include all this code:
#region direct printing
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
private Stream CreateStream(string name,
string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
// Export the given report as an EMF (Enhanced Metafile) file.
private void Export(LocalReport report)
{
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>210mm</PageWidth>
<PageHeight>280mm</PageHeight>
<MarginTop>0.1in</MarginTop>
<MarginLeft>0in</MarginLeft>
<MarginRight>0in</MarginRight>
<MarginBottom>0in</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;
}
// Handler for PrintPageEvents
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 Print(short pages)
{
if (m_streams == null || m_streams.Count == 0)
throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.Copies = pages;
if (!printDoc.PrinterSettings.IsValid)
{
throw new Exception("Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
}
}
#endregion
Thanks for the post!! I have a rdlc report which is then converted to a PDF of one page and is used as label, Now the problem is that i want to get multiple copies of the same page and the page number should be displayed in the format i.e.1/5,2/5... below every copies of the page! Please suggest!! Thanks in advance!!
ReplyDelete