123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using ComPDFKit.PDFDocument;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Master.Model;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using SQLitePCL;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using PDF_Master.Model.HomePageToolsDialogs;
- using PDF_Master.Helper;
- using System.Diagnostics;
- using PDF_Master.CustomControl;
- using PDF_Master.Model.Dialog.HomePageToolsDialogs;
- using ImTools;
- using System.Windows.Forms;
- using DialogResult = Prism.Services.Dialogs.DialogResult;
- namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs
- {
- public class HomePageExtractDialogViewModel : BindableBase, IDialogAware
- {
- #region 参数和属性
- public CPDFDocument document;
- public CPDFViewer currentViewer;
- private HomePageExtractDialogModel extractModel = new HomePageExtractDialogModel();
- private string savefilepath { get; set; }
- public string ExtractToSingleFileIsCheck {get;set;} = "False";
- public string DeleteAfterExtractIsCheck { get; set; } = "Fales";
- public string PageRangeText { get; set; } = "";
- public string PageRangeSelectIndex { get; set; } = "0";
- private int maxPageRange = 0;
- public int MaxPageRange
- {
- get { return maxPageRange; }
- set
- {
- SetProperty(ref maxPageRange, value);
- }
- }
- #endregion
- #region 委托声明
- public DelegateCommand CancelCommand { get; set; }
- public DelegateCommand ExtractCommand { get; set; }
- public DelegateCommand ExtractToSingleFileCommand { get; set; }
- public DelegateCommand DeleteAfterExtractCommand { get; set; }
- public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
- public DelegateCommand<object> CmbPageTextChanged { get; set; }
- #endregion
- public HomePageExtractDialogViewModel()
- {
- CancelCommand = new DelegateCommand(cancel);
- ExtractCommand = new DelegateCommand(extract);
- ExtractToSingleFileCommand = new DelegateCommand(extractToSingleFile);
- DeleteAfterExtractCommand = new DelegateCommand(deleteAfterExtract);
- CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
- CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
- }
- #region 逻辑函数
- private void cancel()
- {
- RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- private void extract()
- {
- FolderBrowserDialog dlg = new FolderBrowserDialog();
- string saveSelectedPath = "";
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- saveSelectedPath = dlg.SelectedPath.Trim();
- saveSelectedPath = saveSelectedPath + "\\";
- }
- else { return; }
- HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref extractModel.PageRange, PageRangeText);
- if (extractModel.PageRange == "")
- {
- Trace.WriteLine("输入不对");
- MessageBoxEx.Show("输入不对");
- return;
- }
- char[] enumerationSeparator = new char[] { ',' };
- char[] rangeSeparator = new char[] { '-' };
- if (!CommonHelper.GetPagesInRange(ref extractModel.PageParm, extractModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
- { //TODO
- Trace.WriteLine("输入不对");
- MessageBoxEx.Show("输入不对");
- return;
- }
- if (extractModel.ExtractToSingleFile)
- {
- for (int i = 0; i < extractModel.PageParm.ToArray().Length; i++)
- {
- CPDFDocument extractdoc = CPDFDocument.CreateDocument();
- extractdoc.ImportPages(currentViewer.Document, (extractModel.PageParm.ToArray()[i] + 1).ToString());
- Trace.WriteLine(extractModel.PageParm.ToArray()[i].ToString());
- string createfilepath= CommonHelper.CreateFilePath(saveSelectedPath + currentViewer.Document.FileName + ".extract" + i.ToString() + ".pdf");
- extractdoc.WriteToFilePath(createfilepath);
- extractdoc.Release();
- }
- if (extractModel.DeleteAfterExtract)
- {
- currentViewer.Document.RemovePages(extractModel.PageParm.ToArray());
- currentViewer.Document.WriteToLoadedPath();
- }
- }
- else
- {
- CPDFDocument extractdoc = CPDFDocument.CreateDocument();
- extractdoc.ImportPages(currentViewer.Document, extractModel.PageRange);
- string createfilepath = CommonHelper.CreateFilePath(saveSelectedPath + currentViewer.Document.FileName + ".extract.pdf");
- extractdoc.WriteToFilePath(createfilepath);
- if (extractModel.DeleteAfterExtract)
- {
- currentViewer.Document.RemovePages(extractModel.PageParm.ToArray());
- currentViewer.Document.WriteToLoadedPath();
- }
- extractdoc.Release();
- }
- Process.Start(saveSelectedPath);
- RequestClose.Invoke(new DialogResult(ButtonResult.OK));
- }
- private void extractToSingleFile()
- {
- if (ExtractToSingleFileIsCheck == "True")
- {
- extractModel.ExtractToSingleFile = true;
- Trace.WriteLine("ExtractToSingleFileIsCheck" + extractModel.ExtractToSingleFile);
- }
- else
- {
- extractModel.ExtractToSingleFile = false;
- Trace.WriteLine("ExtractToSingleFileIsCheck" + extractModel.ExtractToSingleFile);
- }
- }
- private void deleteAfterExtract()
- {
- if (DeleteAfterExtractIsCheck == "True")
- {
- extractModel.DeleteAfterExtract = true;
- Trace.WriteLine("DeleteAfterExtractIsCheck" + extractModel.DeleteAfterExtract);
- }
- else
- {
- extractModel.DeleteAfterExtract = false;
- Trace.WriteLine("DeleteAfterExtractIsCheck" + extractModel.DeleteAfterExtract);
- }
- }
- private void CmbPageSelectionChangedEvent(object e)
- {
- ///这里采用的是将预览UI控件传递过来的方式,为下下策
- ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
- ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
- var ExtractPreview = e as PageTurningPreview;
- if (ExtractPreview != null)
- {
- string PageRangeSelectIndex = this.PageRangeSelectIndex;
- var currentViewer = this.currentViewer;
- string PageRange = "";
- var PageRangeText = this.PageRangeText;
- if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "1" || PageRangeSelectIndex == "2")
- {
- HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText);
- char[] enumerationSeparator = new char[] { ',' };
- char[] rangeSeparator = new char[] { '-' };
- if (!CommonHelper.GetPagesInRange(ref ExtractPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
- { //TODO
- ExtractPreview.PageIndexLists.Add(0);
- Trace.WriteLine("输入不对");
- MessageBoxEx.Show("输入不对");
- return;
- }
- }
- ExtractPreview.PageIndex.Text = (ExtractPreview.PageIndexLists.Last<int>() + 1).ToString();
- ExtractPreview.CurrentIndex = 0;
- ExtractPreview.CurrentPage.Text = (ExtractPreview.PageIndexLists[ExtractPreview.CurrentIndex] + 1).ToString();
- ExtractPreview.AwaitRenderBitmap(ExtractPreview.document);
- }
- }
- private void CmbPageTextChangedEvent(object e)
- {
- var ExtractPreview = e as PageTurningPreview;
- if (ExtractPreview != null)
- {
- string PageRangeSelectIndex = this.PageRangeSelectIndex;
- var currentViewer = this.currentViewer;
- string PageRange = "";
- var PageRangeText = this.PageRangeText;
- HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText);
- char[] enumerationSeparator = new char[] { ',' };
- char[] rangeSeparator = new char[] { '-' };
- if (!CommonHelper.GetPagesInRange(ref ExtractPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
- { //TODO
- ExtractPreview.PageIndexLists.Add(0);
- return;
- }
- else
- {
- ExtractPreview.PageIndex.Text = (ExtractPreview.PageIndexLists.Last<int>() + 1).ToString();
- ExtractPreview.CurrentIndex = 0;
- ExtractPreview.CurrentPage.Text = (ExtractPreview.PageIndexLists[ExtractPreview.CurrentIndex] + 1).ToString();
- ExtractPreview.AwaitRenderBitmap(ExtractPreview.document);
- }
- }
- }
- #endregion
- #region 框架行为
- public string Title => "";
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- CPDFViewer viewer = null;
- string filepath = "";
- parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out viewer);
- parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
- if (viewer != null && viewer.Document != null)
- {
- currentViewer = viewer;
- MaxPageRange = viewer.Document.PageCount;
- savefilepath = filepath.Replace(currentViewer.Document.FileName + ".pdf", "");
- document = currentViewer.Document;
- }
- }
- #endregion
- }
- }
|