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_Master.Model; using System.Drawing; using System.Reflection; using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePagePrinter; using System.Threading.Tasks; using ComPDFKit.PDFPage; using PDF_Master.Helper; using System.Drawing.Drawing2D; using ComPDFKitViewer.PdfViewer; using System.Windows.Media.Imaging; using System.Drawing.Imaging; using PDF_Master.CustomControl; using Prism.Events; using ImTools; using PDF_Master.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_Master.EventAggregators; using System.Collections.ObjectModel; using System.Windows.Forms; using ComboBox = System.Windows.Controls.ComboBox; using CheckBox = System.Windows.Controls.CheckBox; using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel; namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter { public class HomePagePrinterDialogViewModel : BindableBase, IDialogAware { #region 文案 private string T_title; public string T_Title { get { return T_title; } set { SetProperty(ref T_title, value); } } private string T_printer; public string T_Printer { get { return T_printer; } set { SetProperty(ref T_printer, value); } } private string T_pageSettings; public string T_PageSettings { get { return T_pageSettings; } set { SetProperty(ref T_pageSettings, value); } } private string T_copies; public string T_Copies { get { return T_copies; } set { SetProperty(ref T_copies, value); } } private string T_blackAndWhite; public string T_BlackAndWhite { get { return T_blackAndWhite; } set { SetProperty(ref T_blackAndWhite, value); } } private string T_printSettings; public string T_PrintSettings { get { return T_printSettings; } set { SetProperty(ref T_printSettings, value); } } private string T_orientation; public string T_Orientation { get { return T_orientation; } set { SetProperty(ref T_orientation, value); } } private string T_printContent; public string T_PrintContent { get { return T_printContent; } set { SetProperty(ref T_printContent, value); } } private string T_reversePages; public string T_ReversePages { get { return T_reversePages; } set { SetProperty(ref T_reversePages, value); } } private void InitString() { T_Title = App.MainPageLoader.GetString("Print_Title"); T_Printer = App.MainPageLoader.GetString("Print_Printer"); T_PageSettings = App.MainPageLoader.GetString("Print_PageSettings"); T_Copies = App.MainPageLoader.GetString("Print_Copies"); T_BlackAndWhite = App.MainPageLoader.GetString("Print_BlackAndWhite"); T_PrintSettings = App.MainPageLoader.GetString("Print_PrintSettings"); T_Orientation = App.MainPageLoader.GetString("Print_Orientation"); T_PrintContent = App.MainPageLoader.GetString("Print_PrintContent"); T_ReversePages = App.MainPageLoader.GetString("Print_ReversePages"); } #endregion public IRegionManager printRegion; public IDialogService printDialogs; public IEventAggregator printEvent; private CPDFViewer PDFViewer; public PrintSettingsInfo PrintSettingsInfo = new PrintSettingsInfo(); List PageList = new List(); #region 常用字符串 public string Title => ""; 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); } private int _maxPageRange = 0; public int MaxPageRange { get => _maxPageRange; set => SetProperty(ref _maxPageRange, value); } private int _pageRangeSelectIndex = 0; public int PageRangeSelectIndex { get => _pageRangeSelectIndex; set => SetProperty(ref _pageRangeSelectIndex, 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 = 1; 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); } public PageSetupDialog pageSetupDialog = new PageSetupDialog(); #endregion #region 打印态 private bool _isBothSides; public bool IsBothSides { get => _isBothSides; set => _isBothSides = value; } public bool FinishedFrontSideFlag = false; public int printCurrentPageIndex; public string PageListString = null; 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; } } } private string _customRangeString; public string CustomRangeString { get => _customRangeString; set { SetProperty(ref _customRangeString, value); } } 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) { InitString(); 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); ChangePrintModCommand = new DelegateCommand(ChangePrintMod); SetPaperCommand = new DelegateCommand(SetPaper); SelectPrinterCommand = new DelegateCommand(SelectPrinter); 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 = 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); printEvent.GetEvent().Subscribe(RecvFinishedFrontSide, 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() { PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName; pageSetupDialog.Document = PrintSettingsInfo.PrintDocument; pageSetupDialog.Document.DefaultPageSettings.Margins = new Margins((int)(PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins.Left * 2.54), (int)(PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins.Right * 2.54), (int)(PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins.Top * 2.54), (int)(PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins.Bottom * 2.54)); if (pageSetupDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { PrintSettingsInfo.IsPaperSizeChanged = true; 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 }); PrintSettingsInfo.IsPaperSizeChanged = false; } } 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); } PageRangeSelectIndex = 0; } 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); } PageRangeSelectIndex = 0; } 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) { for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++) { PrintSettingsInfo.PageRangeList.Add(temp); } PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange; } else { for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++) { PrintSettingsInfo.PageRangeList.Add(temp); } 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; } public void RecvFinishedFrontSide(FinishedFrontSide finishedFrontSide) { FinishedFrontSideFlag = true; } #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 }); RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel)); } ///双面打印的情况 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 }); } ///手动翻面 ///手动翻面需要注意只有一页时和单面打印一致 ///那奇数页呢? 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 }); } 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 }); } else { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode }); if (FinishedFrontSideFlag && 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 }); } } } else { this.printEvent.GetEvent().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode }); if (FinishedFrontSideFlag && 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)); } 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); VerifyPasswordResult result = SecurityHelper.VerifyPasswordForSelectedPermissions(PDFViewer.Document, Model.Dialog.ToolsDialogs.SaftyDialogs.EnumPermissionsSet.StatusAllowsPrinting, printDialogs); if (result.IsDiscryptied) { if (result.Password != null) { string filePath = PDFViewer.Document.FilePath; if (PDFViewer.Document.IsLocked) { PDFViewer.Document.UnlockWithPassword(result.Password); } else { PDFViewer.Document.CheckOwnerPassword(result.Password); } } ///TODO: ///此处填入需要执行的代码 } if (!result.IsDiscryptied) { System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { this.RequestClose(new Prism.Services.Dialogs.DialogResult()); })); } if (PDFViewer != null && PDFViewer.Document != null && result.IsDiscryptied) { 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; } if (parameters.TryGetValue>(ParameterNames.PageList, out PageList)) { PageListString = CommonHelper.GetPageParmFromList(PageList); if (!string.IsNullOrEmpty(PageListString)) { if (PageList.Count == 1) { System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { PageRangeSelectIndex = 1; })); } else { PrintSettingsInfo.PageRangeList = PageList; System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { CustomRangeString = PageListString; PageRangeSelectIndex = 4; })); } } } System.Windows.Size pageSize = PDFViewer.Document.GetPageSize(0); if ((pageSize.Height / pageSize.Width > 1.0 && (double)PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height / PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width < 1.0) || (pageSize.Height / pageSize.Width < 1.0 && (double)PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height / PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width > 1.0)) { PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = true; PrintOrientationIndex = 0; PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape; } MaxPageRange = PDFViewer.Document.PageCount; NavigationParameters param = new NavigationParameters(); NavigateDocumentRegion(); NavigateModRegion(); } } } }