using ComPDFKit.PDFDocument; using ComPDFKitViewer.PdfViewer; using PDF_Master.Model; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using PDF_Master.Helper; using System.Diagnostics; using PDF_Master.CustomControl; using PDF_Master.Model.HomePageToolsDialogs; using System.Linq; using System.Windows.Forms; using DialogResult = Prism.Services.Dialogs.DialogResult; namespace PDF_Master.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); } } private int maxPageRange = 0; public int MaxPageRange { get { return maxPageRange; } set { SetProperty(ref maxPageRange, 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(string saveSelectedPath) { if (FileNameDeimiterIsCheck == "True") { splitModel.FileNameDeimiter = FileNameDeimiterText; } else { splitModel.FileNameDeimiter = ""; } if (FileNameLabelIsCheck == "True") { splitModel.FileNameLabel = FileNameLabelText; } else { splitModel.FileNameLabel = ""; } if (FrontFileIsCheck == "True") { splitModel.FrontFileName = true; splitModel.FileName = (saveSelectedPath + currentViewer.Document.FileName + splitModel.FileNameDeimiter + splitModel.FileNameLabel); } else { splitModel.FrontFileName = false; splitModel.FileName = (saveSelectedPath + splitModel.FileNameLabel + splitModel.FileNameDeimiter + currentViewer.Document.FileName); } } private void cancel() { RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)); } private void split() { FolderBrowserDialog dlg = new FolderBrowserDialog(); string saveSelectedPath = ""; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { saveSelectedPath = dlg.SelectedPath.Trim(); saveSelectedPath = saveSelectedPath + "\\"; } else { return; } FileNameIsCheck(saveSelectedPath); if (splitModel.Mode == HomePageSplitDialogModel.SplitMode.CustomPageRange) { HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref splitModel.PageRange, PageRangeText); if (splitModel.PageRange == "") { Trace.WriteLine("输入不对"); MessageBoxEx.Show("输入不对"); return; } 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); } } Process.Start(saveSelectedPath); 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() { string filenamelabeltext = ""; string filenamedeimitertext = ""; if (FileNameLabelIsCheck == "False") { filenamelabeltext = ""; } else { filenamelabeltext = FileNameLabelText; } if (FileNameDeimiterIsCheck == "False") { filenamedeimitertext = ""; } else { filenamedeimitertext = FileNameDeimiterText; } if (FrontFileIsCheck == "True") { FilePathName = savefilename + filenamedeimitertext + filenamelabeltext; splitModel.FrontFileName = true; } else { splitModel.FrontFileName = false; FilePathName = filenamelabeltext + filenamedeimitertext + savefilename; } } private void fileNameLabel() { string filenamelabeltext = ""; string filenamedeimitertext = ""; if (FileNameLabelIsCheck == "False") { filenamelabeltext = ""; } else { filenamelabeltext = FileNameLabelText; } if (FileNameDeimiterIsCheck == "False") { filenamedeimitertext = ""; } else { filenamedeimitertext = 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() { string filenamelabeltext = ""; string filenamedeimitertext = ""; if (FileNameLabelIsCheck == "False") { filenamelabeltext = ""; } else { filenamelabeltext = FileNameLabelText; } if (FileNameDeimiterIsCheck == "False") { filenamedeimitertext = ""; } else { filenamedeimitertext = FileNameDeimiterText; } 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; MaxPageRange = viewer.Document.PageCount; document = viewer.Document; savefilename = currentViewer.Document.FileName; FilePathName = savefilename + "-" + "part"; savefilepath = filepath.Replace(savefilename + ".pdf", ""); } } #endregion } }