Skip to content

Instantly share code, notes, and snippets.

@bozhink
Created November 7, 2017 10:59
Show Gist options
  • Save bozhink/c9b2aba06a749504ff861b266bf3cb0b to your computer and use it in GitHub Desktop.
Save bozhink/c9b2aba06a749504ff861b266bf3cb0b to your computer and use it in GitHub Desktop.
C# WinForms Report Printer
public class ReportPrinter : IDisposable
{
private int currentPageIndex;
private IList<Stream> streams;
private DataTable LoadSalesData()
{
// Create a new DataSet and read sales data file
// data.xml into the first DataTable.
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"..\..\data.xml");
return dataSet.Tables[0];
}
// Routine to provide to the report renderer, in order to
// save an image for each page of the report.
private Stream CreateStream(
string name,
string fileNameExtension,
Encoding encoding,
string mimeType,
bool willSeek)
{
Stream stream = new MemoryStream();
this.streams.Add(stream);
return stream;
}
// Export the given report as an EMF (Enhanced Metafile) file.
private void Export(LocalReport report)
{
string deviceInfo = Resources.PrintDeviceInfo;
this.streams = new List<Stream>();
report.Render("Image", deviceInfo, this.CreateStream, out Warning[] warnings);
foreach (Stream stream in this.streams)
{
stream.Position = 0;
}
}
// Handler for PrintPageEvents
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(this.streams[this.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.
this.currentPageIndex++;
ev.HasMorePages = this.currentPageIndex < this.streams.Count;
}
private void Print()
{
if (this.streams == null || this.streams.Count == 0)
{
throw new Exception(message: "Error: no stream to print.");
}
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid)
{
throw new Exception(message: "Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(this.PrintPage);
this.currentPageIndex = 0;
printDoc.Print();
}
}
// Create a local report for Report.rdlc, load the data,
// export the report to an .emf file, and print it.
public void Run(string reportPath, string reportDataSourceName, DataTable dataSourceValue)
{
LocalReport report = new LocalReport
{
ReportPath = reportPath
};
report.DataSources.Add(new ReportDataSource(reportDataSourceName, dataSourceValue));
this.Export(report);
this.Print();
}
public void Dispose()
{
if (this.streams != null)
{
foreach (Stream stream in this.streams)
{
stream.Close();
}
this.streams = null;
}
}
}
@bozhink
Copy link
Author

bozhink commented Nov 7, 2017

@bozhink
Copy link
Author

bozhink commented Nov 7, 2017

NuGet package Microsoft.Reporting.WinForms is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment