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 CmbPageSelectionChanged { get; set; } public DelegateCommand 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(CmbPageSelectionChangedEvent); CmbPageTextChanged = new DelegateCommand(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() + 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() + 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 RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { CPDFViewer viewer = null; string filepath = ""; parameters.TryGetValue(ParameterNames.PDFViewer, out viewer); parameters.TryGetValue(ParameterNames.FilePath, out filepath); if (viewer != null && viewer.Document != null) { currentViewer = viewer; savefilepath = filepath.Replace(currentViewer.Document.FileName + ".pdf", ""); document = currentViewer.Document; } } #endregion } }