123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- using ComPDFKit_Conversion.Converter;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.CustomControl;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Model.Dialog.ConverterDialogs;
- 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.Windows.Forms;
- using DialogResult = Prism.Services.Dialogs.DialogResult;
- namespace PDF_Office.ViewModels.Dialog.ConverterDialogs
- {
- public class ConverterImgDialogViewModel : BindableBase, IDialogAware
- {
- public string Title => "";
- public event Action<IDialogResult> RequestClose;
- #region 参数和属性
- public ConverterImgDialogModel ConverterImgModel = new ConverterImgDialogModel();
- public CPDFViewer currentViewer;
- public string CurrentPageIndex = "1";
- public IDialogService dialogs;
- public string PageRangeText { set; get; } = "0";
- public string PageRangeSelectIndex { set; get; } = "0";
- public List<string> ImageTypeFamily { set; get; } = new List<string>();
- private int imageTypeSelectedIndex;
- /// <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 void SetImageTypeOrigin()
- {
- this.ImageTypeFamily.Add("JPEG");
- this.ImageTypeFamily.Add("JPG");
- this.ImageTypeFamily.Add("PNG");
- this.ImageTypeFamily.Add("GIF");
- this.ImageTypeFamily.Add("TIFF");
- this.ImageTypeFamily.Add("TGA");
- this.ImageTypeFamily.Add("BMP");
- this.ImageTypeFamily.Add("JPEG-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");
- }
- #endregion
- #region 委托声明
- public DelegateCommand CancelCommand { get; set; }
- public DelegateCommand ConverterCommnad { get; set; }
- public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
- public DelegateCommand<object> CmbPageTextChanged { get; set; }
- #endregion
- public ConverterImgDialogViewModel(IDialogService dialogService)
- {
- CancelCommand = new DelegateCommand(cancel);
- ConverterCommnad = new DelegateCommand(converter);
- CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
- CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
- dialogs = dialogService;
- }
- #region 逻辑函数
- /// <summary>
- /// 根据下拉框选项,选择不同的图片类型
- /// </summary>
- /// <param name="index"></param>
- private void UpdateImageType(int index)
- {
- this.ConverterImgModel.Type = ImgType.PNG;
- switch (index)
- {
- //case结果是同样的情况下,case可堆叠在一起,表示处理方式一样
- 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.Type = ImgType.JPEG;
- break;
- default:
- break;
- }
- }
- private void UpdateImageDPI(int index)
- {
- switch (index)
- {
- case 0:
- Trace.WriteLine("50");
- break;
- case 1:
- Trace.WriteLine("72");
- break;
- case 2:
- Trace.WriteLine("96");
- break;
- case 3:
- Trace.WriteLine("150");
- break;
- case 4:
- Trace.WriteLine("300");
- break;
- case 5:
- Trace.WriteLine("600");
- break;
- default:
- break;
- }
- }
- private void cancel()
- {
- RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- private void converter()
- {
- FolderBrowserDialog dlg = new FolderBrowserDialog();
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- ConverterImgModel.OutputPath = dlg.SelectedPath.Trim();
- }
- HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterImgModel.PageRange, PageRangeText, true, CurrentPageIndex);
- char[] enumerationSeparator = new char[] { ',' };
- char[] rangeSeparator = new char[] { '-' };
- if (!CommonHelper.GetPagesInRange(ref ConverterImgModel.PageIndexLists, ConverterImgModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
- { //TODO
- Trace.WriteLine("输入不对");
- MessageBoxEx.Show("输入不对");
- return;
- }
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.ConverterType, "Img");
- value.Add(ParameterNames.ConverterTypeModel, ConverterImgModel);
- 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 == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3")
- {
- HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
- 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("输入不对");
- MessageBoxEx.Show("输入不对");
- return;
- }
- }
- if (PageRangeSelectIndex == "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("输入不对");
- MessageBoxEx.Show("输入不对");
- return;
- }
- this.CurrentPageIndex = (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, true);
- 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);
- return;
- }
- else
- {
- ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
- ConverterPreview.CurrentIndex = 0;
- }
- }
- }
- }
- #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);
- if (pdfViewer != null)
- {
- currentViewer = pdfViewer;
- if (currentViewer.Tag != null) { ConverterImgModel.Pawssword = currentViewer.Tag.ToString(); }
- ConverterImgModel.InputPath = pdfViewer.Document.FilePath;
- FileInfo fileinfo = new FileInfo(ConverterImgModel.InputPath);
- ConverterImgModel.OutputPath = fileinfo.DirectoryName;
- SetImageTypeOrigin();
- SetImageDPIOrigin();
- }
- }
- #endregion
- }
- }
|