Hi eleveth,
Please take reference below code and correct your code.
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="450" Height="250">
</rsweb:ReportViewer>
<br />
<asp:Button Text="Print" runat="server" OnClick="Print" />
Namespaces
C#
using System.Collections.Generic;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using Microsoft.Reporting.WebForms;
VB.Net
Imports System.Data
Imports Microsoft.Reporting.WebForms
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Code
C#
private int pageIndex = 0;
private IList<Stream> streams;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
Customers dsCustomers = GetData();
ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
}
private Customers GetData()
{
using (Customers dsCustomers = new Customers())
{
DataTable dt = dsCustomers.Tables["DataTable1"];
dt.Rows.Add("ALFKI", "Maria", "Boise", "Germany");
dt.Rows.Add("ANATR", "Ana Trujillo", "México D.F.", "Mexico");
dt.Rows.Add("ANTON", "Antonio Moreno", "Montréal", "Mexico");
dt.Rows.Add("AROUT", "Thomas Hardy", "Mannheim", "Sweden");
dt.Rows.Add("BERGS", "Christina Berglund", "Luleå", "Sweden");
return dsCustomers;
}
}
protected void Print(object sender, EventArgs e)
{
Export(ReportViewer1.LocalReport);
pageIndex = 0;
Print();
}
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;
streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in streams)
{
stream.Position = 0;
}
}
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new FileStream(Server.MapPath("~/Files/") + name + "." + fileNameExtension, FileMode.Create);
streams.Add(stream);
return stream;
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(streams[pageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
pageIndex++;
ev.HasMorePages = (pageIndex < streams.Count);
}
private void Print()
{
if (streams == null || streams.Count == 0)
{
return;
}
PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
VB.Net
Dim pageIndex As Integer = 0
Dim streams As IList(Of Stream)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
Dim dsCustomers As Customers = GetData()
Dim datasource As ReportDataSource = New ReportDataSource("Customers", dsCustomers.Tables(0))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
End If
End Sub
Private Function GetData() As Customers
Dim dsCustomers As Customers = New Customers
Dim dt As DataTable = dsCustomers.Tables("DataTable1")
dt.Rows.Add("ALFKI", "Maria", "Boise", "Germany")
dt.Rows.Add("ANATR", "Ana Trujillo", "México D.F.", "Mexico")
dt.Rows.Add("ANTON", "Antonio Moreno", "Montréal", "Mexico")
dt.Rows.Add("AROUT", "Thomas Hardy", "Mannheim", "Sweden")
dt.Rows.Add("BERGS", "Christina Berglund", "Luleå", "Sweden")
Return dsCustomers
End Function
Protected Overloads Sub Print(ByVal sender As Object, ByVal e As EventArgs)
Export(ReportViewer1.LocalReport)
pageIndex = 0
Print()
End Sub
Private Sub Export(ByVal report As LocalReport)
Dim deviceInfo As String = "<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>"
Dim warnings() As Warning
streams = New List(Of Stream)
report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
For Each stream As Stream In streams
stream.Position = 0
Next
End Sub
Private Function CreateStream(ByVal name As String, ByVal fileNameExtension As String, ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
Dim stream As Stream = New FileStream((Server.MapPath("~/Files/") _
+ (name + ("." + fileNameExtension))), FileMode.Create)
streams.Add(stream)
Return stream
End Function
Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim pageImage As Metafile = New Metafile(streams(pageIndex))
ev.Graphics.DrawImage(pageImage, ev.PageBounds)
pageIndex = (pageIndex + 1)
ev.HasMorePages = (pageIndex < streams.Count)
End Sub
Private Overloads Sub Print()
If ((streams Is Nothing) _
OrElse (streams.Count = 0)) Then
Return
End If
Dim printDoc As PrintDocument = New PrintDocument
AddHandler printDoc.PrintPage, AddressOf Me.PrintPage
printDoc.Print()
End Sub
Screenshot