using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.Reporting.WebForms; using System.Net; using System.Collections.Specialized; using System.IO; using Spire.Pdf; using System.Globalization; using System.Text; using Newtonsoft.Json; using System.Drawing.Printing; using System.Drawing; using System.Diagnostics; using Aspose.Pdf.Facades; namespace EmisTerminal.EIF { public class Report { public static void PrintStudentScore(Guid userID) { string reportpath = "/EducationResult/GztyPersonalStudentScoreView"; List parameters = new List(); parameters.Add(new ReportParameter("LoginID", (string)null)); parameters.Add(new ReportParameter("UserID", userID.ToString())); parameters.Add(new ReportParameter("CollegeID", (string)null)); parameters.Add(new ReportParameter("StandardID", (string)null)); parameters.Add(new ReportParameter("YearID", (string)null)); parameters.Add(new ReportParameter("SchoolyearNumID", (string)null)); parameters.Add(new ReportParameter("SchoolcodeID", (string)null)); parameters.Add(new ReportParameter("ClassmajorID", (string)null)); PrintReport(reportpath, parameters); } public static string GenerateStudentScore(Guid userID, string idNum) { string reportpath = "/EducationResult/GztyPersonalStudentScoreView"; List parameters = new List(); parameters.Add(new ReportParameter("LoginID", (string)null)); parameters.Add(new ReportParameter("UserID", userID.ToString())); parameters.Add(new ReportParameter("CollegeID", (string)null)); parameters.Add(new ReportParameter("StandardID", (string)null)); parameters.Add(new ReportParameter("YearID", (string)null)); parameters.Add(new ReportParameter("SchoolyearNumID", (string)null)); parameters.Add(new ReportParameter("SchoolcodeID", (string)null)); parameters.Add(new ReportParameter("ClassmajorID", (string)null)); return GenerateReport(reportpath, idNum, parameters); } private static string GenerateReport(string reportpath, string idNum, List parameters = null) { string User = Const.LOCAL_SETTING_REPORT_USER_NAME; string Pass = Const.LOCAL_SETTING_REPORT_PASSWORD; string ReportServerUrl = Const.LOCAL_SETTING_REPORT_URL; IReportServerCredentials irsc = new CustomReportCredentials(User, Pass, ""); Uri uri = new Uri(ReportServerUrl); ReportViewer reportViewer = new ReportViewer(); reportViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote; reportViewer.ServerReport.ReportServerCredentials = irsc; reportViewer.ServerReport.ReportServerUrl = uri; reportViewer.ServerReport.ReportPath = reportpath; if (parameters != null && parameters.Count != 0) { reportViewer.ServerReport.SetParameters(parameters); } string deviceInfo = ReportPrintHelper.CreateEMFDeviceInfo(reportViewer.ServerReport); // Generating Image renderer pages one at a time can be expensive. In order // to generate page 2, the server would need to recalculate page 1 and throw it // away. Using PersistStreams causes the server to generate all the pages in // the background but return as soon as page 1 is complete. NameValueCollection firstPageParameters = new NameValueCollection(); firstPageParameters.Add("rs:PersistStreams", "True"); // GetNextStream returns the next page in the sequence from the background process // started by PersistStreams. NameValueCollection nonFirstPageParameters = new NameValueCollection(); nonFirstPageParameters.Add("rs:GetNextStream", "True"); string mimeType; string fileExtension; Stream pageStream = reportViewer.ServerReport.Render("PDF", deviceInfo, firstPageParameters, out mimeType, out fileExtension); PDFSign signControl = new PDFSign(); string filePath = signControl.Sign("StudentScore" + idNum, pageStream); return filePath; } private static void PrintReport(string reportpath, List parameters = null) { var fileString = GenerateReport(reportpath, "1045123", parameters); PdfDocument pdf = new PdfDocument(HttpContext.Current.Server.MapPath(fileString)); pdf.Print(); } public static void PrintFile(string reportFilePath, Guid userID) { PdfViewer viewer = new PdfViewer(); viewer.BindPdf(HttpContext.Current.Server.MapPath(reportFilePath)); viewer.PrintDocument(); viewer.Close(); string postString = "userID=" + userID.ToString(); byte[] postData = Encoding.UTF8.GetBytes(postString); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(Const.LOCAL_SETTING_EMIS_URL + "/PrintServices/Count"); req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; req.Referer = HttpContext.Current.Request.Url.AbsoluteUri; Stream reqStream = req.GetRequestStream(); reqStream.Write(postData, 0, postData.Length); reqStream.Close(); HttpWebResponse response = (HttpWebResponse)req.GetResponse(); StreamReader resReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string srcString = resReader.ReadToEnd(); var returnObj = JsonConvert.DeserializeObject>(srcString); if (!returnObj.IsSuccess) { throw new Exception(returnObj.Message); } } } public class CustomReportCredentials : IReportServerCredentials { private string _UserName; private string _PassWord; private string _DomainName; public CustomReportCredentials(string UserName, string PassWord, string DomainName) { _UserName = UserName; _PassWord = PassWord; _DomainName = DomainName; } public System.Security.Principal.WindowsIdentity ImpersonationUser { get { return null; } } public ICredentials NetworkCredentials { get { return new NetworkCredential(_UserName, _PassWord, _DomainName); } } public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority) { authCookie = null; user = password = authority = null; return false; } } }