123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- using ComPDFKit.PDFDocument;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.Model;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using SQLitePCL;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using PDF_Office.Model.HomePageToolsDialogs;
- using PDF_Office.Helper;
- using System.Diagnostics;
- using PDF_Office.CustomControl;
- using PDF_Office.Model.Dialog.HomePageToolsDialogs;
- using ImTools;
- namespace PDF_Office.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";
- #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()
- {
- HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref extractModel.PageRange, PageRangeText);
- 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(savefilepath + 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(savefilepath + currentViewer.Document.FileName + ".extract.pdf");
- extractdoc.WriteToFilePath(createfilepath);
- if (extractModel.DeleteAfterExtract)
- {
- currentViewer.Document.RemovePages(extractModel.PageParm.ToArray());
- currentViewer.Document.WriteToLoadedPath();
- }
- extractdoc.Release();
- }
- CommonHelper.ShowFileBrowser(currentViewer.Document.FilePath);
- 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;
- savefilepath = filepath.Replace(currentViewer.Document.FileName + ".pdf", "");
- document = currentViewer.Document;
- }
- }
- #endregion
- }
- }
|