123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- using ComPDFKit_Conversion.Converter;
- using ComPDFKit_Conversion.Options;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Master.CustomControl;
- using PDF_Master.Helper;
- using PDF_Master.Model;
- using PDF_Master.Model.Dialog.ConverterDialogs;
- using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
- using PDF_Master.Properties;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Windows;
- using System.Windows.Forms;
- using DialogResult = Prism.Services.Dialogs.DialogResult;
- namespace PDF_Master.ViewModels.Dialog.ConverterDialogs
- {
- public class ConverterImgDialogViewModel : BindableBase, IDialogAware
- {
- #region 文案
- private string T_title;
- public string T_Title
- {
- get { return T_title; }
- set
- {
- SetProperty(ref T_title, value);
- }
- }
- private string T_yes;
- public string T_YES
- {
- get { return T_yes; }
- set
- {
- SetProperty(ref T_yes, value);
- }
- }
- private string T_no;
- public string T_No
- {
- get { return T_no; }
- set
- {
- SetProperty(ref T_no, value);
- }
- }
- private string T_pageRange;
- public string T_PageRange
- {
- get { return T_pageRange; }
- set
- {
- SetProperty(ref T_pageRange, value);
- }
- }
- private string T_format;
- public string T_Format
- {
- get { return T_format; }
- set
- {
- SetProperty(ref T_format, value);
- }
- }
- private string _TextUpgrade;
- public string TextUpgrade
- {
- get { return _TextUpgrade; }
- set { _TextUpgrade = value; }
- }
- private void IntString()
- {
- TextUpgrade = App.ServiceLoader.GetString("TextUpgrade");
- T_Title = App.MainPageLoader.GetString("Convert_PDFToImageTitle");
- T_YES = App.MainPageLoader.GetString("Convert_Yes");
- T_No = App.MainPageLoader.GetString("Convert_No");
- T_PageRange = App.MainPageLoader.GetString("Convert_PDFPageRange");
- T_Format = App.MainPageLoader.GetString("Convert_PDFToImageFormat");
- }
- #endregion
- public string Title => "";
- public event Action<IDialogResult> RequestClose;
- #region 参数和属性
- public ConverterImgDialogModel ConverterImgModel = new ConverterImgDialogModel();
- public CPDFViewer currentViewer;
- public string CurrentPageIndex = "1";
- public bool IsCurrentPageIndex = true;
- public IDialogService dialogs;
- public string PageRangeText { set; get; } = "0";
- public string PageRangeSelectIndex { set; get; } = "0";
- private int maxPageRange = 0;
- public int MaxPageRange
- {
- get { return maxPageRange; }
- set
- {
- SetProperty(ref maxPageRange, value);
- }
- }
- public List<string> ImageTypeFamily { set; get; } = new List<string>();
- private int imageTypeSelectedIndex = 0;
- /// <summary>
- /// 图像类型的下拉框索引值
- /// 如果只需要根据选中项的索引判断,则可以不用再采取command绑定命令的形式,而是可以通过一个int类型属性绑定的形式,然后在set方法里进行处理
- /// </summary>
- public int ImageTypeSelectedIndex
- {
- get { return imageTypeSelectedIndex; }
- set
- {
- SetProperty(ref imageTypeSelectedIndex, value);
- UpdateImageType(ImageTypeSelectedIndex);
- }
- }
- private int imageDPISelectedIndex;
- /// <summary>
- /// 图像类型的下拉框索引值
- /// 如果只需要根据选中项的索引判断,则可以不用再采取command绑定命令的形式,而是可以通过一个int类型属性绑定的形式,然后在set方法里进行处理
- /// </summary>
- public int ImageDPISelectedIndex
- {
- get { return imageDPISelectedIndex; }
- set
- {
- SetProperty(ref imageDPISelectedIndex, value);
- UpdateImageDPI(ImageDPISelectedIndex);
- }
- }
- private Visibility limitationsConvertVisible = Visibility.Hidden;
- public Visibility LimitationsConvertVisible
- {
- get { return limitationsConvertVisible; }
- set
- {
- SetProperty(ref limitationsConvertVisible, value);
- }
- }
- private void SetImageTypeOrigin()
- {
- this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageJPEG"));
- this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageJPG"));
- this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImagePNG"));
- this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageGIF"));
- this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageTIFF"));
- this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageTGA"));
- this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageBMP"));
- this.ImageTypeFamily.Add(App.MainPageLoader.GetString("Convert_PDFToImageJPEG-2000"));
- }
- public List<string> ImageDPIFamily { set; get; } = new List<string>();
- private void SetImageDPIOrigin()
- {
- this.ImageDPIFamily.Add("50 dpi");
- this.ImageDPIFamily.Add("72 dpi");
- this.ImageDPIFamily.Add("96 dpi");
- this.ImageDPIFamily.Add("150 dpi");
- this.ImageDPIFamily.Add("300 dpi");
- this.ImageDPIFamily.Add("600 dpi");
- }
- private Dictionary<string, string> CheckPageSelect = new Dictionary<string, string>();
- private void InitCheckPageSelect()
- {
- CheckPageSelect.Clear();
- if (!IsCurrentPageIndex)
- {
- CheckPageSelect.Add("0", "0");
- CheckPageSelect.Add("1", "1");
- CheckPageSelect.Add("2", "2");
- CheckPageSelect.Add("3", "3");
- CheckPageSelect.Add("4", "4");
- }
- else
- {
- CheckPageSelect.Add("0", "0");
- CheckPageSelect.Add("1", "-2");
- CheckPageSelect.Add("2", "1");
- CheckPageSelect.Add("3", "2");
- CheckPageSelect.Add("4", "3");
- }
- }
- #endregion
- #region 委托声明
- public DelegateCommand CancelCommand { get; set; }
- public DelegateCommand ConverterCommnad { get; set; }
- public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
- public DelegateCommand<object> CmbPageTextChanged { get; set; }
- public DelegateCommand SetCustomPageRangeCommand { get; set; }
- public DelegateCommand<object> PreviewCancelDownCommand { get; set; }
- public DelegateCommand BatchConverterCommand { get; set; }
- public DelegateCommand UnlockMouseDownCommand { get; set; }
- #endregion
- public ConverterImgDialogViewModel(IDialogService dialogService)
- {
- IntString();
- CancelCommand = new DelegateCommand(cancel);
- ConverterCommnad = new DelegateCommand(converter);
- CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
- CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
- SetCustomPageRangeCommand = new DelegateCommand(SetCustomPageRange);
- PreviewCancelDownCommand = new DelegateCommand<object>(PreviewCancelDown);
- BatchConverterCommand = new DelegateCommand(BatchConverter);
- UnlockMouseDownCommand = new DelegateCommand(UnlockMouseDown);
- SetImageTypeOrigin();
- SetImageDPIOrigin();
- dialogs = dialogService;
- }
- #region 逻辑函数
- public void SetCustomPageRange()
- {
- if (PageRangeSelectIndex == CheckPageSelect["4"])
- {
- List<int> PageIndexLists = new List<int>();
- if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
- { //TODO
- //AlertsMessage alertsMessage = new AlertsMessage();
- //alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
- //if (alertsMessage.result == ContentResult.Ok)
- //{
- // return;
- //}
- //else
- //{
- // //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
- // //{
- // // Unicode = Unicode,
- // // Status = EnumDelete.StatusCreate
- // //});
- // return;
- //}
- }
- }
- }
- /// <summary>
- /// 根据下拉框选项,选择不同的图片类型
- /// </summary>
- /// <param name="index"></param>
- private void UpdateImageType(int index)
- {
- this.ConverterImgModel.Options.ImageType = ImgType.PNG;
- switch (index)
- {
- case 0:
- this.ConverterImgModel.imageType = ".jpeg";
- break;
- case 7:
- this.ConverterImgModel.imageType = ".jp2";
- break;
- case 3:
- this.ConverterImgModel.imageType = ".gif";
- break;
- case 4:
- this.ConverterImgModel.imageType = ".tiff";
- break;
- case 5:
- this.ConverterImgModel.imageType = ".tga";
- break;
- case 6:
- this.ConverterImgModel.imageType = ".bmp";
- break;
- case 2:
- this.ConverterImgModel.imageType = ".png";
- break;
- case 1:
- this.ConverterImgModel.imageType = ".jpg";
- this.ConverterImgModel.Options.ImageType = ImgType.JPEG;
- break;
- default:
- break;
- }
- }
- private void UpdateImageDPI(int index)
- {
- switch (index)
- {
- case 0:
- this.ConverterImgModel.Options.ImageDpi = 50;
- Trace.WriteLine("50");
- break;
- case 1:
- this.ConverterImgModel.Options.ImageDpi = 72;
- Trace.WriteLine("72");
- break;
- case 2:
- this.ConverterImgModel.Options.ImageDpi = 96;
- Trace.WriteLine("96");
- break;
- case 3:
- this.ConverterImgModel.Options.ImageDpi = 150;
- Trace.WriteLine("150");
- break;
- case 4:
- this.ConverterImgModel.Options.ImageDpi = 300;
- Trace.WriteLine("300");
- break;
- case 5:
- this.ConverterImgModel.Options.ImageDpi = 600;
- Trace.WriteLine("600");
- break;
- default:
- break;
- }
- // this.ConverterImgModel.Options.ImageDpi = 150;
- }
- private void cancel()
- {
- RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- private void PreviewCancelDown(object e)
- {
- var PageRangeComboBox = e as WritableComboBox;
- if (PageRangeComboBox != null)
- {
- PageRangeComboBox.IsloseFocus = false;
- }
- }
- private void converter()
- {
- if (PageRangeSelectIndex == CheckPageSelect["4"])
- {
- List<int> PageIndexLists = new List<int>();
- if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
- { //TODO
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
- if (alertsMessage.result == ContentResult.Ok)
- {
- return;
- }
- else
- {
- //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
- //{
- // Unicode = Unicode,
- // Status = EnumDelete.StatusCreate
- //});
- return;
- }
- }
- }
- FolderBrowserDialog dlg = new FolderBrowserDialog();
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- ConverterImgModel.OutputPath = dlg.SelectedPath.Trim();
- }
- else
- {
- return;
- }
- HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterImgModel.PageRange, PageRangeText, !IsCurrentPageIndex, CurrentPageIndex);
- if (ConverterImgModel.PageRange == "")
- {
- Trace.WriteLine("输入不对");
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
- return;
- }
- char[] enumerationSeparator = new char[] { ',' };
- char[] rangeSeparator = new char[] { '-' };
- if (!CommonHelper.GetPagesInRange(ref ConverterImgModel.PageIndexLists, ConverterImgModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
- { //TODO
- Trace.WriteLine("输入不对");
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
- return;
- }
- ConverterImgModel.imgOptions = ConverterImgModel.ImgOptions();
- // DialogParameters value = new DialogParameters();
- //value.Add(ParameterNames.ConverterType, "Img");
- //value.Add(ParameterNames.ConverterTypeModel, ConverterImgModel);
- var dialogresult = new DialogResult(ButtonResult.OK);
- dialogresult.Parameters.Add(ParameterNames.ConverterType, "Img");
- dialogresult.Parameters.Add(ParameterNames.ConverterTypeModel, ConverterImgModel);
- RequestClose.Invoke(dialogresult);
- //RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
- //dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
- //{
- //});
- }
- private void CmbPageSelectionChangedEvent(object e)
- {
- ///这里采用的是将预览UI控件传递过来的方式,为下下策
- ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
- ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
- var ConverterPreview = e as PageTurningPreview;
- if (ConverterPreview != null)
- {
- string PageRangeSelectIndex = this.PageRangeSelectIndex;
- var currentViewer = this.currentViewer;
- string PageRange = "";
- var PageRangeText = this.PageRangeText;
- if (PageRangeSelectIndex == CheckPageSelect["0"] || PageRangeSelectIndex == CheckPageSelect["2"] || PageRangeSelectIndex == CheckPageSelect["3"] || PageRangeSelectIndex == CheckPageSelect["4"])
- {
- if (PageRangeSelectIndex == CheckPageSelect["4"]) { HomePageEditHelper.GetPagerange("0", currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex); } else { HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex); }
- char[] enumerationSeparator = new char[] { ',' };
- char[] rangeSeparator = new char[] { '-' };
- if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
- { //TODO
- ConverterPreview.PageIndexLists.Add(0);
- Trace.WriteLine("输入不对");
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
- return;
- }
- }
- if (PageRangeSelectIndex == CheckPageSelect["1"])
- {
- char[] enumerationSeparator = new char[] { ',' };
- char[] rangeSeparator = new char[] { '-' };
- if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
- , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
- { //TODO
- Trace.WriteLine("输入不对");
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
- return;
- }
- this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
- }
- // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
- ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
- ConverterPreview.CurrentIndex = 0;
- }
- }
- private void CmbPageTextChangedEvent(object e)
- {
- var ConverterPreview = e as PageTurningPreview;
- if (ConverterPreview != null)
- {
- if (ConverterPreview != null)
- {
- string PageRangeSelectIndex = this.PageRangeSelectIndex;
- var currentViewer = this.currentViewer;
- string PageRange = "";
- var PageRangeText = this.PageRangeText;
- HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex);
- char[] enumerationSeparator = new char[] { ',' };
- char[] rangeSeparator = new char[] { '-' };
- if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
- { //TODO
- HomePageEditHelper.GetPagerange("0", currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex);
- if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
- { //TODO
- ConverterPreview.PageIndexLists.Add(0);
- Trace.WriteLine("输入不对");
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
- return;
- }
- ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
- ConverterPreview.CurrentIndex = 0;
- return;
- }
- else
- {
- // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
- ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
- ConverterPreview.CurrentIndex = 0;
- }
- }
- }
- }
- private void BatchConverter()
- {
- DialogParameters convertpdftoword = new DialogParameters();
- convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
- convertpdftoword.Add("ConverterTypeIndex", 7);
- HomePageBatchProcessingDialogModel.FilePaths = new List<string> { currentViewer.Document.FilePath.ToString() };
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
- convertpdftoword.Add(ParameterNames.FilePath, new string[] { currentViewer.Document.FilePath.ToString() });
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
- }
- private void UnlockMouseDown()
- {
- ConverterHelper.convertUnlock();
- }
- #endregion
- #region 构架行为
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- CPDFViewer pdfViewer = null;
- parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
- parameters.TryGetValue<bool>("PageRangeComboBoxCurrentPage", out IsCurrentPageIndex);
- if (pdfViewer != null)
- {
- InitCheckPageSelect();
- currentViewer = pdfViewer;
- MaxPageRange = currentViewer.Document.PageCount;
- if (currentViewer.Tag != null) { ConverterImgModel.Pawssword = currentViewer.Tag.ToString(); }
- ConverterImgModel.InputPath = pdfViewer.Document.FilePath;
- ImageTypeSelectedIndex = 0;
- ImageDPISelectedIndex =3 ;
- FileInfo fileinfo = new FileInfo(ConverterImgModel.InputPath);
- ConverterImgModel.OutputPath = fileinfo.DirectoryName;
- if (Settings.Default.UserDate.subscribestatus != 1)
- {
- LimitationsConvertVisible = Visibility.Visible;
- }
- }
- }
- #endregion
- }
- }
|