/// <summary>
        /// Export the given report as an EMF (Enhanced Metafile) file.
        /// </summary>
        /// <param name="report">The local report instance.</param>
        /// <param name="pageSettings">The page setting.</param>
        /// <returns>Returns true if success.</returns>
        private bool Export(LocalReport report, LocalReportPageSettings pageSettings)
        {
            MethodBase med = MethodBase.GetCurrentMethod();

            string deviceInfo = string.Empty;
            LocalReportPageSettings pgSettings = pageSettings;

            if (null == pgSettings)
            {
                pgSettings = new LocalReportPageSettings();
            }

            _ps = pgSettings;

            string pageTags = string.Empty;

            if (pageSettings.FromPage != -1 && pageSettings.ToPage != -1)
            {
                string pageTagsFmt = string.Empty;
                pageTagsFmt += "<StartPage>{0}</StartPage>";
                pageTagsFmt += "<EndPage>{1}</EndPage>";
                pageTags     = string.Format(pageTagsFmt,
                                             pageSettings.FromPage, pageSettings.ToPage);
            }

            string deviceInfofmt =
                "<DeviceInfo>" +
                "  <OutputFormat>EMF</OutputFormat>" +
                "  <PageWidth>{0}in</PageWidth>" +
                "  <PageHeight>{1}in</PageHeight>" +
                "  <MarginTop>{2}in</MarginTop>" +
                "  <MarginLeft>{3}in</MarginLeft>" +
                "  <MarginRight>{4}in</MarginRight>" +
                "  <MarginBottom>{5}in</MarginBottom>" +
                //"  <DpiX>{6}</DpiX>" +
                //"  <DpiY>{7}</DpiY>" +
                //"  <PrintDpiX>{8}</PrintDpiX>" +
                //"  <PrintDpiY>{9}</PrintDpiY>" +
                pageTags +
                "</DeviceInfo>";

            deviceInfo = string.Format(deviceInfofmt,
                                       pgSettings.PageWidth,
                                       pgSettings.PageHeight,
                                       pgSettings.MarginTop,
                                       pgSettings.MarginLeft,
                                       pgSettings.MarginRight,
                                       pgSettings.MarginBottom //,
                                                               //pgSettings.DpiX,
                                                               //pgSettings.DpiY,
                                                               //pgSettings.PrintDpiX,
                                                               //pgSettings.PrintDpiY
                                       );
            try
            {
                FreeStreams(); // Free exist image streams.
                Warning[] warnings;
                m_streams = new List <Stream>();
                report.Render("Image", deviceInfo, CreateStream, out warnings);
                foreach (Stream stream in m_streams)
                {
                    stream.Position = 0;
                }
            }
            catch (Exception ex)
            {
                med.Err(ex);

                RdlcMessageService.Instance.SendMessage(ex.ToString());

                FreeStreams();
            }

            return(true);
        }
        /// <summary>
        /// Print.
        /// </summary>
        /// <param name="report"></param>
        /// <param name="printerName"></param>
        /// <param name="pageSettings"></param>
        /// <param name="noOfCopies"></param>
        /// <returns></returns>
        public bool Print(LocalReport report, string printerName,
                          LocalReportPageSettings pageSettings, short noOfCopies = 1)
        {
            RdlcMessageService.Instance.SendMessage("Begin Render/Print process");

            MethodBase med = MethodBase.GetCurrentMethod();

            bool result = false;

            if (!Export(report, pageSettings))
            {
                RdlcMessageService.Instance.SendMessage("Cannot Render output.");
                return(result);
            }
            else
            {
                RdlcMessageService.Instance.SendMessage("Render output completed.");
            }

            string sPrinterName = printerName;

            //const string printerName = "Microsoft Office Document Image Writer";
            if (m_streams == null || m_streams.Count == 0)
            {
                return(result);
            }

            PrintDocument printDoc = new PrintDocument();

            if (!string.IsNullOrWhiteSpace(sPrinterName))
            {
                printDoc.PrinterSettings.PrinterName = sPrinterName;

                RdlcMessageService.Instance.SendMessage("Target Printer : " + printerName);
            }
            else
            {
                // It's should used default printer.
                RdlcMessageService.Instance
                .SendMessage("Not specificed printer name so Default printer should be used instead.");
            }
            if (!printDoc.PrinterSettings.IsValid)
            {
                string msg = String.Format(
                    "Can't find printer \"{0}\".", printerName);
                med.Err(msg);

                RdlcMessageService.Instance.SendMessage(msg);

                return(result);
            }

            // Set land scape.
            printDoc.DefaultPageSettings.Landscape = pageSettings.Landscape;
            // Set No of Copies.
            printDoc.DefaultPageSettings.PrinterSettings.Copies = noOfCopies;
            // Set From Page/To Page.
            //if (pageSettings.FromPage != -1)
            //    printDoc.DefaultPageSettings.PrinterSettings.FromPage = pageSettings.FromPage;
            //if (pageSettings.ToPage != -1)
            //    printDoc.DefaultPageSettings.PrinterSettings.ToPage = pageSettings.ToPage;

            // Hook handlers
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            try
            {
                //for (int i = 0; i < noOfCopies; ++i)
                {
                    m_currentPageIndex = 0; // Reset print index.
                    printDoc.Print();
                }
                // print OK.
                result = true;
            }
            catch (Exception ex)
            {
                med.Err(ex);

                RdlcMessageService.Instance.SendMessage(ex.ToString());
            }
            finally
            {
                FreeStreams();
            }

            RdlcMessageService.Instance.SendMessage("End Render/Print process");

            return(result);
        }