简体   繁体   中英

Printing rdlc report without preview in web application in IIS mode

There is code in MSDN for printing rdlc without preview in web application but it does not work when i implement in IIS...is there any solution for this ....or any code which work in on print of rdlc without preview here is the link http://msdn.microsoft.com/en-us/library/ms252091.aspx It does not give any exception but also does not print the report on MSDN they said create a console application but I need it in asp.net web application which run in IIS.

using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Generic;
using Microsoft.Reporting.WebForms;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;

///<summary>
/// Summary description for Printing

/// this is the cool code found on MSDN site to print labels out without preview

/// abhay maini

///</summary>

publicclassPrinting : IDisposable

{

public Printing()
{

//

// TODO: Add constructor logic here

//

}

publicint m_currentPageIndex;
publicIList<Stream> m_streams;

// Routine to provide to the report renderer, in order to

// save an image for each page of the report.

publicStream CreateStream(string name,string fileNameExtension, Encoding encoding,string mimeType, bool willSeek)
{

string CurrentDrive;

CurrentDrive = Application.StartupPath.ToString();Stream stream = newFileStream("C:\\Labels\\" + name + "." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);



return stream;
}

publicvoid Export(LocalReport report)
{

string deviceInfo =
"<DeviceInfo>" +

" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>4.0in</PageWidth>" +

" <PageHeight>2.0in</PageHeight>" +
" <MarginTop>0.00in</MarginTop>" +

" <MarginLeft>0.00in</MarginLeft>" +
" <MarginRight>0.00in</MarginRight>" +

" <MarginBottom>0.00in</MarginBottom>" +
"</DeviceInfo>";

Warning[] warnings;m_streams = newList<Stream>();


report.Render("Image", deviceInfo, CreateStream, out warnings);

foreach (Stream stream in m_streams)
stream.Position = 0;

}

// Handler for PrintPageEvents

publicvoid PrintPage(object sender, PrintPageEventArgs ev)
{

Metafile pageImage = newMetafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);

m_currentPageIndex++;

ev.HasMorePages = (m_currentPageIndex < m_streams.Count);

}

publicvoid Print(string PrinterName)
{

// const string printerName = PrinterName;



if (m_streams == null || m_streams.Count == 0)
return;

PrintDocument printDoc = newPrintDocument();
printDoc.PrinterSettings.PrinterName = PrinterName;

if (!printDoc.PrinterSettings.IsValid)
{

string msg = String.Format(
"Can't find printer \"{0}\".", PrinterName);

MessageBox.Show(msg, "Print Error");return;
}

printDoc.PrintPage += newPrintPageEventHandler(PrintPage);
printDoc.Print();

}

// Create a local report for Report.rdlc, load the data,

// export the report to an .emf file, and print it.

publicvoid Run(string ReportName, string PrinterName, DataTable MyDataTable,string DSstring)
{

LocalReport report = newLocalReport();
report.ReportPath = ReportName;

report.DataSources.Clear();

report.DataSources.Add(newReportDataSource(DSstring, MyDataTable));
Export(report);

m_currentPageIndex = 0;

Print(PrinterName);

}

publicvoid Dispose()
{

if (m_streams != null)
{

foreach (Stream stream in m_streams)
stream.Close();

m_streams = null;
}

}



}





The above class can be called as below behind text change event:



protectedvoid TxtScanId_TextChanged(object sender, EventArgs e)
{

string sqlPrintScanID = "SELECT [ScanID], [LoadID], [tempVRMA], [CustPalletID], [TypeOfAsset] FROM [SerialScanDetail] WHERE [ScanID]=" + TxtScanId.Text + "";
string strConnection = ConfigurationManager.ConnectionStrings["RevisionConnectionString"].ToString();

SqlConnection conn = newSqlConnection(strConnection);
SqlDataAdapter da = newSqlDataAdapter();

da.SelectCommand = newSqlCommand(sqlPrintScanID, conn);
DataSet ds = newDataSet();

da.Fill(ds, "RevisionDataSet_SerialScanDetail");
//ReportViewer1.LocalReport.Refresh();

NewPrinting.Run(@"Reports\Report_ScanID.rdlc", "Eltron 2442", ds.Tables[0], "RevisionDataSet_SerialScanDetail");


}

I would suggest making use of the ReportViewer class.

        ReportViewer reportViewer = new ReportViewer();
        reportViewer.LocalReport.ReportPath = "ReportPath";
        reportViewer.LocalReport.DataSources.Add(new ReportDataSource("data", data));
        byte[] byteInfo;

        byteInfo = reportViewer.LocalReport.Render("Image", deviceInfo, CreateStream, out warnings);
        MemoryStream ms = new MemoryStream(byteInfo);
        Image returnImage = Image.FromStream(ms);

returnImage is then an image data type that you can display/print.

I see you making a call to render but not assigning the value to use, this combined with not using a report viewer could be the problem.

The reason MSDN suggest making a console application is that it is a good idea to do a proof of concept in a basic program such as a console, get the result you want, then port that code into your desired environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM