コード例 #1
0
ファイル: Form1.cs プロジェクト: xieyitong/PrintService
        private void SetPrint(string sysiniPath, string reportTemplate)
        {
            //1.判断sysini配置文件是否存在
            if (File.Exists(sysIniPath)) //存在
            {
                //2.是否存在指定模板的打印机
                var printName = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, reportTemplate, null);
                if (string.IsNullOrEmpty(printName) || string.IsNullOrEmpty(LocalPrinter.GetPrinterByName(printName)))
                {
                    PrintDocument printDocument = new PrintDocument();
                    //设置打印机,记录设置信息
                    PrintDialog printDialog = new PrintDialog();
                    printDialog.Document          = printDocument;
                    printDialog.UseEXDialog       = true;
                    printDocument.OriginAtMargins = true;
                    //printDocument.DefaultPageSettings.PaperSize.Kind = System.Drawing.Printing.PaperKind.Custom;
                    this.Invoke(new Action(() =>
                    {
                        var result = printDialog.ShowDialog();
                        if (DialogResult.OK == result) //如果确认,将会覆盖所有的打印参数设置
                        {
                            //页面设置对话框(可以不使用,其实PrintDialog对话框已提供页面设置)
                            PageSetupDialog psd = new PageSetupDialog();
                            psd.Document        = printDocument;
                            psd.ShowDialog();
                        }
                        //1设置默认打印机
                        Externs.SetDefaultPrinter(printDocument.PrinterSettings.PrinterName);
                        //2设置打印机名称
                        INIHepler.INIWriteValue(sysIniPath, reportTemplate, reportTemplate, printDocument.PrinterSettings.PrinterName);
                        //3设置打印机打印方向
                        INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetLandscape", printDocument.DefaultPageSettings.Landscape.ToString());
                        //4设置纸张名称 A3等
                        INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperName", printDocument.DefaultPageSettings.PaperSize.PaperName);
                        //5设置下边距
                        INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsBottom", printDocument.DefaultPageSettings.Margins.Bottom.ToString());
                        //6设置上边距
                        INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsTop", printDocument.DefaultPageSettings.Margins.Top.ToString());
                        //7设置左边距
                        INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsLeft", printDocument.DefaultPageSettings.Margins.Left.ToString());
                        //8设置右边距
                        INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsRight", printDocument.DefaultPageSettings.Margins.Right.ToString());
                        //9设置纸张高度
                        INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperHeight", printDocument.DefaultPageSettings.PaperSize.Height.ToString());
                        //10设置纸张宽度
                        INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperWidth", printDocument.DefaultPageSettings.PaperSize.Width.ToString());
                    }));
                }
            }
            else //不存在打印设置
            {
                //3.不存在进行打印设置,并执行打印操作,记录设置信息
                PrintDocument printDocument = new PrintDocument();
                PrintDialog   printDialog   = new PrintDialog();
                printDialog.Document          = printDocument;
                printDialog.UseEXDialog       = true;
                printDocument.OriginAtMargins = true;
                // printDocument.DefaultPageSettings.PaperSize.Kind = System.Drawing.Printing.PaperKind.Custom;


                this.Invoke(new Action(() =>
                {
                    var result = printDialog.ShowDialog();
                    if (DialogResult.OK == result) //如果确认,将会覆盖所有的打印参数设置
                    {
                        //页面设置对话框(可以不使用,其实PrintDialog对话框已提供页面设置)
                        PageSetupDialog psd = new PageSetupDialog();
                        psd.Document        = printDocument;
                        psd.ShowDialog();
                    }
                }));
                //1设置默认打印机
                Externs.SetDefaultPrinter(printDocument.PrinterSettings.PrinterName);
                //2设置打印机名称
                INIHepler.INIWriteValue(sysIniPath, reportTemplate, reportTemplate, printDocument.PrinterSettings.PrinterName);
                //3设置打印机打印方向
                INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetLandscape", printDocument.DefaultPageSettings.Landscape.ToString());

                //4设置纸张名称 A3等
                INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperName", reportTemplate);
                //5设置下边距
                INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsBottom", printDocument.DefaultPageSettings.Margins.Bottom.ToString());
                //6设置上边距
                INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsTop", printDocument.DefaultPageSettings.Margins.Top.ToString());
                //7设置左边距
                INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsLeft", printDocument.DefaultPageSettings.Margins.Left.ToString());
                //8设置右边距
                INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsRight", printDocument.DefaultPageSettings.Margins.Right.ToString());
                //9设置纸张高度
                INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperHeight", printDocument.DefaultPageSettings.PaperSize.Height.ToString());
                //10设置纸张宽度
                INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperWidth", printDocument.DefaultPageSettings.PaperSize.Width.ToString());
            }
        }