1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102 |
- 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;
- using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
- namespace PDF_Office.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<int> PageList = new List<int>();
- #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<string, string> GetPrintModNameFromRadioButton = new Dictionary<string, string>();
- #endregion
- #region 打印系统配置
- private readonly PrintServer localDefaultPrintServer = new PrintServer();
- private PrintQueue printQueue;
- #endregion
- #region 打印参数设置
- /// <summary>
- /// (文档本身的)打印页集合
- /// </summary>
- private List<int> DocumentTargetPages = new List<int>();
- #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 打印参数设置
- /// <summary>
- /// 打印机名称
- /// </summary>
- public string PrinterName;
- private int _maxCopiesNumber;
- /// <summary>
- /// 最大打印份数
- /// </summary>
- 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);
- }
- /// <summary>
- /// 打印机支持的颜色
- /// </summary>
- public List<OutputColor> OutputColorList = new List<OutputColor>();
- #endregion
- #region 组件
- private List<string> _printerNameList;
- /// <summary>
- /// 打印机名
- /// </summary>
- public List<string> PrinterNameList
- {
- get { return _printerNameList; }
- set { _printerNameList = value; }
- }
- private List<string> _printContentList;
- /// <summary>
- /// 打印内容
- /// </summary>
- public List<string> PrintContentList
- {
- get { return _printContentList; }
- set { _printContentList = value; }
- }
- private int _printContentIndex = 1;
- public int PrintContentIndex
- {
- get => _printContentIndex;
- set => SetProperty(ref _printContentIndex, value);
- }
- private List<string> _printerOrientationList;
- /// <summary>
- /// 打印列表
- /// </summary>
- public List<string> 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;
- /// <summary>
- /// 双面打印阶段:正面或背面
- /// </summary>
- 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<object> SelectPrinterCommand { get; set; }
- public DelegateCommand<object> ChangePrintModCommand { get; set; }
- public DelegateCommand<object> SetGrayscaleCommand { get; set; }
- public DelegateCommand<object> SetDuplexCommand { get; set; }
- public DelegateCommand<object> SetPageRangeSelectionIndexCommand { get; set; }
- public DelegateCommand<object> SetCustomPageRangeCommand { get; set; }
- public DelegateCommand<object> SetPrintContentCommand { get; set; }
- public DelegateCommand<object> SetReversePageCommand { get; set; }
- public DelegateCommand<object> SetPrintBorderCommand { get; set; }
- public DelegateCommand<object> 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<object>(ChangePrintMod);
- SetPaperCommand = new DelegateCommand(SetPaper);
- SelectPrinterCommand = new DelegateCommand<object>(SelectPrinter);
- SetGrayscaleCommand = new DelegateCommand<object>(SetGrayscale);
- SetDuplexCommand = new DelegateCommand<object>(SetDuplex);
- SetPageRangeSelectionIndexCommand = new DelegateCommand<object>(SetPageRangeSelectionIndex);
- SetCustomPageRangeCommand = new DelegateCommand<object>(SetCustomPageRange);
- SetPrintContentCommand = new DelegateCommand<object>(SetPrintContent);
- SetReversePageCommand = new DelegateCommand<object>(SetReversePage);
- SetPrintBorderCommand = new DelegateCommand<object>(SetPrintBorder);
- SetPrintOritationCommand = new DelegateCommand<object>(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<SendPrintSettingsModInfoEvent>().Subscribe(RecvPrintSettingsModInfo, e => e.Unicode == Unicode);
- printEvent.GetEvent<SendDuplexPrintModEvent>().Subscribe(RecvDuplexPrintMod, e => e.Unicode == Unicode);
- printEvent.GetEvent<SendFinishedFrontSideEvent>().Subscribe(RecvFinishedFrontSide, e => e.Unicode == Unicode);
- PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
- if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
- {
- PrintOrientationIndex = 0;
- }
- else
- {
- PrintOrientationIndex = 1;
- }
- this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().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<PageMediaSize> PageMediaSizeList = new List<PageMediaSize>();
- }
- private void InitPrinterNameList()
- {
- PrinterNameList = new List<string>();
- 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<string>();
- PrintContentList.Add("文档");
- PrintContentList.Add("文档和标记");
- PrintContentList.Add("文档和图章");
- PrintContentList.Add("文档和表单");
- }
- private void InitPrintOrientationList()
- {
- PrintOrientationList = new List<string>();
- 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);
- }
- /// <summary>
- /// 选择打印机
- /// </summary>
- /// <param name="e"></param>
- public void SelectPrinter(object e)
- {
- var comboBox = e as ComboBox;
- PrinterName = comboBox.SelectedItem.ToString();
- }
- /// <summary>
- /// 打开纸张设置窗口
- /// </summary>
- 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<SendPrintSettingsInfoEvent>().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);
- }
- /// <summary>
- /// 加载对应打印设置
- /// </summary>
- 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的设置
- /// <summary>
- /// 设置灰度
- /// 改变打印机设置
- /// 改变渲染
- /// </summary>
- 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<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
- }
- /// <summary>
- /// 设置双面打印
- /// 改变打印流程
- /// 不改变打印相关参数
- /// </summary>
- /// <param name="e"></param>
- 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<SendPrintSettingsInfoEvent>().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<SendPrintSettingsInfoEvent>().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<SendPrintSettingsInfoEvent>().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<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
- }
- public void SetReversePage(object e)
- {
- PrintSettingsInfo.PageRangeList.Reverse();
- this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().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<SendPrintSettingsInfoEvent>().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<SendPrintSettingsInfoEvent>().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<SendPrintSettingsInfoEvent>().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;
- }
- /// <summary>
- /// 确认打印,单面,双面或手动双面
- /// 大小模式支持单面,双面
- /// 海报模式只支持单面
- /// 多页模式支持单面,双面
- /// 小册子模式双面打印:双面
- /// 仅正面/背面:单面
- ///
- /// 单面打印:正常传参
- ///
- /// 双面打印:
- /// 长边翻页,短边翻页
- ///
- /// 自动双面打印
- /// 传值给打印机就行
- ///
- /// 手动双面打印
- /// 长边翻页 先打印奇数页,进入二阶段,正向打印偶数页
- /// 短边翻页 先打印奇数页,进入二阶段,反向打印偶数页
- ///
- /// 异常退出是否要挂起打印机线程?
- ///
- /// </summary>
- public void ConfirmPrint()
- {
- printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
- LoadPrinterSettings();
- Collection<Duplexing> list = new Collection<Duplexing>();
- 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<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
- }
- else if (IsBookletSinglePage(PrintSettingsInfo.PrintModInfo))
- {
- this.printEvent.GetEvent<SendPrintQueueEvent>().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<SendPrintQueueEvent>().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<SendPrintQueueEvent>().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<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
- }
- else
- {
- this.printEvent.GetEvent<SendPrintQueueEvent>().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<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
- }
- }
- }
- else
- {
- this.printEvent.GetEvent<SendPrintQueueEvent>().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<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
- }
- }
- }
- }
- RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
- }
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- this.printQueue.Dispose();
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- parameters.TryGetValue<CPDFViewer>(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;
- PDFViewer.CloseDocument();
- PDFViewer.InitDocument(filePath);
- PDFViewer.Document.UnlockWithPassword(result.Password);
- PDFViewer.Load();
- }
- ///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<int>(ParameterNames.PrintCurrentPage, out printCurrentPageIndex))
- {
- isCurrentPage = true;
- }
- else
- {
- isCurrentPage = false;
- }
- if (parameters.TryGetValue<List<int>>(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();
- }
- }
- }
- }
|