123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Drawing.Printing;
- using ComPDFKit.PDFDocument;
- using PDF_Office.Model;
- using System.Drawing;
- using System.Reflection;
- using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
- using System.Threading.Tasks;
- using ComPDFKit.PDFPage;
- using PDF_Office.Helper;
- using System.Drawing.Drawing2D;
- using ComPDFKitViewer.PdfViewer;
- using System.Windows.Media.Imaging;
- using System.Drawing.Imaging;
- using PDF_Office.CustomControl;
- using Prism.Events;
- using ImTools;
- using PDF_Office.Views.Dialog.HomePageToolsDialogs.HomePagePrinter;
- using System.Printing;
- using System.Windows.Media.Animation;
- using System.Windows.Controls;
- using static Dropbox.Api.TeamLog.SharedLinkAccessLevel;
- using DryIoc;
- using System.Text.RegularExpressions;
- using System.Diagnostics;
- using PDF_Office.EventAggregators;
- using System.Collections.ObjectModel;
- using System.Windows.Forms;
- using ComboBox = System.Windows.Controls.ComboBox;
- using CheckBox = System.Windows.Controls.CheckBox;
- namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
- {
- public class HomePagePrinterDialogViewModel : BindableBase, IDialogAware
- {
- public IRegionManager printRegion;
- public IDialogService printDialogs;
- public IEventAggregator printEvent;
- private CPDFViewer PDFViewer;
- public PrintSettingsInfo PrintSettingsInfo = new PrintSettingsInfo();
- #region 常用字符串
- public string Title => "Primter";
- public string HomePagePrinterDocumentName = "HomePagePrinterDocumentContent";
- public string HomePagePrinterModSizeName = "HomePagePrinterModSizeContent";
- public string HomePagePrinterModPosterName = "HomePagePrinterModPosterContent";
- public string HomePagePrinterModMultipleName = "HomePagePrinterModMultipleContent";
- public string HomePagePrinterModBookletName = "HomePagePrinterModBookletContent";
- #endregion
- #region 字典
- public Dictionary<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);
- }
- #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 = 0;
- 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);
- }
- #endregion
- #region 打印态
- private bool _isBothSides;
- public bool IsBothSides
- {
- get => _isBothSides;
- set => _isBothSides = value;
- }
- public int printCurrentPageIndex;
- 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;
- }
- }
- }
- 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)
- {
- this.printRegion = regionManager;
- this.printDialogs = dialogService;
- this.printEvent = eventAggregator;
- HomePagePrinterDocumentRegionName = Guid.NewGuid().ToString();
- HomePagePrinterModRegionName = Guid.NewGuid().ToString();
- CancelCommand = new DelegateCommand(Cancel);
- ConfirmPrintCommand = new DelegateCommand(ConfirmPrint);
- SetPaperCommand = new DelegateCommand(SetPaper);
- SelectPrinterCommand = new DelegateCommand<object>(SelectPrinter);
- ChangePrintModCommand = new DelegateCommand<object>(ChangePrintMod);
- 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 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);
- 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()
- {
- if (true)
- {
- PageSetupDialog pageSetupDialog = new PageSetupDialog();
- PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName;
- pageSetupDialog.Document = PrintSettingsInfo.PrintDocument;
- if (pageSetupDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
- if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
- {
- PrintOrientationIndex = 0;
- }
- else
- {
- PrintOrientationIndex = 1;
- }
- this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); ;
- }
- }
- else
- {
- printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
- LoadPrinterSettings();
- DialogParameters value = new DialogParameters();
- value.Add("Unicode", Unicode);
- value.Add(ParameterNames.PrintQueue, printQueue);
- printDialogs.ShowDialog(DialogNames.HomePagePrinterPaperSettingsDialog, value, e =>
- {
- if (e.Result == ButtonResult.OK)
- {
- PrintSettingsInfo.PageMediaSize = e.Parameters.GetValue<PageMediaSize>("PageMediaSize");
- PrintSettingsInfo.Margin.Clear();
- PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginLeft"));
- PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginTop"));
- PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginRight"));
- PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginBottom"));
- this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
- }
- });
- }
- }
- public void ChangePrintMod(object e)
- {
- var rdo = e as System.Windows.Controls.RadioButton;
- CurrentHomePagePrinterModName = GetPrintModNameFromRadioButton[rdo.Name];
- if (CurrentHomePagePrinterModName == "HomePagePrinterModMultipleContent" || CurrentHomePagePrinterModName == "HomePagePrinterModBookletContent")
- {
- PrintPageBorderVisibility = Visibility.Visible;
- }
- else
- {
- PrintPageBorderVisibility = Visibility.Collapsed;
- }
- NavigationParameters param = new NavigationParameters();
- param.Add("document", PDFViewer.Document);
- param.Add("Unicode", Unicode);
- printRegion.RequestNavigate(HomePagePrinterModRegionName, CurrentHomePagePrinterModName, param);
- }
- /// <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);
- }
- }
- else
- {
- for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2)
- {
- PrintSettingsInfo.PageRangeList.Add(temp);
- }
- }
- }
- }
- else
- {
- PrintSettingsInfo.EnumPageRange = int.Parse(cmb.SelectedIndex);
- if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusAllRange)
- {
- for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
- {
- PrintSettingsInfo.PageRangeList.Add(temp);
- }
- }
- else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCurrentPage)
- {
- PrintSettingsInfo.PageRangeList.Add(printCurrentPageIndex);
- }
- else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusOddRange)
- {
- for (int temp = 0; temp < PDFViewer.Document.PageCount; temp += 2)
- {
- PrintSettingsInfo.PageRangeList.Add(temp);
- }
- }
- else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusEvenRange)
- {
- //TODO: 偶数页页数不够异常
- if (PDFViewer.Document.PageCount <= 1)
- {
- for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
- {
- PrintSettingsInfo.PageRangeList.Add(temp);
- }
- }
- else
- {
- for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2)
- {
- PrintSettingsInfo.PageRangeList.Add(temp);
- }
- }
- }
- }
- this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
- }
- else
- {
- if (!isCurrentPage)
- {
- PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange;
- }
- else
- {
- PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange;
- }
- }
- }
- public void SetCustomPageRange(object e)
- {
- if ((PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange&&!isCurrentPage)||(PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange && isCurrentPage))
- {
- var cmb = e as WritableComboBox;
- if (cmb == null)
- {
- return;
- }
- if (!CommonHelper.GetPagesInRange(ref DocumentTargetPages, cmb.Text, PDFViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
- { //TODO
- Trace.WriteLine("输入不对");
- MessageBoxEx.Show("输入不对");
- return;
- }
- PrintSettingsInfo.PageRangeList = DocumentTargetPages;
- this.printEvent.GetEvent<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;
- }
- #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 });
- }
- ///双面打印的情况
- 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 });
- RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
- }
- ///手动翻面
- ///手动翻面需要注意只有一页时和单面打印一致
- ///那奇数页呢?
- else
- {
- if ((PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusSize && PrintSettingsInfo.PageRangeList.Count == 1) || (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet && PrintSettingsInfo.PageRangeList.Count == 1))
- {
- this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
- RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
- }
- else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
- {
- var multipleInfo = (MultipleInfo)PrintSettingsInfo.PrintModInfo;
- if (PrintSettingsInfo.PageRangeList.Count / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) == 0)
- {
- this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
- RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
- }
- else
- {
- ///z
- this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode });
- if (MessageBoxEx.Show("Printing on the front side of the paper is complete. Please reverse the paper, click 'OK' to print the reverse side", "", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
- {
- this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
- RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
- }
- else
- {
- RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
- }
- }
- }
- else
- {
- this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode });
- if (MessageBoxEx.Show("Printing on the front side of the paper is complete. Please reverse the paper, click 'OK' to print the reverse side", "", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
- {
- this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
- RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
- }
- else
- {
- RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
- }
- }
- }
- }
- }
- public event Action<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);
- if (PDFViewer != null && PDFViewer.Document != null)
- {
- 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;
- }
- NavigationParameters param = new NavigationParameters();
- NavigateDocumentRegion();
- NavigateModRegion();
- }
- }
- }
- }
|