123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 |
- 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<object> CmbPageSelectionChanged { get; set; }
- public DelegateCommand<object> 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<object>(CmbPageSelectionChangedEvent);
- CmbPageTextChanged = new DelegateCommand<object>(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<int>() + 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<int>() + 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<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;
- document = viewer.Document;
- savefilename = currentViewer.Document.FileName;
- FilePathName = savefilename + "-" + "part";
- savefilepath = filepath.Replace(savefilename + ".pdf", "");
- }
- }
- #endregion
- }
- }
|