Explorar el Código

页面编辑-补充粘贴、复制、剪切功能

ZhouJieSheng hace 2 años
padre
commit
d54c6815cc

+ 3 - 0
PDF Office/App.xaml.cs

@@ -59,6 +59,9 @@ namespace PDF_Office
 
         public static MainWindowViewModel mainWindowViewModel;
 
+        /// <summary>
+        /// 当前软件已打开的文件路径列表
+        /// </summary>
         public static List<string> OpenedFileList = new List<string>();
 
         public static CacheFilePath CachePath;

+ 27 - 2
PDF Office/Helper/CacheFilePath.cs

@@ -1,4 +1,6 @@
-using PDF_Office.Properties;
+using ComPDFKit.PDFDocument;
+using PDF_Office.Properties;
+using Prism.Mvvm;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -13,7 +15,7 @@ namespace PDF_Office.Helper
     /// 功能模块需要创建缓存文件夹时,统一在此类里处理
     /// 临时文件在app启动时进行删除
     /// </summary>
-    public class CacheFilePath
+    public class CacheFilePath:BindableBase
     {
         private static readonly CacheFilePath instance = new CacheFilePath();
 
@@ -24,6 +26,21 @@ namespace PDF_Office.Helper
         List<string> SignatureFreeHand = new List<string> { "Signature" ,"FreeHand" };
         List<string> SignatureStamp = new List<string> { "Signature", "Stamp" };
         List<string> MergeFile = new List<string> { "Temp" };
+
+        private CPDFDocument copyDoc;
+        /// <summary>
+        /// 页面编辑复制粘贴的临时缓存文件
+        /// </summary>
+        public CPDFDocument CopyDoc
+        {
+            get { return copyDoc; }
+            set
+            {
+                SetProperty(ref copyDoc, value);
+            }
+        }
+
+
         public static CacheFilePath Instance => instance;
 
         private CacheFilePath()
@@ -31,6 +48,14 @@ namespace PDF_Office.Helper
 
         }
 
+        ~ CacheFilePath()
+        {
+            if(CopyDoc!=null)
+            {
+                CopyDoc.Release();
+            }
+        }
+
         /// <summary>
         /// 自定图章缓存文件夹
         /// </summary>

+ 177 - 0
PDF Office/ViewModels/PageEdit/PageEditContentViewModel.cs

@@ -294,6 +294,20 @@ namespace PDF_Office.ViewModels.PageEdit
             }
         }
 
+        private bool canPaste = false;
+        /// <summary>
+        /// 是否可以粘贴
+        /// </summary>
+        public bool CanPaste
+        {
+            get { return canPaste; }
+            set
+            {
+                SetProperty(ref canPaste, value);
+            }
+        }
+
+
         #endregion
 
         #region 命令
@@ -309,6 +323,21 @@ namespace PDF_Office.ViewModels.PageEdit
         /// </summary>
         public DelegateCommand<object> KeyDown { get; set; }
 
+        /// <summary>
+        /// 粘贴
+        /// </summary>
+        public DelegateCommand PasteCommand { get; set; }
+
+        /// <summary>
+        /// 复制
+        /// </summary>
+        public DelegateCommand CopyCommand { get; set; }
+
+        /// <summary>
+        /// 剪切
+        /// </summary>
+        public DelegateCommand CutCommand { get; set; }
+
         /// <summary>
         /// 替换文件
         /// </summary>
@@ -390,16 +419,33 @@ namespace PDF_Office.ViewModels.PageEdit
             ZoomInCommand = new DelegateCommand(ZoomInCommandEvent, CanZoomIn).ObservesProperty(() => ZoomIndex);
             ZoomOutCommand = new DelegateCommand(ZoomOutCommandEvent, CanZoomOut).ObservesProperty(() => ZoomIndex);
             ShowPageSizeCommand = new DelegateCommand(ShowPageSize);
+            CopyCommand = new DelegateCommand(copy);
+            PasteCommand = new DelegateCommand(paste,CanPasteExcute).ObservesProperty(() => CanPaste);
+            CutCommand = new DelegateCommand(cut);
 
             //订阅页面刷新事件
             eventAggregator.GetEvent<PageEditRefreshEvent>().Subscribe(OnPageEditRefreshEvent, e => e.Unicode == unicode);
 
+            CacheFilePath.Instance.PropertyChanged += Instance_PropertyChanged;
 
             RefreshPageRangeItem();
         }
 
 
+
         #region 事件
+        private void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
+        {
+            if(e.PropertyName=="CopyDoc")
+            {
+                if(CacheFilePath.Instance.CopyDoc!=null)
+                {
+                    CanPaste = true;
+                }
+            }
+        }
+
+
         /// <summary>
         /// 替换功能的逻辑
         /// </summary>
@@ -906,6 +952,120 @@ namespace PDF_Office.ViewModels.PageEdit
 
         #region 方法
 
+        /// <summary>
+        /// 剪切
+        /// </summary>
+        private void cut()
+        {
+            CacheFilePath.Instance.CopyDoc = null;
+            CacheFilePath.Instance.CopyDoc = CPDFDocument.CreateDocument();
+            List<int> pages = new List<int>();
+            for (int i = 0; i < PageEditItems.Count; i++)
+            {
+                if (PageEditItems[i].Selected)
+                {
+                    pages.Add(i + 1);
+                }
+
+            }
+
+            bool result = CacheFilePath.Instance.CopyDoc.ImportPages(PDFViewer.Document, String.Join(",", pages));
+            if (!result)
+            {
+                ShowToast("复制失败");
+                return;
+            }
+
+            DoRemoveSelectedPages();
+            ReloadAfterOption(true, true, Tuple.Create(0, PDFViewer.Document.PageCount));
+        }
+
+        /// <summary>
+        /// 复制
+        /// </summary>
+        private void copy()
+        {
+            //付费锁
+            //if (!App.IsActive())
+            //{
+            //    IAPFunctionDialog dialog = new IAPFunctionDialog("PageEdit");
+            //    dialog.ShowDialog();
+            //    return;
+            //}
+            CacheFilePath.Instance.CopyDoc = null;
+            CacheFilePath.Instance.CopyDoc = CPDFDocument.CreateDocument();
+            List<int> pages = new List<int>();
+            for(int i=0;i<PageEditItems.Count;i++)
+            {
+                if(PageEditItems[i].Selected)
+                {
+                    pages.Add(i + 1);
+                }
+         
+            }
+
+            bool result = CacheFilePath.Instance.CopyDoc.ImportPages(PDFViewer.Document, String.Join(",",pages));
+            if (!result)
+            {
+                ShowToast("复制失败");
+                return;
+            }
+        }
+
+        /// <summary>
+        /// 是否可以粘贴
+        /// </summary>
+        /// <returns></returns>
+        private bool CanPasteExcute()
+        {
+            return this.CanPaste; 
+        }
+
+        /// <summary>
+        /// 粘贴
+        /// </summary>
+        private async void paste()
+        {
+            //付费锁
+            //if (!App.IsActive())
+            //{
+            //    IAPFunctionDialog dialog = new IAPFunctionDialog("PageEdit");
+            //    dialog.ShowDialog();
+            //    return;
+            //}
+            IsLoading = Visibility.Visible;
+            int insertindex = maxSelectedIndex;
+            insertindex++;
+            int pagecount = CacheFilePath.Instance.CopyDoc.PageCount;
+            bool result = PDFViewer.Document.ImportPagesAtIndex(CacheFilePath.Instance.CopyDoc, "1-" + pagecount, insertindex);
+            if (!result)
+            {
+                IsLoading = Visibility.Collapsed;
+                ShowToast("粘贴失败");
+                return;
+            }
+
+            //UI层插入
+            for (int i = 0; i < pagecount; i++)
+            {
+                var item = GetNewPageEditItem(i);
+                PageEditItems.Insert(insertindex + i, item);
+            }
+
+            //加一点延时才能显示选中效果
+            await Task.Delay(200);
+            List<int> pageRange = new List<int>();
+            for (int i = 0; i < pagecount; i++)
+            {
+                pageRange.Add(insertindex + i + 1);
+            }
+            NotifyUIToRefresh(pageRange);
+
+            //刷新页码等
+            ReloadAfterOption(true, true, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
+            IsLoading = Visibility.Collapsed;
+        }
+
         /// <summary>
         /// 页面编辑执行Undo操作的方法
         /// </summary>
@@ -1101,6 +1261,23 @@ namespace PDF_Office.ViewModels.PageEdit
             }
         }
 
+        /// <summary>
+        /// 获取已选中的页面集合
+        /// </summary>
+        /// <returns></returns>
+        private List<int> getSelectedPage()
+        {
+            List<int> pages = new List<int>();
+            for (int i = 0; i < PageEditItems.Count; i++)
+            {
+                if (PageEditItems[i].Selected)
+                {
+                    pages.Add(i);
+                }
+            }
+            return pages;
+        }
+
         /// <summary>
         /// 删除选中项的方法 不带Reload操作
         /// 方便其他逻辑中调用

+ 1 - 1
PDF Office/ViewModels/PropertyPanel/PDFEdit/TextEditPropertyViewModel.cs

@@ -16,7 +16,7 @@ using System.Windows.Controls;
 using System.Windows.Input;
 using System.Windows.Media;
 
-namespace PDF_Office.Views.PropertyPanel.PDFEdit
+namespace PDF_Office.ViewModels.PropertyPanel.PDFEdit
 {
     public class TextEditPropertyViewModel : BindableBase, INavigationAware
     {

+ 4 - 4
PDF Office/Views/PageEdit/PageEditContent.xaml

@@ -122,9 +122,9 @@
                 <MenuItem Command="{Binding DeleteCommand}" Header="删除页面" />
                 <MenuItem Command="{Binding RightRotateCommand}" Header="顺时针旋转页面" />
                 <MenuItem Command="{Binding LeftRotateCommand}" Header="逆时针旋转页面" />
-                <MenuItem Header="复制" />
-                <MenuItem Header="剪切" />
-                <MenuItem Header="粘贴" />
+                <MenuItem Command="{Binding CopyCommand}" Header="复制" />
+                <MenuItem Command="{Binding CutCommand}" Header="剪切" />
+                <MenuItem Command="{Binding PasteCommand}" Header="粘贴" />
                 <MenuItem
                     Command="{Binding ShowPageSizeCommand}"
                     Header="显示页面大小"
@@ -346,7 +346,7 @@
                         <Setter Property="HorizontalContentAlignment" Value="Center" />
                         <Setter Property="VerticalContentAlignment" Value="Center" />
                         <Setter Property="ContextMenu" Value="{StaticResource ListBoxItemMenu}" />
-                        <EventSetter Event="DragLeave" Handler="ListBoxItem_DragLeave"/>
+                        <EventSetter Event="DragLeave" Handler="ListBoxItem_DragLeave" />
                         <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBoxItem_PreviewMouseLeftButtonDown" />
                         <EventSetter Event="PreviewMouseDoubleClick" Handler="ListBoxItem_PreviewMouseDoubleClick" />
                     </Style>

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 818 - 254
PDF Office/Views/PropertyPanel/PDFEdit/TextEditProperty.xaml


+ 1 - 0
PDF Office/Views/PropertyPanel/PDFEdit/TextEditProperty.xaml.cs

@@ -1,4 +1,5 @@
 using PDF_Office.ViewModels.PropertyPanel;
+using PDF_Office.ViewModels.PropertyPanel.PDFEdit;
 using System;
 using System.Collections.Generic;
 using System.Linq;