using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Windows; using System.Drawing.Printing; using ComPDFKit.PDFDocument; using PDF_Office.Model; using System.Drawing; using System.Reflection; using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter; using System.Threading.Tasks; using ComPDFKit.PDFPage; using PDF_Office.Helper; using System.Drawing.Drawing2D; using ComPDFKitViewer.PdfViewer; using System.Windows.Media.Imaging; using System.Drawing.Imaging; using PDF_Office.CustomControl; using Prism.Events; using ImTools; using PDF_Office.Views.Dialog.HomePageToolsDialogs.HomePagePrinter; using System.Printing; using System.Windows.Media.Animation; using System.Windows.Controls; using static Dropbox.Api.TeamLog.SharedLinkAccessLevel; using DryIoc; using System.Text.RegularExpressions; using System.Diagnostics; using PDF_Office.EventAggregators; using System.Collections.ObjectModel; using System.Windows.Forms; using ComboBox = System.Windows.Controls.ComboBox; using CheckBox = System.Windows.Controls.CheckBox; namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter { public class HomePagePrinterDialogViewModel : BindableBase, IDialogAware { public IRegionManager printRegion; public IDialogService printDialogs; public IEventAggregator printEvent; private CPDFViewer PDFViewer; public PrintSettingsInfo PrintSettingsInfo = new PrintSettingsInfo(); #region 常用字符串 public string Title => "Primter"; public string HomePagePrinterDocumentName = "HomePagePrinterDocumentContent"; public string HomePagePrinterModSizeName = "HomePagePrinterModSizeContent"; public string HomePagePrinterModPosterName = "HomePagePrinterModPosterContent"; public string HomePagePrinterModMultipleName = "HomePagePrinterModMultipleContent"; public string HomePagePrinterModBookletName = "HomePagePrinterModBookletContent"; #endregion #region 字典 public Dictionary GetPrintModNameFromRadioButton = new Dictionary(); #endregion #region 打印系统配置 private readonly PrintServer localDefaultPrintServer = new PrintServer(); private PrintQueue printQueue; #endregion #region 打印参数设置 /// /// (文档本身的)打印页集合 /// private List DocumentTargetPages = new List(); #endregion private int _printOrientationIndex = 0; public int PrintOrientationIndex { get => _printOrientationIndex; set => SetProperty(ref _printOrientationIndex, value); } #region 相关region private string _homePagePrinterDocumentRegionName; public string HomePagePrinterDocumentRegionName { get { return _homePagePrinterDocumentRegionName; } set { _homePagePrinterDocumentRegionName = value; } } private string _homePagePrinterModRegionName; public string HomePagePrinterModRegionName { get { return _homePagePrinterModRegionName; } set { _homePagePrinterModRegionName = value; } } #endregion #region 打印参数设置 /// /// 打印机名称 /// public string PrinterName; private int _maxCopiesNumber; /// /// 最大打印份数 /// public int MaxCopiesNumber { get => _maxCopiesNumber; set => SetProperty(ref _maxCopiesNumber, value); } private Visibility _printPageBorderVisibility = Visibility.Collapsed; public Visibility PrintPageBorderVisibility { get => _printPageBorderVisibility; set => SetProperty(ref _printPageBorderVisibility, value); } /// /// 打印机支持的颜色 /// public List OutputColorList = new List(); #endregion #region 组件 private List _printerNameList; /// /// 打印机名 /// public List PrinterNameList { get { return _printerNameList; } set { _printerNameList = value; } } private List _printContentList; /// /// 打印内容 /// public List PrintContentList { get { return _printContentList; } set { _printContentList = value; } } private int _printContentIndex = 0; public int PrintContentIndex { get => _printContentIndex; set => SetProperty(ref _printContentIndex, value); } private List _printerOrientationList; /// /// 打印列表 /// public List PrintOrientationList { get { return _printerOrientationList; } set { _printerOrientationList = value; } } private int _printerSelectedIndex; public int PrinterSelectedIndex { get => _printerSelectedIndex; set => SetProperty(ref _printerSelectedIndex, value); } #endregion #region 打印态 private bool _isBothSides; public bool IsBothSides { get => _isBothSides; set => _isBothSides = value; } public int printCurrentPageIndex; public bool isCurrentPage; /// /// 双面打印阶段:正面或背面 /// public EnumBothSidesStage BothSidesStage = EnumBothSidesStage.StatusNone; #endregion #region 窗口相关参数 private EnumPrintMod currentHomePagePrinterMod; private string _currentHomePagePrinterModName; public string CurrentHomePagePrinterModName { get => _currentHomePagePrinterModName; set { _currentHomePagePrinterModName = value; if (value == "HomePagePrinterModSizeContent") { currentHomePagePrinterMod = EnumPrintMod.StatusSize; } else if (value == "HomePagePrinterModPosterContent") { currentHomePagePrinterMod = EnumPrintMod.StatusPoster; } else if (value == "HomePagePrinterModMultipleContent") { currentHomePagePrinterMod = EnumPrintMod.StatusMultiple; } else if (value == "HomePagePrinterModBookletContent") { currentHomePagePrinterMod = EnumPrintMod.StatusBooklet; } } } public bool IsFirstOpen = true; string Unicode = null; #endregion #region 委托 public DelegateCommand CancelCommand { get; set; } public DelegateCommand ConfirmPrintCommand { get; set; } public DelegateCommand SetPaperCommand { get; set; } public DelegateCommand SelectPrinterCommand { get; set; } public DelegateCommand ChangePrintModCommand { get; set; } public DelegateCommand SetGrayscaleCommand { get; set; } public DelegateCommand SetDuplexCommand { get; set; } public DelegateCommand SetPageRangeSelectionIndexCommand { get; set; } public DelegateCommand SetCustomPageRangeCommand { get; set; } public DelegateCommand SetPrintContentCommand { get; set; } public DelegateCommand SetReversePageCommand { get; set; } public DelegateCommand SetPrintBorderCommand { get; set; } public DelegateCommand SetPrintOritationCommand { get; set; } #endregion HomePagePrinterDialogViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator) { this.printRegion = regionManager; this.printDialogs = dialogService; this.printEvent = eventAggregator; HomePagePrinterDocumentRegionName = Guid.NewGuid().ToString(); HomePagePrinterModRegionName = Guid.NewGuid().ToString(); CancelCommand = new DelegateCommand(Cancel); ConfirmPrintCommand = new DelegateCommand(ConfirmPrint); SetPaperCommand = new DelegateCommand(SetPaper); SelectPrinterCommand = new DelegateCommand(SelectPrinter); ChangePrintModCommand = new DelegateCommand(ChangePrintMod); SetGrayscaleCommand = new DelegateCommand(SetGrayscale); SetDuplexCommand = new DelegateCommand(SetDuplex); SetPageRangeSelectionIndexCommand = new DelegateCommand(SetPageRangeSelectionIndex); SetCustomPageRangeCommand = new DelegateCommand(SetCustomPageRange); SetPrintContentCommand = new DelegateCommand(SetPrintContent); SetReversePageCommand = new DelegateCommand(SetReversePage); SetPrintBorderCommand = new DelegateCommand(SetPrintBorder); SetPrintOritationCommand = new DelegateCommand(SetPrintOritation); Unicode = Guid.NewGuid().ToString(); InitComponent(); InitDictionary(); InitParams(); PageSetupDialog pageSetupDialog = new PageSetupDialog(); PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName; PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = false; pageSetupDialog.Document = PrintSettingsInfo.PrintDocument; PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings; if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape) { PrintOrientationIndex = 0; } else { PrintOrientationIndex = 1; } printEvent.GetEvent().Subscribe(RecvPrintSettingsModInfo, e => e.Unicode == Unicode); printEvent.GetEvent().Subscribe(RecvDuplexPrintMod, e => e.Unicode == Unicode); PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings; if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape) { PrintOrientationIndex = 0; } else { PrintOrientationIndex = 1; } this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } private void InitComponent() { InitPrinterNameList(); InitPrintContentList(); InitPrintOrientationList(); } private void InitDictionary() { InitGetPrintModNameFromRadioButton(); } private void InitParams() { printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName); List PageMediaSizeList = new List(); } private void InitPrinterNameList() { PrinterNameList = new List(); foreach (string printerListItem in PrinterSettings.InstalledPrinters) { PrinterNameList.Add(printerListItem); } if (PrinterNameList.Contains("Microsoft Print to PDF")) { PrinterSelectedIndex = PrinterNameList.IndexOf("Microsoft Print to PDF"); } else { PrinterSelectedIndex = 0; } PrinterName = PrinterNameList[PrinterSelectedIndex]; } private void InitPrintContentList() { PrintContentList = new List(); PrintContentList.Add("文档"); PrintContentList.Add("文档和标记"); PrintContentList.Add("文档和图章"); PrintContentList.Add("文档和表单"); } private void InitPrintOrientationList() { PrintOrientationList = new List(); PrintOrientationList.Add("纵向"); PrintOrientationList.Add("横向"); } public void InitGetPrintModNameFromRadioButton() { GetPrintModNameFromRadioButton.Clear(); GetPrintModNameFromRadioButton.Add("ModSizeRdo", HomePagePrinterModSizeName); GetPrintModNameFromRadioButton.Add("ModPosterRdo", HomePagePrinterModPosterName); GetPrintModNameFromRadioButton.Add("ModMultipleRdo", HomePagePrinterModMultipleName); GetPrintModNameFromRadioButton.Add("ModBookletRdo", HomePagePrinterModBookletName); } /// /// 选择打印机 /// /// public void SelectPrinter(object e) { var comboBox = e as ComboBox; PrinterName = comboBox.SelectedItem.ToString(); } /// /// 打开纸张设置窗口 /// public void SetPaper() { if (true) { PageSetupDialog pageSetupDialog = new PageSetupDialog(); PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName; pageSetupDialog.Document = PrintSettingsInfo.PrintDocument; if (pageSetupDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings; if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape) { PrintOrientationIndex = 0; } else { PrintOrientationIndex = 1; } this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); ; } } else { printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName); LoadPrinterSettings(); DialogParameters value = new DialogParameters(); value.Add("Unicode", Unicode); value.Add(ParameterNames.PrintQueue, printQueue); printDialogs.ShowDialog(DialogNames.HomePagePrinterPaperSettingsDialog, value, e => { if (e.Result == ButtonResult.OK) { PrintSettingsInfo.PageMediaSize = e.Parameters.GetValue("PageMediaSize"); PrintSettingsInfo.Margin.Clear(); PrintSettingsInfo.Margin.Add(e.Parameters.GetValue("MarginLeft")); PrintSettingsInfo.Margin.Add(e.Parameters.GetValue("MarginTop")); PrintSettingsInfo.Margin.Add(e.Parameters.GetValue("MarginRight")); PrintSettingsInfo.Margin.Add(e.Parameters.GetValue("MarginBottom")); this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } }); } } public void ChangePrintMod(object e) { var rdo = e as System.Windows.Controls.RadioButton; CurrentHomePagePrinterModName = GetPrintModNameFromRadioButton[rdo.Name]; if (CurrentHomePagePrinterModName == "HomePagePrinterModMultipleContent" || CurrentHomePagePrinterModName == "HomePagePrinterModBookletContent") { PrintPageBorderVisibility = Visibility.Visible; } else { PrintPageBorderVisibility = Visibility.Collapsed; } NavigationParameters param = new NavigationParameters(); param.Add("document", PDFViewer.Document); param.Add("Unicode", Unicode); printRegion.RequestNavigate(HomePagePrinterModRegionName, CurrentHomePagePrinterModName, param); } /// /// 加载对应打印设置 /// public void LoadPrinterSettings() { if (printQueue.GetPrintCapabilities().MaxCopyCount.HasValue) { MaxCopiesNumber = printQueue.GetPrintCapabilities().MaxCopyCount.Value; } else { MaxCopiesNumber = 1; } OutputColorList.Clear(); foreach (OutputColor color in printQueue.GetPrintCapabilities().OutputColorCapability) { OutputColorList.Add(color); } } #region 来自UI的设置 /// /// 设置灰度 /// 改变打印机设置 /// 改变渲染 /// public void SetGrayscale(object e) { var chk = e as CheckBox; if ((bool)chk.IsChecked) { PrintSettingsInfo.IsGrayscale = true; } else if (!(bool)chk.IsChecked) { PrintSettingsInfo.IsGrayscale = false; } this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } /// /// 设置双面打印 /// 改变打印流程 /// 不改变打印相关参数 /// /// public void SetDuplex(object e) { var chk = e as CheckBox; if ((bool)chk.IsChecked) { PrintSettingsInfo.IsDuplex = true; } else if (!(bool)chk.IsChecked) { PrintSettingsInfo.IsDuplex = false; } } public void SetPageRangeSelectionIndex(object e) { var cmb = e as WritableComboBox; PrintSettingsInfo.PageRangeList.Clear(); if (int.Parse(cmb.SelectedIndex) < cmb.Items.Count - 1) { if (!isCurrentPage) { PrintSettingsInfo.EnumPageRange = int.Parse(cmb.SelectedIndex); if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusAllRange) { for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++) { PrintSettingsInfo.PageRangeList.Add(temp); } } else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusOddRange) { for (int temp = 0; temp < PDFViewer.Document.PageCount; temp += 2) { PrintSettingsInfo.PageRangeList.Add(temp); } } else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusEvenRange) { //TODO: 偶数页页数不够异常 if (PDFViewer.Document.PageCount <= 1) { for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++) { PrintSettingsInfo.PageRangeList.Add(temp); } } else { for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2) { PrintSettingsInfo.PageRangeList.Add(temp); } } } } else { PrintSettingsInfo.EnumPageRange = int.Parse(cmb.SelectedIndex); if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusAllRange) { for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++) { PrintSettingsInfo.PageRangeList.Add(temp); } } else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCurrentPage) { PrintSettingsInfo.PageRangeList.Add(printCurrentPageIndex); } else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusOddRange) { for (int temp = 0; temp < PDFViewer.Document.PageCount; temp += 2) { PrintSettingsInfo.PageRangeList.Add(temp); } } else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusEvenRange) { //TODO: 偶数页页数不够异常 if (PDFViewer.Document.PageCount <= 1) { for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++) { PrintSettingsInfo.PageRangeList.Add(temp); } } else { for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2) { PrintSettingsInfo.PageRangeList.Add(temp); } } } } this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } else { if (!isCurrentPage) { PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange; } else { PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange; } } } public void SetCustomPageRange(object e) { if ((PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange&&!isCurrentPage)||(PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange && isCurrentPage)) { var cmb = e as WritableComboBox; if (cmb == null) { return; } if (!CommonHelper.GetPagesInRange(ref DocumentTargetPages, cmb.Text, PDFViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' })) { //TODO Trace.WriteLine("输入不对"); MessageBoxEx.Show("输入不对"); return; } PrintSettingsInfo.PageRangeList = DocumentTargetPages; this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } } public void SetPrintOritation(object e) { var cmb = e as ComboBox; if ((EnumPrintOrientation)cmb.SelectedIndex == EnumPrintOrientation.StatusPortrait) { PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = false; } else { PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = true; } PrintSettingsInfo.EnumPrintOrientation = (EnumPrintOrientation)cmb.SelectedIndex; this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } public void SetPrintContent(object e) { var cmb = e as ComboBox; if (cmb.SelectedIndex == (int)EnumPrintContent.StatusOnlyDocument) { PrintSettingsInfo.IsPrintWithAnnot = false; PrintSettingsInfo.IsPrintWithForm = false; } else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndMarkUps) { PrintSettingsInfo.IsPrintWithAnnot = true; PrintSettingsInfo.IsPrintWithForm = true; } else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndStamps) { PrintSettingsInfo.IsPrintWithAnnot = true; PrintSettingsInfo.IsPrintWithForm = false; } else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndForm) { PrintSettingsInfo.IsPrintWithAnnot = false; PrintSettingsInfo.IsPrintWithForm = true; } this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } public void SetReversePage(object e) { PrintSettingsInfo.PageRangeList.Reverse(); this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } public void SetPrintBorder(object e) { var chk = e as CheckBox; if ((bool)chk.IsChecked) { PrintSettingsInfo.IsPrintPageBorde = true; } else { PrintSettingsInfo.IsPrintPageBorde = false; } this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } public void RecvPrintSettingsModInfo(PrintModInfoWithUnicode printModInfoWithUnicode) { if (printModInfoWithUnicode.printModInfo.EnumPrintMod == currentHomePagePrinterMod) { PrintSettingsInfo.PrintModInfo = printModInfoWithUnicode.printModInfo; this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } } public void RecvDuplexPrintMod(EnumDuplexPrintModWithUnicode enumDuplexPrintModWithUnicode) { PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod = enumDuplexPrintModWithUnicode.enumDuplexPrintMod; } #endregion public void Cancel() { RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel)); } public void NavigateDocumentRegion() { NavigationParameters param = new NavigationParameters(); param.Add(ParameterNames.PDFViewer, PDFViewer); param.Add(ParameterNames.PrintSettingsInfo, PrintSettingsInfo); param.Add("Unicode", Unicode); printRegion.RequestNavigate(HomePagePrinterDocumentRegionName, HomePagePrinterDocumentName, param); PageSetupDialog pageSetupDialog = new PageSetupDialog(); PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName; pageSetupDialog.Document = PrintSettingsInfo.PrintDocument; PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings; if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape) { PrintOrientationIndex = 0; } else { PrintOrientationIndex = 1; } this.printEvent.GetEvent().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); } public void NavigateModRegion() { NavigationParameters param = new NavigationParameters(); param.Add(ParameterNames.PDFViewer, PDFViewer); param.Add("Unicode", Unicode); printRegion.RequestNavigate(HomePagePrinterModRegionName, HomePagePrinterModSizeName, param); } public bool IsBookletSinglePage(PrintModInfo printModInfo) { if (printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet) { var bookletInfo = (BookletInfo)printModInfo; if (!(bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides)) { return true; } } return false; } public bool IsBookletBothPage(PrintModInfo printModInfo) { if (printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet) { var bookletInfo = (BookletInfo)printModInfo; if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides) { return true; } } return false; } /// /// 确认打印,单面,双面或手动双面 /// 大小模式支持单面,双面 /// 海报模式只支持单面 /// 多页模式支持单面,双面 /// 小册子模式双面打印:双面 /// 仅正面/背面:单面 /// /// 单面打印:正常传参 /// /// 双面打印: /// 长边翻页,短边翻页 /// /// 自动双面打印 /// 传值给打印机就行 /// /// 手动双面打印 /// 长边翻页 先打印奇数页,进入二阶段,正向打印偶数页 /// 短边翻页 先打印奇数页,进入二阶段,反向打印偶数页 /// /// 异常退出是否要挂起打印机线程? /// /// public void ConfirmPrint() { printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName); LoadPrinterSettings(); Collection list = new Collection(); if (printQueue.GetPrintCapabilities().DuplexingCapability.Count > 0) { list.Clear(); for (int temp = 0; temp < printQueue.GetPrintCapabilities().DuplexingCapability.Count; temp++) { list.Add(printQueue.GetPrintCapabilities().DuplexingCapability[temp]); } } ///单面打印的情况:直接执行 if (PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusNone && !IsBookletBothPage(PrintSettingsInfo.PrintModInfo)) { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode }); } else if (IsBookletSinglePage(PrintSettingsInfo.PrintModInfo)) { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode }); } ///双面打印的情况 else if ((int)PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod > 0 || IsBookletBothPage(PrintSettingsInfo.PrintModInfo)) { ///支持自动双面 if (list.Count > 0) { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode }); RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK)); } ///手动翻面 ///手动翻面需要注意只有一页时和单面打印一致 ///那奇数页呢? else { if ((PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusSize && PrintSettingsInfo.PageRangeList.Count == 1) || (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet && PrintSettingsInfo.PageRangeList.Count == 1)) { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode }); RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK)); } else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple) { var multipleInfo = (MultipleInfo)PrintSettingsInfo.PrintModInfo; if (PrintSettingsInfo.PageRangeList.Count / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) == 0) { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode }); RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK)); } else { ///z this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode }); if (MessageBoxEx.Show("Printing on the front side of the paper is complete. Please reverse the paper, click 'OK' to print the reverse side", "", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK) { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode }); RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK)); } else { RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel)); } } } else { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode }); if (MessageBoxEx.Show("Printing on the front side of the paper is complete. Please reverse the paper, click 'OK' to print the reverse side", "", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK) { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode }); RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK)); } else { RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel)); } } } } } public event Action RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { this.printQueue.Dispose(); } public void OnDialogOpened(IDialogParameters parameters) { parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); if (PDFViewer != null && PDFViewer.Document != null) { for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++) { PrintSettingsInfo.PageRangeList.Add(temp); } if (parameters.TryGetValue(ParameterNames.PrintCurrentPage, out printCurrentPageIndex)) { isCurrentPage = true; } else { isCurrentPage = false; } NavigationParameters param = new NavigationParameters(); NavigateDocumentRegion(); NavigateModRegion(); } } } }