private static PrintDocument PrepareDoc(this Report report, PrinterSettings settings = null) { if (report.PreparedPages.Count < 1) { report.Prepare(); if (report.PreparedPages.Count < 1) { return(null); } } var page = 0; var exp = new ImageExport { ImageFormat = ImageExportFormat.Png, Resolution = 600 }; var doc = new PrintDocument { DocumentName = report.Name }; if (settings != null) { doc.PrinterSettings = settings; } // Ajustando o tamanho da pagina doc.QueryPageSettings += (sender, args) => { var rPage = report.PreparedPages.GetPage(page); args.PageSettings.Landscape = rPage.Landscape; args.PageSettings.Margins = new Margins((int)(scaleFactor * rPage.LeftMargin * Units.HundrethsOfInch), (int)(scaleFactor * rPage.RightMargin * Units.HundrethsOfInch), (int)(scaleFactor * rPage.TopMargin * Units.HundrethsOfInch), (int)(scaleFactor * rPage.BottomMargin * Units.HundrethsOfInch)); args.PageSettings.PaperSize = new PaperSize("Custom", (int)(ExportUtils.GetPageWidth(rPage) * scaleFactor * Units.HundrethsOfInch), (int)(ExportUtils.GetPageHeight(rPage) * scaleFactor * Units.HundrethsOfInch)); }; doc.PrintPage += (sender, args) => { using (var ms = new MemoryStream()) { exp.PageRange = PageRange.PageNumbers; exp.PageNumbers = $"{page + 1}"; exp.Export(report, ms); args.Graphics.DrawImage(Image.FromStream(ms), args.PageBounds); } page++; args.HasMorePages = page < report.PreparedPages.Count; }; doc.EndPrint += (sender, args) => page = 0; doc.Disposed += (sender, args) => exp?.Dispose(); return(doc); }
public byte[] PrintResult(string userNo, int page) { try { Config.WebMode = true; Report rep = new Report(); rep.Load(Server.MapPath("~/App_Data/rptResult.frx")); using (VShapeUpDataContext _context = new VShapeUpDataContext()) { var data = (from x in _context.V_ChallengeResult_Reports where x.UserNo == userNo && x.Page == page select x).ToList().ToDataTable(); rep.RegisterData(data, "Report"); DataBand band = rep.FindObject("Data") as DataBand; band.DataSource = rep.GetDataSource("Report"); } if (rep.Report.Prepare()) { //PDFExport pdfExport = new PDFExport(); //pdfExport.ShowProgress = false; //pdfExport.Compressed = true; //pdfExport.AllowPrint = true; //pdfExport.EmbeddingFonts = true; //MemoryStream strm = new MemoryStream(); //rep.Report.Export(pdfExport, strm); //rep.Dispose(); //pdfExport.Dispose(); //strm.Position = 0; //return strm.ToArray(); ImageExport export = new ImageExport(); MemoryStream strm = new MemoryStream(); rep.Report.Export(export, strm); rep.Dispose(); export.Dispose(); strm.Position = 0; return(strm.ToArray()); } else { return(null); } } catch (Exception ex) { return(null); } }
public byte[] PrintAllChallenger() { try { Config.WebMode = true; Report rep = new Report(); rep.Load(Server.MapPath("~/App_Data/rptResult.frx")); using (VShapeUpDataContext _context = new VShapeUpDataContext()) { var data = (from x in _context.V_RegisterChallengeResult_Reports select x).ToList().ToDataTable(); rep.RegisterData(data, "Report"); DataBand band = rep.FindObject("Data") as DataBand; band.DataSource = rep.GetDataSource("Report"); band.Sort.Add(new Sort("[ReportData.UserNo]")); band.Sort.Add(new Sort("[ReportData.Page]")); } if (rep.Report.Prepare()) { ImageExport export = new ImageExport(); MemoryStream strm = new MemoryStream(); rep.Report.Export(export, strm); rep.Dispose(); export.Dispose(); strm.Position = 0; return(strm.ToArray()); } else { return(null); } } catch (Exception ex) { return(null); } }