Procházet zdrojové kódy

其他-补充页面编辑-提取逻辑

ZhouJieSheng před 2 roky
rodič
revize
835db6d2f9

+ 15 - 0
PDF Office/Model/DialogNames.cs

@@ -20,5 +20,20 @@ namespace PDF_Office.Model
         /// 全屏模式弹窗
         /// </summary>
         public static string FullScreenDialog = "FullScreenDialog";
+
+        /// <summary>
+        /// 页面编辑- 提取弹窗
+        /// </summary>
+        public static string ExtractDialog = "ExtractDialog";
+
+        /// <summary>
+        /// 页面编辑-拆分弹窗
+        /// </summary>
+        public static string SplitDialog = "SplitDailog";
+
+        /// <summary>
+        /// 页面编辑-插入弹窗
+        /// </summary>
+        public static string InsertDialog = "InsertDialog";
     }
 }

+ 26 - 0
PDF Office/Model/PageEdit/ExtractModel.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PDF_Office.Model.PageEdit
+{
+    public class ExtractModel
+    {
+        /// <summary>
+        /// 页面范围
+        /// </summary>
+        public List<int> PageRange { get; set; } = new List<int>();
+
+        /// <summary>
+        /// 每页提取成一个文档,否则多页提取成一个文档
+        /// </summary>
+        public bool IsEveryPageToFile { get; set; } = false;
+
+        /// <summary>
+        /// 提取后删除页面
+        /// </summary>
+        public bool IsDeleteAfterExtract { get; set; } = false;
+    }
+}

+ 5 - 0
PDF Office/Model/ParameterNames.cs

@@ -30,5 +30,10 @@ namespace PDF_Office.Model
         public static string InsertType_Blank = "Insert_Blank";
 
         public static string InsertType_Custom = "Insert_Custom";
+
+        /// <summary>
+        /// 用于弹窗传参的数据模型
+        /// </summary>
+        public static string DataModel = "Model";
     }
 }

+ 1 - 0
PDF Office/ViewModels/BottomToolContentViewModel.cs

@@ -425,6 +425,7 @@ namespace PDF_Office.ViewModels
             if (e.Key == "PageNum")
             {
                 CurrentPage = (int)e.Value;
+                PageCount = PDFViewer.Document.PageCount;
             }
             if (e.Key == "ViewMode")
             {

+ 60 - 2
PDF Office/ViewModels/Dialog/PageEditDialogs/ExtractDialogViewModel.cs

@@ -1,12 +1,70 @@
-using System;
+using Prism.Mvvm;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using Prism.Services.Dialogs;
+using PDF_Office.Model.PageEdit;
+using Prism.Commands;
+using PDF_Office.Model;
 
 namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
 {
-    class ExtractDialogViewModel
+    public class ExtractDialogViewModel:BindableBase,IDialogAware
     {
+        public string Title => "";
+
+        public event Action<IDialogResult> RequestClose;
+
+        public DelegateCommand CancelCommand { get; set; }
+
+        public DelegateCommand ExtractCommnad { get; set; }
+
+        private ExtractModel model;
+
+        public ExtractModel Model
+        {
+            get { return model; }
+            set
+            {
+                SetProperty(ref model, value);
+            }
+        }
+
+
+        public ExtractDialogViewModel()
+        {
+            Model = new ExtractModel();
+            CancelCommand = new DelegateCommand(cancel);
+            ExtractCommnad = new DelegateCommand(extract);
+        }
+
+        private void cancel()
+        {
+            RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
+        }
+
+        private void extract()
+        {
+            DialogParameters valuePairs = new DialogParameters();
+            valuePairs.Add(ParameterNames.DataModel,Model);
+            RequestClose.Invoke(new DialogResult(ButtonResult.OK,valuePairs));
+        }
+
+        public bool CanCloseDialog()
+        {
+            return true;
+        }
+
+        public void OnDialogClosed()
+        {
+            return;
+        }
+
+        public void OnDialogOpened(IDialogParameters parameters)
+        {
+            
+        }
     }
 }

+ 45 - 2
PDF Office/ViewModels/Dialog/PageEditDialogs/InsertDialogViewModel.cs

@@ -1,4 +1,7 @@
-using System;
+using Prism.Commands;
+using Prism.Mvvm;
+using Prism.Services.Dialogs;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -6,7 +9,47 @@ using System.Threading.Tasks;
 
 namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
 {
-    class InsertDialogViewModel
+    public class InsertDialogViewModel : BindableBase, IDialogAware
     {
+        public string Title =>"";
+
+        public event Action<IDialogResult> RequestClose;
+
+        public DelegateCommand CancelCommand { get; set; }
+
+        public DelegateCommand InsertCommnad { get; set; }
+
+        public InsertDialogViewModel()
+        {
+            CancelCommand = new DelegateCommand(cancel);
+            InsertCommnad = new DelegateCommand(insert);
+        }
+
+        private void cancel()
+        {
+            RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
+        }
+
+        private void insert()
+        {
+            DialogParameters valuePairs = new DialogParameters();
+            //valuePairs.Add(ParameterNames.DataModel, Model);
+            RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
+        }
+
+        public bool CanCloseDialog()
+        {
+            return true;
+        }
+
+        public void OnDialogClosed()
+        {
+        
+        }
+
+        public void OnDialogOpened(IDialogParameters parameters)
+        {
+           
+        }
     }
 }

+ 44 - 2
PDF Office/ViewModels/Dialog/PageEditDialogs/SplitDialogViewModel.cs

@@ -1,4 +1,7 @@
-using System;
+using Prism.Commands;
+using Prism.Mvvm;
+using Prism.Services.Dialogs;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -6,7 +9,46 @@ using System.Threading.Tasks;
 
 namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
 {
-    class SplitDialogViewModel
+    public class SplitDialogViewModel : BindableBase, IDialogAware
     {
+        public string Title => "";
+
+        public event Action<IDialogResult> RequestClose;
+
+        public DelegateCommand CancelCommand { get; set; }
+
+        public DelegateCommand SplitCommnad { get; set; }
+
+        public SplitDialogViewModel()
+        {
+            CancelCommand = new DelegateCommand(cancel);
+            SplitCommnad = new DelegateCommand(split);
+        }
+        private void cancel()
+        {
+            RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
+        }
+
+        private void split()
+        {
+            DialogParameters valuePairs = new DialogParameters();
+            RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
+        }
+
+
+        public bool CanCloseDialog()
+        {
+            return true;
+        }
+
+        public void OnDialogClosed()
+        {
+           
+        }
+
+        public void OnDialogOpened(IDialogParameters parameters)
+        {
+            
+        }
     }
 }

+ 104 - 12
PDF Office/ViewModels/PageEdit/PageEditContentViewModel.cs

@@ -319,9 +319,6 @@ namespace PDF_Office.ViewModels.PageEdit
                 }
 
                 ReloadAfterOption(true, true, Tuple.Create(insertIndex-1, insertIndex + pageCount));
-
-
-
                 IsLoading = Visibility.Collapsed;
             }
         }
@@ -331,7 +328,7 @@ namespace PDF_Office.ViewModels.PageEdit
         /// </summary>
         private void SplitCommandEvent()
         {
-
+            dialogs.ShowDialog(DialogNames.SplitDialog, null, null);
         }
 
         /// <summary>
@@ -339,7 +336,13 @@ namespace PDF_Office.ViewModels.PageEdit
         /// </summary>
         private void ExtractCommandEvent()
         {
-
+            dialogs.ShowDialog(DialogNames.ExtractDialog,null,e=> { 
+                if(e.Result==ButtonResult.OK&&e.Parameters!=null)
+                {
+                    var model = e.Parameters.GetValue<ExtractModel>(ParameterNames.DataModel);
+                    DoExtractPages(model.IsEveryPageToFile,model.IsDeleteAfterExtract);
+                }
+            });
         }
 
         /// <summary>
@@ -417,6 +420,7 @@ namespace PDF_Office.ViewModels.PageEdit
             else
             {
                 //插入自定义页面
+                dialogs.ShowDialog(DialogNames.InsertDialog, null, null);
             }
             IsLoading = Visibility.Collapsed;
         }
@@ -424,10 +428,13 @@ namespace PDF_Office.ViewModels.PageEdit
         /// <summary>
         /// 删除选中页面
         /// </summary>
-        private void DeleteCommandEvent()
+        private async void DeleteCommandEvent()
         {
+            IsLoading = Visibility.Visible;
+            await Task.Delay(3);
             DoRemoveSelectedPages();
-            ReloadAfterOption(true, true, null);
+            ReloadAfterOption(true, true, Tuple.Create(0,PDFViewer.Document.PageCount));
+            IsLoading = Visibility.Collapsed;
         }
 
         /// <summary>
@@ -716,10 +723,8 @@ namespace PDF_Office.ViewModels.PageEdit
         /// 删除选中项的方法 不带Reload操作
         /// 方便其他逻辑中调用
         /// </summary>
-        private async void DoRemoveSelectedPages(List<int> pageList = null)
+        private void DoRemoveSelectedPages(List<int> pageList = null)
         {
-            IsLoading = Visibility.Visible;
-            await Task.Delay(3);
             List<int> indexList = new List<int>();
             if (pageList == null || pageList.Count < 0)
             {
@@ -738,7 +743,6 @@ namespace PDF_Office.ViewModels.PageEdit
             
             if(indexList.Count>0&& indexList.Count==PDFViewer.Document.PageCount)
             {
-                IsLoading = Visibility.Collapsed;
                 AlertsMessage alertsMessage = new AlertsMessage();
                 alertsMessage.ShowDialog("","不能删除所有页面","OK");
                 return;
@@ -748,7 +752,6 @@ namespace PDF_Office.ViewModels.PageEdit
             if (!result)
             {
                 ShowTip = Visibility.Visible;
-                IsLoading = Visibility.Collapsed;
                 return;
             }
 
@@ -757,6 +760,95 @@ namespace PDF_Office.ViewModels.PageEdit
             {
                 PageEditItems.RemoveAt(indexList[i]);
             }
+        }
+
+        /// <summary>
+        /// 提取选中页面的方法
+        /// </summary>
+        /// <param name="isEveryPageToFile">是否每页生成一个文件</param>
+        /// <param name="isDeleteAfterExtract">是否提取后删除页面</param>
+        private async void DoExtractPages(bool isEveryPageToFile,bool isDeleteAfterExtract)
+        {
+            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
+            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                if (string.IsNullOrEmpty(dialog.SelectedPath))
+                {
+                    AlertsMessage alertsMessage = new AlertsMessage();
+                    alertsMessage.ShowDialog("","","OK");//补充提示文案
+                    return;
+                }
+            }
+            else
+                return;
+
+            string selectedfile = "";
+            IsLoading = Visibility.Visible;
+            await Task.Delay(3);
+            List<int> pagenums = new List<int>();
+            for(int i=0;i<PageEditItems.Count;i++)
+            {
+                if(PageEditItems[i].Selected)
+                {
+                    pagenums.Add(PageEditItems[i].PageNumber);
+                }
+            }
+
+            if (isEveryPageToFile)
+            {
+                //每页生成的单独的文件
+                for (int i = 0; i < pagenums.Count; i++)
+                {
+                    string filename = PDFViewer.Document.FileName + " " + pagenums[i] + ".pdf";
+                    string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
+                    path = CommonHelper.CreateFilePath(path);
+                    selectedfile = path;
+                    CPDFDocument savedoc = CPDFDocument.CreateDocument();
+                    bool result = savedoc.ImportPages(PDFViewer.Document, pagenums[i].ToString());
+                    if (!result)
+                    {
+                        savedoc.Release();
+                        continue;
+                    }
+                    result = savedoc.WriteToFilePath(path);
+                    if (!result)
+                    {
+                        savedoc.Release();
+                        continue;
+                    }
+                    savedoc.Release();//释放内存
+                }
+            }
+            else//全部生成一个文件
+            {
+                string filename = PDFViewer.Document.FileName + " " + CommonHelper.GetPageParmFromList(pagenums) + ".pdf";
+                string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
+                path = CommonHelper.CreateFilePath(path);
+                selectedfile = path;
+                CPDFDocument savedoc = CPDFDocument.CreateDocument();
+                string range = String.Join(",", pagenums);
+                bool result = savedoc.ImportPages(PDFViewer.Document, range);
+                if (!result)
+                {
+                    ShowTip = Visibility.Visible;
+                    IsLoading = Visibility.Collapsed;
+                    return;
+                }
+                result = savedoc.WriteToFilePath(path);
+                if (!result)
+                {
+                    ShowTip = Visibility.Visible;
+                }
+                savedoc.Release();//释放内存
+            }
+
+            if(isDeleteAfterExtract)
+            {
+                DoRemoveSelectedPages();
+                ReloadAfterOption(true,true,Tuple.Create(0,PDFViewer.Document.PageCount));
+            }
+
+            CommonHelper.ShowFileBrowser(selectedfile);
             IsLoading = Visibility.Collapsed;
         }