using ComPDFKit.PDFDocument; using ComPDFKitViewer.PdfViewer; using PDF_Office.Model; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using PDF_Office.Helper; using System.Diagnostics; using PDF_Office.CustomControl; using PDF_Office.Model.HomePageToolsDialogs; using System.Linq; namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs { public class HomePageSplitDialogViewModel : BindableBase, IDialogAware { #region 参数和属性 public CPDFDocument document; public CPDFViewer currentViewer; private HomePageSplitDialogModel splitModel = new HomePageSplitDialogModel(); private string savefilepath { get; set; } private string savefilename { get; set; } public string PageRangeText { set; get; } = "0"; public string PageRangeSelectIndex { set; get; } = "0"; private string averagePagesIsCheck = "True"; public string AveragePagesIsCheck { get { return averagePagesIsCheck; } set { SetProperty(ref averagePagesIsCheck, value); } } private string averageFilesIsCheck = "False"; public string AverageFilesIsCheck { get { return averageFilesIsCheck; } set { SetProperty(ref averageFilesIsCheck, value); } } private string customPageRangeIsCheck = "False"; public string CustomPageRangeIsCheck { get { return customPageRangeIsCheck; } set { SetProperty(ref customPageRangeIsCheck, value); } } private string averagePagesIsEnabled = "True"; public string AveragePagesIsEnabled { get { return averagePagesIsEnabled; } set { SetProperty(ref averagePagesIsEnabled, value); } } private string averageFilesIsEnabled = "False"; public string AverageFilesIsEnabled { get { return averageFilesIsEnabled; } set { SetProperty(ref averageFilesIsEnabled, value); } } private string customPageRangeIsEnabled = "False"; public string CustomPageRangeIsEnabled { get { return customPageRangeIsEnabled; } set { SetProperty(ref customPageRangeIsEnabled, value); } } public string AveragePagesText { set; get; } = "1"; public string AverageFilesText { set; get; } = "1"; private string filePathName = ""; public string FilePathName { get { return filePathName; } set { value = value+".pdf"; SetProperty(ref filePathName, value); } } public string FileNameLabelText { set; get; } = "part"; public string FileNameDeimiterText { set; get; } = "-"; public string FrontFileIsCheck { set; get; } = "True"; public string FileNameLabelIsCheck { set; get; } = "True"; public string FileNameDeimiterIsCheck { set; get; } = "True"; private string fileNameLabelIsEnabled = "False"; public string FileNameLabelIsEnabled { get { return fileNameLabelIsEnabled; } set { SetProperty(ref fileNameLabelIsEnabled, value); } } private string fileNameDeimiterIsEnabled = "False"; public string FileNameDeimiterIsEnabled { get { return fileNameDeimiterIsEnabled; } set { SetProperty(ref fileNameDeimiterIsEnabled, value); } } #endregion #region 委托声明 public DelegateCommand CancelCommand { get; set; } public DelegateCommand SplitCommand { get; set; } public DelegateCommand AveragePagesCommand { get; set; } public DelegateCommand AverageFilesCommand { get; set; } public DelegateCommand CustomPageRangeCommand { get; set; } public DelegateCommand FrontFileNameCommand { get; set; } public DelegateCommand FileNameLabelCommand { get; set; } public DelegateCommand FileNameDeimiterCommand { get; set; } public DelegateCommand FileNameDeimiterChangeCommand { get; set; } public DelegateCommand FileNameLabelChangeCommand { get; set; } public DelegateCommand CmbPageSelectionChanged { get; set; } public DelegateCommand CmbPageTextChanged { get; set; } #endregion public HomePageSplitDialogViewModel() { CancelCommand = new DelegateCommand(cancel); SplitCommand = new DelegateCommand(split); AveragePagesCommand = new DelegateCommand(averagePages); AverageFilesCommand = new DelegateCommand(averageFiles); CustomPageRangeCommand = new DelegateCommand(customPageRange); FrontFileNameCommand = new DelegateCommand(frontFileName); FileNameLabelCommand = new DelegateCommand(fileNameLabel); FileNameDeimiterCommand = new DelegateCommand(fileNameDeimiter); FileNameDeimiterChangeCommand = new DelegateCommand(fileNameDeimiter); FileNameLabelChangeCommand = new DelegateCommand(fileNameLabel); CmbPageSelectionChanged = new DelegateCommand(CmbPageSelectionChangedEvent); CmbPageTextChanged = new DelegateCommand(CmbPageTextChangedEvent); } #region 逻辑函数 private void FileNameIsCheck() { if (FileNameDeimiterIsCheck == "True") { splitModel.FileNameDeimiter = FileNameDeimiterText; } else { splitModel.FileNameDeimiter = ""; } if (FileNameDeimiterIsCheck == "True") { splitModel.FileNameDeimiter = FileNameDeimiterText; } else { splitModel.FileNameDeimiter = ""; } if (FileNameLabelIsCheck == "True") { splitModel.FrontFileName = true; splitModel.FileName = (savefilepath + currentViewer.Document.FileName + splitModel.FileNameDeimiter + splitModel.FileNameLabel); } else { splitModel.FrontFileName = false; splitModel.FileName = (savefilepath + splitModel.FileNameLabel + splitModel.FileNameDeimiter + currentViewer.Document.FileName); } } private void cancel() { RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)); } private void split() { HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref splitModel.PageRange, PageRangeText); FileNameIsCheck(); char[] enumerationSeparator = new char[] { ',' }; char[] rangeSeparator = new char[] { '-' }; if (!CommonHelper.GetPagesInRange(ref splitModel.PageParm, splitModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator)) { //TODO Trace.WriteLine("输入不对"); MessageBoxEx.Show("输入不对"); return; } Trace.WriteLine("splitModel.PageRange: " + splitModel.PageRange + "-----"); if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.AveragePages) { if (AveragePagesText != "") { splitModel.GetModeCount = int.Parse(AveragePagesText); } int averagepagesremainder = 0; var averagepagesdocs = HomePageEditHelper.AverageSplitPages(currentViewer.Document.PageCount, splitModel.GetModeCount, ref averagepagesremainder); int averagepagesdoc; for (averagepagesdoc = 1; averagepagesdoc < averagepagesdocs; averagepagesdoc++) { CPDFDocument extractdoc = CPDFDocument.CreateDocument(); extractdoc.ImportPages(currentViewer.Document, (1 + splitModel.GetModeCount * (averagepagesdoc - 1)).ToString() + "-" + (splitModel.GetModeCount * averagepagesdoc).ToString()); string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + averagepagesdoc.ToString() + ".pdf"); extractdoc.WriteToFilePath(createfilepath); extractdoc.Release(); } if (averagepagesdoc == averagepagesdocs) { CPDFDocument extractdoc = CPDFDocument.CreateDocument(); extractdoc.ImportPages(currentViewer.Document, (1 + splitModel.GetModeCount * (averagepagesdoc - 1)).ToString() + "-" + currentViewer.Document.PageCount.ToString()); string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + averagepagesdoc.ToString() + ".pdf"); extractdoc.WriteToFilePath(createfilepath); } } if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.AverageFiles) { if (AverageFilesText != "") { splitModel.GetModeCount = int.Parse(AverageFilesText); } int averagefilesremainder = 0; var averagefilespages = HomePageEditHelper.AverageSplitDocs(currentViewer.Document.PageCount, splitModel.GetModeCount, ref averagefilesremainder); int averagefilesdoc; if (splitModel.GetModeCount > currentViewer.Document.PageCount) { splitModel.GetModeCount = currentViewer.Document.PageCount; } for (averagefilesdoc = 1; averagefilesdoc <= splitModel.GetModeCount; averagefilesdoc++) { CPDFDocument extractdoc = CPDFDocument.CreateDocument(); if (averagefilesremainder > 0) { extractdoc.ImportPages(currentViewer.Document, (1 + (averagefilespages + 1) * (averagefilesdoc - 1)).ToString() + "-" + ((averagefilespages + 1) * averagefilesdoc).ToString()); string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + averagefilesdoc.ToString() + ".pdf"); extractdoc.WriteToFilePath(createfilepath); averagefilesremainder--; } else { extractdoc.ImportPages(currentViewer.Document, (averagefilespages * averagefilesremainder + averagefilesremainder + 1 + averagefilespages * (averagefilesdoc - 1)).ToString() + "-" + (averagefilespages * averagefilesremainder + averagefilesremainder + (averagefilespages * averagefilesdoc)).ToString()); string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + averagefilesdoc.ToString() + ".pdf"); extractdoc.WriteToFilePath(createfilepath); } extractdoc.Release(); } } if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.CustomPageRange) { CPDFDocument extractdoc = CPDFDocument.CreateDocument(); extractdoc.ImportPages(currentViewer.Document, splitModel.PageRange); string createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + "1" + ".pdf"); extractdoc.WriteToFilePath(createfilepath); if (splitModel.PageRange != "1-" + currentViewer.Document.PageCount.ToString()) { currentViewer.Document.RemovePages(splitModel.PageParm.ToArray()); createfilepath = CommonHelper.CreateFilePath(splitModel.FileName + ".split" + "2" + ".pdf"); currentViewer.Document.WriteToFilePath(createfilepath); } } CommonHelper.ShowFileBrowser(currentViewer.Document.FilePath); RequestClose.Invoke(new DialogResult(ButtonResult.OK)); } private void averagePages() { CustomPageRangeIsCheck = "False"; AverageFilesIsCheck = "False"; AveragePagesIsCheck = "True"; CustomPageRangeIsEnabled = "False"; AverageFilesIsEnabled = "False"; AveragePagesIsEnabled = "True"; splitModel.Mode = HomePageSplitDialogModel.SplitMode.AveragePages; } private void averageFiles() { CustomPageRangeIsCheck = "False"; AverageFilesIsCheck = "True"; AveragePagesIsCheck = "False"; CustomPageRangeIsEnabled = "False"; AverageFilesIsEnabled = "True"; AveragePagesIsEnabled = "False"; splitModel.Mode = HomePageSplitDialogModel.SplitMode.AverageFiles; } private void customPageRange() { CustomPageRangeIsCheck = "True"; AverageFilesIsCheck = "False"; AveragePagesIsCheck = "False"; CustomPageRangeIsEnabled = "True"; AverageFilesIsEnabled = "False"; AveragePagesIsEnabled = "False"; splitModel.Mode = HomePageSplitDialogModel.SplitMode.CustomPageRange; } private void frontFileName() { if (FileNameLabelIsCheck == "False") { FileNameLabelText = ""; } if (FileNameDeimiterIsCheck == "False") { FileNameDeimiterText = ""; } if (FrontFileIsCheck == "True") { FilePathName = savefilename + FileNameDeimiterText + FileNameLabelText; splitModel.FrontFileName = true; } else { splitModel.FrontFileName = false; FilePathName = FileNameLabelText + FileNameDeimiterText + savefilename; } } private void fileNameLabel() { if (FileNameDeimiterIsCheck == "False") { FileNameDeimiterText = ""; } if (FileNameLabelIsCheck == "True") { FileNameLabelIsEnabled = "True"; if (FrontFileIsCheck == "True") { FilePathName = savefilename + FileNameDeimiterText + FileNameLabelText; splitModel.FrontFileName = true; } else { splitModel.FrontFileName = false; FilePathName = FileNameLabelText + FileNameDeimiterText + savefilename; } } else { FileNameLabelIsEnabled = "False"; if (FrontFileIsCheck == "True") { FilePathName = savefilename + FileNameDeimiterText; splitModel.FrontFileName = true; } else { splitModel.FrontFileName = false; FilePathName = FileNameDeimiterText + savefilename; } } } private void fileNameDeimiter() { if (FileNameLabelIsCheck == "False") { FileNameLabelText = ""; } if (FileNameDeimiterIsCheck == "True") { FileNameDeimiterIsEnabled = "True"; if (FrontFileIsCheck == "True") { FilePathName = savefilename + FileNameDeimiterText + FileNameLabelText; } else { FilePathName = FileNameLabelText + FileNameDeimiterText + savefilename; } } else { FileNameDeimiterIsEnabled = "False"; if (FrontFileIsCheck == "True") { FilePathName = savefilename + FileNameLabelText; } else { FilePathName = FileNameLabelText + savefilename; } } } private void CmbPageSelectionChangedEvent(object e) { ///这里采用的是将预览UI控件传递过来的方式,为下下策 ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式 ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里 var SplitPreview = e as PageTurningPreview; if (SplitPreview != 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 SplitPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator)) { //TODO SplitPreview.PageIndexLists.Add(0); Trace.WriteLine("输入不对"); MessageBoxEx.Show("输入不对"); return; } } SplitPreview.PageIndex.Text = (SplitPreview.PageIndexLists.Last() + 1).ToString(); SplitPreview.CurrentIndex = 0; SplitPreview.CurrentPage.Text = (SplitPreview.PageIndexLists[SplitPreview.CurrentIndex] + 1).ToString(); SplitPreview.AwaitRenderBitmap(SplitPreview.document); } } private void CmbPageTextChangedEvent(object e) { var SplitPreview = e as PageTurningPreview; if (SplitPreview != 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 SplitPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator)) { //TODO SplitPreview.PageIndexLists.Add(0); return; } else { SplitPreview.PageIndex.Text = (SplitPreview.PageIndexLists.Last() + 1).ToString(); SplitPreview.CurrentIndex = 0; SplitPreview.CurrentPage.Text = (SplitPreview.PageIndexLists[SplitPreview.CurrentIndex] + 1).ToString(); SplitPreview.AwaitRenderBitmap(SplitPreview.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; document = viewer.Document; savefilename = currentViewer.Document.FileName; FilePathName = savefilename + "-" + "part" ; savefilepath = filepath.Replace(savefilename + ".pdf", ""); } } #endregion } }