Exemple #1
0
        /// <summary>
        /// 输出到打印机
        /// </summary>
        /// <param name="reportData"></param>
        public static void PrintToPrinter(string printerName, string documentName, I3ReportDatas reportDatas, int startPage, int endPage, bool paintPageIndex)
        {
            PrinterSettings ps = new PrinterSettings();

            ps.PrinterName = printerName;
            if (!ps.IsValid)
            {
                throw new Exception("打印机" + printerName + "不存在或不可用!");
            }

            if (string.IsNullOrEmpty(documentName))
            {
                documentName = "I3ReportDocument";
            }
            I3PrintDocument document = new I3PrintDocument(reportDatas, startPage, endPage);

            document.PaintPageIndex     = paintPageIndex;
            document.PrinterSettings    = ps;
            document.DocumentName       = documentName;
            document.QueryPageSettings += new QueryPageSettingsEventHandler(document_QueryPageSettings);
            document.PrintPage         += new PrintPageEventHandler(document_PrintPage);
            document.Print();

            #region 这段代码可显示默认的打印预览窗体。。。不好用。。。
            //PrintPreviewDialog ppd = new PrintPreviewDialog();
            //ppd.WindowState = FormWindowState.Maximized;//设定窗体最大化
            //ppd.PrintPreviewControl.Zoom = 1;
            //ppd.FormBorderStyle = FormBorderStyle.Fixed3D;
            //ppd.Document = document;
            //ppd.ShowDialog();
            #endregion
        }
Exemple #2
0
        static void document_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
        {
            I3PrintDocument document   = sender as I3PrintDocument;
            I3ReportData    reportData = document.ReportDatas.GetReportDataByAreaIndex(document.NextPage - 1);

            e.PageSettings.Landscape = reportData.PageSetting.PaperOrientation == PaperOrientation.横向;
            e.PageSettings.Margins   = new Margins(0, 0, 0, 0);
        }
Exemple #3
0
        static void document_PrintPage(object sender, PrintPageEventArgs e)
        {
            //float scale = (100F / 96F) * (600F / e.Graphics.DpiX);  //打印机600DPI对应屏幕96DPI  //这个转换与dpi无关
            float scale = (100F / 96F);

            float  phyOffsetX = 0;
            float  phyOffsetY = 0;
            IntPtr hdc        = e.Graphics.GetHdc();

            try
            {
                float width  = I3Win32PrintHelper.GetPaperWidth(hdc);  //纸张的宽度(打印机单位)
                float height = I3Win32PrintHelper.GetPaperHeight(hdc);

                phyOffsetX  = I3Win32PrintHelper.GetPaperPhyOffsetX(hdc); //纸张的X偏移(打印机单位)
                phyOffsetX  = phyOffsetX * e.PageBounds.Width / width;    //转换为 1/100 英寸
                phyOffsetX *= scale;                                      //转换为像素

                phyOffsetY  = I3Win32PrintHelper.GetPaperPhyOffsetY(hdc);
                phyOffsetY  = phyOffsetY * e.PageBounds.Height / height;
                phyOffsetY *= scale;
            }
            finally
            {
                e.Graphics.ReleaseHdc(hdc);
            }

            I3PrintDocument document   = sender as I3PrintDocument;
            I3ReportData    reportData = document.ReportDatas.GetReportDataByAreaIndex(document.NextPage - 1);
            I3PageSetting   setting    = reportData.PageSetting;

            if (document.NextPage > 0 && document.NextPage <= document.EndPage)
            {
                RectangleF rect = new RectangleF(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, e.MarginBounds.Height);
                rect.X -= phyOffsetX;
                rect.Y -= phyOffsetY;
                RectangleF contentRect = new RectangleF(setting.PaperContentRect.X * scale, setting.PaperContentRect.Y * scale,
                                                        setting.PaperContentRect.Width * scale, setting.PaperContentRect.Height * scale);
                contentRect.X -= phyOffsetX;
                contentRect.Y -= phyOffsetY;
                PrintAreaToGraphics(e.Graphics, scale, rect, contentRect, document.ReportDatas,
                                    document.ReportDatas.PrintAreas.Dic[document.NextPage - 1], document.PaintPageIndex, document.NextPage - 1);
                if (document.NextPage < document.EndPage)
                {
                    document.NextPage += 1;
                    e.HasMorePages     = true;
                }
                else
                {
                    document.NextPage = -1;
                    e.HasMorePages    = false;
                }
            }
            else
            {
                e.HasMorePages = false;
            }
        }