diff --git a/Payroll.Report/Class/PaySlip.cs b/Payroll.Report/Class/PaySlip.cs index 1a669b5..3881878 100644 --- a/Payroll.Report/Class/PaySlip.cs +++ b/Payroll.Report/Class/PaySlip.cs @@ -19,6 +19,7 @@ namespace Payroll.Report ObjectsTemplate _oGratuityParams = null; private DateTime _dSalaryMonth = DateTime.MinValue; private DateTime _dMonthEndDate = DateTime.MinValue; + private fReportViewer _mailViewer = null; DateTime _fiscalyearStart; DateTime _SalaryMonth; public PaySlip() @@ -3406,7 +3407,8 @@ namespace Payroll.Report val = Employee.Get(); List source = (List)(object)Designation.Get(EnumStatus.Regardless); DataRow dataRow = null; - fReportViewer fReportViewer2 = new fReportViewer(); + // Reused across calls on the same PaySlip: a new viewer recompiles the RDLC every time. + fReportViewer fReportViewer2 = _mailViewer ?? (_mailViewer = new fReportViewer()); DateTime dateTime = salaryMonth2; ObjectsTemplate val2 = new ObjectsTemplate(); val2 = SalaryProcess.Get(GlobalFunctions.LastDateOfMonth(dateTime)); diff --git a/Payroll.Report/Class/rptIncomeTax.cs b/Payroll.Report/Class/rptIncomeTax.cs index 743a8b4..c7f0f86 100644 --- a/Payroll.Report/Class/rptIncomeTax.cs +++ b/Payroll.Report/Class/rptIncomeTax.cs @@ -1826,70 +1826,101 @@ namespace Payroll.Report _TaxParameterID = taxParameterID; bool isCurrentYear = Payroll.BO.SystemInformation.CurrentSysInfo.TaxParamID == _TaxParameterID; - BeginTaxCardDoc(isCurrentYear); + // Setting up an EPDoc (A4 paper) queries the default printer and is slow, + // so one doc is kept per tax year and only its pages are cleared per employee. + if (m_oDoc == null || _pdfDocTaxParamID != taxParameterID.Integer) + { + BeginTaxCardDoc(isCurrentYear); + _pdfDocTaxParamID = taxParameterID.Integer; + } + else + { + m_oDoc.Pages.Clear(); + m_nCurPage = 0; + } AddTaxCardPages(oEmp, isCurrentYear); return RenderDocToPdf(m_oDoc); } + private int _pdfDocTaxParamID = -1; + /// - /// Renders an EPDoc's pages to an image-based A4 PDF in memory. - /// EPDoc.PrintDoc() always opens a PrintDialog, so this does the same setup it does - /// (setDevice + start page) via reflection and prints through a PreviewPrintController, - /// which records each page as a metafile instead of sending it to a printer. + /// Renders an EPDoc's pages to an image-based A4 PDF in memory, without a printer. + /// EPDoc can only output through a printer (and PrintDoc() always opens a PrintDialog), + /// so each page is drawn with the internal EPPage.Draw(Graphics) that EPDoc's PrintPage handler uses. /// private static byte[] RenderDocToPdf(EPDoc doc) { - const string pdfPrinter = "Microsoft Print to PDF"; - System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); - ps.PrinterName = pdfPrinter; - if (ps.IsValid) - doc.PrinterSettings.PrinterName = pdfPrinter; - - System.Reflection.BindingFlags bf = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance; - Type docType = typeof(EPDoc); - docType.GetMethod("setDevice", bf, null, Type.EmptyTypes, null).Invoke(doc, null); - int startPage = (int)docType.GetField("_startPage", bf).GetValue(doc); - docType.GetField("_curPageIndex", bf).SetValue(doc, startPage - 1); - - System.Drawing.Printing.PreviewPrintController controller = new System.Drawing.Printing.PreviewPrintController(); - doc.PrintController = controller; - doc.Print(); - System.Drawing.Printing.PreviewPageInfo[] pages = controller.GetPreviewPageInfo(); - if (pages == null || pages.Length == 0) + if (doc.Pages.Count == 0) return null; + System.Reflection.MethodInfo draw = typeof(EPPage).GetMethod("Draw", + System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, + null, new Type[] { typeof(Graphics) }, null); + const int dpi = 200; + // InitDoc always lays out A4 portrait: 8.27 x 11.69 inch + int w = 827 * dpi / 100; + int h = 1169 * dpi / 100; using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) + using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { + // 100 dpi + scale: drawing units are 1/100 inch (like a printer page) and + // point-sized fonts convert at 100 px/inch, so text matches the printed preview. + bmp.SetResolution(100, 100); iTextSharp.text.Document pdf = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0); iTextSharp.text.pdf.PdfWriter.GetInstance(pdf, ms); pdf.Open(); - foreach (System.Drawing.Printing.PreviewPageInfo page in pages) + for (int i = 0; i < doc.Pages.Count; i++) { - // PhysicalSize is in 1/100 inch - int w = page.PhysicalSize.Width * dpi / 100; - int h = page.PhysicalSize.Height * dpi / 100; - using (Bitmap bmp = new Bitmap(w, h)) + using (Graphics g = Graphics.FromImage(bmp)) { - bmp.SetResolution(dpi, dpi); - using (Graphics g = Graphics.FromImage(bmp)) - { - g.Clear(Color.White); - g.DrawImage(page.Image, 0, 0, w, h); - } - iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Png); - img.ScaleAbsolute(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height); - img.SetAbsolutePosition(0, 0); - pdf.NewPage(); - pdf.Add(img); + g.Clear(Color.White); + g.PageUnit = GraphicsUnit.Pixel; + g.ScaleTransform(dpi / 100f, dpi / 100f); + draw.Invoke(doc.Pages[i], new object[] { g }); } - page.Image.Dispose(); + iTextSharp.text.Image img = ToRgbImage(bmp); + img.ScaleAbsolute(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height); + img.SetAbsolutePosition(0, 0); + pdf.NewPage(); + pdf.Add(img); } pdf.Close(); return ms.ToArray(); } } + /// Raw 8-bit RGB image (much faster than PNG encoding; iText flate-compresses it). + private static iTextSharp.text.Image ToRgbImage(Bitmap bmp) + { + int w = bmp.Width, h = bmp.Height; + System.Drawing.Imaging.BitmapData bd = bmp.LockBits(new Rectangle(0, 0, w, h), + System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); + byte[] row = new byte[bd.Stride]; + byte[] rgb = new byte[w * h * 3]; + try + { + for (int y = 0; y < h; y++) + { + System.Runtime.InteropServices.Marshal.Copy(new IntPtr(bd.Scan0.ToInt64() + (long)y * bd.Stride), row, 0, bd.Stride); + int o = y * w * 3; + for (int i = 0; i < w * 3; i += 3) + { + // bitmap rows are BGR + rgb[o + i] = row[i + 2]; + rgb[o + i + 1] = row[i + 1]; + rgb[o + i + 2] = row[i]; + } + } + } + finally + { + bmp.UnlockBits(bd); + } + return iTextSharp.text.Image.GetInstance(w, h, 3, 8, rgb); + } + private void BeginTaxCardDoc(bool isCurrentYear) { _taxParameter = TaxParameter.Get(_TaxParameterID); diff --git a/Payroll.Report/UI/fReportViewer.cs b/Payroll.Report/UI/fReportViewer.cs index 40c1154..36e2083 100644 --- a/Payroll.Report/UI/fReportViewer.cs +++ b/Payroll.Report/UI/fReportViewer.cs @@ -2176,6 +2176,8 @@ namespace Payroll.Report ReportViewer.LocalReport.DataSources.Add(new ReportDataSource(dMain.TableName, dMain)); + // -= first: this viewer may be reused for many payslips (PaySlip.MailReportWithAccountNoMask) + ReportViewer.LocalReport.SubreportProcessing -= new SubreportProcessingEventHandler(LocalReport_SubreportProcessing); ReportViewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing); ReportViewer.LocalReport.ReportEmbeddedResource = RDLC;