public bool SetupPrinterInfo(FwPrinterInfo prtInfo, PrinterSettingMode saveOrPrint = PrinterSettingMode.Save) { if (prtInfo == null) { prtInfo = InitializeSettings(); } this.PrinterInfo = prtInfo; bool result; PrinterSettingWindow ps = new PrinterSettingWindow(); ps.Title = string.Format("{0}用プリンター設定", this.ReportName); ps.OKボタン機能名 = saveOrPrint == PrinterSettingMode.Save ? "保存" : "印刷"; PageSettings pages = ApplySettings(prtInfo); if (this.IsCustomMode) { result = ps.SettingDialogCustom(pages); } else { result = ps.SettingDialogStandard(pages); } if (result) { this.PageSettings = ps.PageSettings; this.PrinterInfo.printerName = ps.PageSettings.PrinterSettings.PrinterName; this.PrinterInfo.paperSizeName = ps.PaperSizeName; this.PrinterInfo.paperSourceName = ps.PaperSourceName; this.PrinterInfo.paperWidth = ps.CustomWidth; this.PrinterInfo.paperHeight = ps.CustomHeight; this.PrinterInfo.margins = ps.PageSettings.Margins; this.PrinterInfo.landscape = ps.PageSettings.Landscape; this.PrinterInfo.isCustomPaper = ps.IsCustomPageSize; } return(result); }
/// <summary> /// 印刷実行(ユーザー定義用紙も反映) /// </summary> /// <param name="dataTable">プリンタ設定情報</param> /// <param name="fromPage">開始ページ(全て印刷の場合はNULL)</param> /// <param name="toPage">終了ページ(全て印刷の場合はNULL)</param> public void PrintOutToPrinter(FwPrinterInfo prtInfo, int fromPage = 0, int toPage = 0, int copies = 1) { //IsCustomMode = false; //IsDotPrinter = false; //// ■ 仮対処 //ShowPreview(); //return; bool needsetting = false; if (prtInfo == null) { needsetting = true; } else { this.PageSettings = ApplySettings(prtInfo, fromPage, toPage, copies); if (this.PageSettings == null) { needsetting = true; } else { if (this.PageSettings.PrinterSettings == null) { needsetting = true; } else { if (prtInfo.printerName != this.PageSettings.PrinterSettings.PrinterName) { needsetting = true; } else { if (this.PrinterList.Contains(prtInfo.printerName) != true) { needsetting = true; } } } } } if (needsetting) { bool result = SetupPrinterInfo(prtInfo, PrinterSettingMode.Print); if (result != true) { this.IsPrinted = false; this.DialogResult = DialogResult.Cancel; return; } } else { this.PrinterInfo = prtInfo; } try { PageSettings pages = ApplySettings(this.PrinterInfo, fromPage, toPage, copies); targetReportDocument.PrintOptions.CopyFrom(pages.PrinterSettings, pages); if (this.CRVIEWER.ViewerCore.ReportSource != null) { this.CRVIEWER.ViewerCore.ReuseParameterWhenRefresh = true; this.CRVIEWER.ViewerCore.RefreshReport(); } // 印刷処理を実行する。プリンタの設定、ページ設定を渡す。 targetReportDocument.PrintToPrinter(pages.PrinterSettings, pages, true); this.IsPrinted = true; } catch (Exception ex) { this.IsPrinted = false; throw ex; } }
private PageSettings ApplySettings(FwPrinterInfo prtInfo, int fromPage = 0, int toPage = 0, int copies = 1) { try { this.PrinterInfo = prtInfo; PageSettings pages = new PageSettings(); PrinterSettings prts = new PrinterSettings(); // 変更対象のプロパティを調整 // プリンタの設定をする。 //prts.PrinterName = PrinterInfo.printerName; // プリンタ名 if (fromPage == 0 && toPage == 0) { // 全て印刷する場合 prts.FromPage = 0; prts.ToPage = 0; prts.PrintRange = PrintRange.AllPages; } else { // 印刷範囲を指定する場合 if (fromPage > toPage) { throw new ReportException("印刷するページ範囲を正しく指定してください。"); } if (fromPage == 0 || toPage == 0) { throw new ReportException("印刷するページ範囲を正しく指定してください。"); } prts.PrintRange = PrintRange.SomePages; prts.FromPage = fromPage; prts.ToPage = toPage; } prts.PrinterName = PrinterInfo.printerName; prts.Copies = (short)copies; prts.Collate = false; pages.PrinterSettings = prts; foreach (System.Drawing.Printing.PaperSource item in prts.PaperSources) { if (string.IsNullOrEmpty(_paperSourceName)) { if (item.SourceName == PrinterInfo.paperSourceName) { pages.PaperSource = item; break; } } else { if (item.SourceName == _paperSourceName) { pages.PaperSource = item; break; } } } // 用紙サイズがカスタムかどうか if (PrinterInfo.isCustomPaper) { pages.PaperSize = new System.Drawing.Printing.PaperSize("Custom", (int)PrinterInfo.paperWidth, (int)PrinterInfo.paperHeight); pages.PaperSize.RawKind = 256; } else { foreach (System.Drawing.Printing.PaperSize item in prts.PaperSizes) { if (item.PaperName == PrinterInfo.paperSizeName) { pages.PaperSize = item; break; } } } if (PrinterInfo.margins != null) { pages.Margins = PrinterInfo.margins; } pages.Landscape = prtInfo.landscape; return(pages); } catch (Exception ex) { throw ex; } }