123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- 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;
- 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);
- }
- /// <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 enum EnumBothSidesStage
- {
- StatusFrontSide,
- StatusBackSide
- }
- /// <summary>
- /// 双面打印阶段:正面或背面
- /// </summary>
- public EnumBothSidesStage BothSidesStage;
- #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;
- #endregion
- #region 委托
- public DelegateCommand<object> SelectPrinterCommand { get; set; }
- public DelegateCommand CancelCommand { get; set; }
- public DelegateCommand ConfirmPrintCommand { get; set; }
- public DelegateCommand SetPaperCommand { get; set; }
- public DelegateCommand<object> ChangePrintModCommand { get; set; }
- public DelegateCommand<object> SetGrayscaleCommand { get; set; }
- public DelegateCommand<object> SetBothSidesCommand { get; set; }
- public DelegateCommand<object> SetPageRangeSelectionIndexCommand { get; set; }
- public DelegateCommand<object> SetCustomPageRangeCommand { 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();
- SelectPrinterCommand = new DelegateCommand<object>(SelectPrinter);
- CancelCommand = new DelegateCommand(Cancel);
- ConfirmPrintCommand = new DelegateCommand(ConfirmPrint);
- SetPaperCommand = new DelegateCommand(SetPaper);
- ChangePrintModCommand = new DelegateCommand<object>(ChangePrintMod);
- SetGrayscaleCommand = new DelegateCommand<object>(SetGrayscale);
- SetBothSidesCommand = new DelegateCommand<object>(SetBothSides);
- SetPageRangeSelectionIndexCommand = new DelegateCommand<object>(SetPageRangeSelectionIndex);
- SetCustomPageRangeCommand = new DelegateCommand<object>(SetCustomPageRange);
- InitComponent();
- InitDictionary();
- InitParams();
- printEvent.GetEvent<SendPrintSettingsModInfoEvent>().Subscribe(RecvPrintSettingssModInfo);
- }
- private void InitComponent()
- {
- InitPrinterNameList();
- InitPrintContentList();
- InitPrintOrientationList();
- }
- private void InitDictionary()
- {
- InitGetPrintModNameFromRadioButton();
- }
- private void InitParams()
- {
- printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
- List<PageMediaSize> PageMediaSizeList = new List<PageMediaSize>();
- foreach (PageMediaSize size in printQueue.GetPrintCapabilities().PageMediaSizeCapability)
- {
- if (size == null || size.PageMediaSizeName == null)
- {
- continue;
- }
- if (size.PageMediaSizeName == PageMediaSizeName.ISOA4)
- {
- PrintSettingsInfo.PageMediaSize = size;
- }
- PageMediaSizeList.Add(size);
- }
- if (PrintSettingsInfo.PageMediaSize == null)
- {
- PrintSettingsInfo.PageMediaSize = PageMediaSizeList[0];
- }
- }
- private void InitPrinterNameList()
- {
- PrinterNameList = new List<string>();
- PrintDocument printDocument = new PrintDocument();
- string defaultPrinterName = printDocument.PrinterSettings.PrinterName;
- 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);
- }
- public void AutoOritation()
- {
- //纸张长边为高
- if (PrintSettingsInfo.PageMediaSize.Height / PrintSettingsInfo.PageMediaSize.Width >= 1)
- {
- //PDF长边为高
- if (PDFViewer.Document.GetPageSize(0).Height / PDFViewer.Document.GetPageSize(0).Width >= 1)
- {
- PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusPortrait;
- }
- //PDF长边为宽
- else
- {
- PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape;
- }
- }
- //纸张短边为高
- else
- {
- //PDF长边为高
- if (PDFViewer.Document.GetPageSize(0).Height / PDFViewer.Document.GetPageSize(0).Width >= 1)
- {
- PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape;
- }
- //PDF长边为宽
- else
- {
- PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusPortrait;
- }
- }
- PrintOrientationIndex = (int)PrintSettingsInfo.EnumPrintOrientation;
- }
- public void InitParamsByPDFView()
- {
- AutoOritation();
- }
- /// <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()
- {
- printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
- LoadPrinterSettings();
- DialogParameters value = new DialogParameters();
- 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"));
- AutoOritation();
- this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(PrintSettingsInfo);
- }
- });
- }
- public void ChangePrintMod(object e)
- {
- var rdo = e as RadioButton;
- CurrentHomePagePrinterModName = GetPrintModNameFromRadioButton[rdo.Name];
- printRegion.RequestNavigate(HomePagePrinterModRegionName, CurrentHomePagePrinterModName);
- }
- /// <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;
- }
- }
- /// <summary>
- /// 设置双面打印
- /// 改变打印流程
- /// 不改变打印相关参数
- /// </summary>
- /// <param name="e"></param>
- public void SetBothSides(object e)
- {
- var chk = e as CheckBox;
- if ((bool)chk.IsChecked)
- {
- IsBothSides = true;
- BothSidesStage = EnumBothSidesStage.StatusFrontSide;
- }
- else if (!(bool)chk.IsChecked)
- {
- IsBothSides = false;
- }
- }
- public void SetPageRangeSelectionIndex(object e)
- {
- var cmb = e as WritableComboBox;
- PrintSettingsInfo.EnumPageRange = (EnumPageRange)int.Parse(cmb.SelectedIndex);
- if (int.Parse(cmb.SelectedIndex) < cmb.Items.Count)
- {
- }
- else
- {
- }
- }
- public void SetCustomPageRange(object e)
- {
- if (PrintSettingsInfo.EnumPageRange == EnumPageRange.StatusCustomizedRange)
- {
- 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.CustomRange = DocumentTargetPages;
- }
- }
- public void RecvPrintSettingssModInfo(PrintModInfo printModInfo)
- {
- if(printModInfo.EnumPrintMod == currentHomePagePrinterMod)
- {
- PrintSettingsInfo.printModInfo = printModInfo;
- }
- }
- #endregion
- public void Cancel()
- {
- RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- public void NavigateDocumentRegion()
- {
- NavigationParameters param = new NavigationParameters();
- param.Add(ParameterNames.PDFViewer, PDFViewer);
- param.Add(ParameterNames.PrintSettingsInfo, PrintSettingsInfo);
- printRegion.RequestNavigate(HomePagePrinterDocumentRegionName, HomePagePrinterDocumentName, param);
- }
- public void NavigateModRegion()
- {
- NavigationParameters param = new NavigationParameters();
- param.Add(ParameterNames.PDFViewer, PDFViewer);
- printRegion.RequestNavigate(HomePagePrinterModRegionName, HomePagePrinterModSizeName);
- }
- /// <summary>
- /// 确认打印,单面或手动双面
- /// </summary>
- public void ConfirmPrint()
- {
- if (!IsBothSides)
- {
- RequestClose.Invoke(new DialogResult(ButtonResult.OK));
- }
- else if (IsBothSides)
- {
- MessageBoxEx.Show("翻页");
- RequestClose.Invoke(new DialogResult(ButtonResult.OK));
- }
- }
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- if (PDFViewer != null)
- {
- NavigationParameters param = new NavigationParameters();
- NavigateDocumentRegion();
- NavigateModRegion();
- InitParamsByPDFView();
- }
- }
- }
- }
|