12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106 |
- 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<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;
- 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<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();
- }
- }
- }
- }
|