Quellcode durchsuchen

compdfkit(win) - viewer项目调整,菜单功能补充

weixiangjie vor 9 Monaten
Ursprung
Commit
5366a34d06

Datei-Diff unterdrückt, da er zu groß ist
+ 3 - 2
Demo/Examples/Viewer/App.xaml.cs


+ 85 - 212
Demo/Examples/Viewer/MainWindow.xaml.cs

@@ -15,6 +15,9 @@ using System.Windows.Controls;
 using System.Windows.Controls.Primitives;
 using System.Windows.Input;
 using System.Windows.Media.Imaging;
+using ComPDFKit.PDFPage;
+using ComPDFKit.Tool;
+
 namespace Viewer
 {
     /// <summary>
@@ -70,24 +73,18 @@ namespace Viewer
                 return;
             }
             pdfViewControl.PDFToolManager?.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Viewer);
-            pdfViewControl.PDFView?.Load();
-            pdfViewControl.PDFView?.SetShowLink(true);
             PDFGrid.Child = pdfViewControl;
-            pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
-            pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
-            pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
-            pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
-            pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
-            pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
-            pdfViewControl.PDFView.SetFormFieldHighlight(true);
             PasswordUI.Closed -= PasswordUI_Closed;
             PasswordUI.Canceled -= PasswordUI_Canceled;
             PasswordUI.Confirmed -= PasswordUI_Confirmed;
             PasswordUI.Closed += PasswordUI_Closed;
             PasswordUI.Canceled += PasswordUI_Canceled;
             PasswordUI.Confirmed += PasswordUI_Confirmed;
+            
+            pdfViewControl.MouseRightButtonDownHandler -= PDFViewControl_MouseRightButtonDownHandler;
+            pdfViewControl.MouseRightButtonDownHandler += PDFViewControl_MouseRightButtonDownHandler;
 
-            pdfViewControl.PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitWidth);
+            pdfViewControl.PDFViewTool.GetCPDFViewer().SetFitMode(FitMode.FitWidth);
             CPDFSaclingControl.InitWithPDFViewer(pdfViewControl);
             CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFViewTool.GetCPDFViewer().GetZoom() * 100)));
 
@@ -311,10 +308,10 @@ namespace Viewer
         {
             if (pdfViewControl != null && pdfViewControl.PDFToolManager != null && pdfViewControl.PDFToolManager.GetDocument() != null)
             {
-                CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
+                CPDFDocument pdfDoc = pdfViewControl.PDFToolManager.GetDocument();
                 if (pdfDoc.WriteToLoadedPath())
                 {
-                    pdfViewControl.PDFView.UndoManager.CanSave = false;
+                    pdfViewControl.PDFViewTool.IsDocumentModified = false;
                     return;
                 }
 
@@ -327,7 +324,7 @@ namespace Viewer
                 {
                     if (pdfDoc.WriteToFilePath(saveDialog.FileName))
                     {
-                        pdfViewControl.PDFView.UndoManager.CanSave = false;
+                        pdfViewControl.PDFViewTool.IsDocumentModified = false;
                     }
                 }
             }
@@ -338,9 +335,9 @@ namespace Viewer
             string filePath = CommonHelper.GetExistedPathOrEmpty();
             if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
             {
-                if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
+                if (pdfViewControl.PDFToolManager != null && pdfViewControl.PDFToolManager.GetDocument() != null)
                 {
-                    string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
+                    string oldFilePath = pdfViewControl.PDFToolManager.GetDocument().FilePath;
                     if (oldFilePath.ToLower() == filePath.ToLower())
                     {
                         return;
@@ -348,14 +345,14 @@ namespace Viewer
                 }
 
                 passwordViewer = new PDFViewControl();
-                passwordViewer.PDFView.InitDocument(filePath);
-                if (passwordViewer.PDFView.Document == null)
+                passwordViewer.InitDocument(filePath);
+                if (passwordViewer.PDFToolManager.GetDocument() == null)
                 {
                     MessageBox.Show("Open File Failed");
                     return;
                 }
 
-                if (passwordViewer.PDFView.Document.IsLocked)
+                if (passwordViewer.PDFToolManager.GetDocument().IsLocked)
                 {
                     PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
                     PasswordUI.ClearPassword();
@@ -364,7 +361,7 @@ namespace Viewer
                 }
                 else
                 {
-                    pdfViewControl.PDFView.Document.Release();
+                    pdfViewControl.PDFToolManager.GetDocument().Release();
                     pdfViewControl = passwordViewer;
                     LoadDocument();
                 }
@@ -385,214 +382,90 @@ namespace Viewer
 
         #region Context Menu
 
-        private void ExtraImage_Click(object sender, RoutedEventArgs e)
+        private void ExtractImage_Click(object sender, RoutedEventArgs e)
         {
             System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
             if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
-                string choosePath = folderDialog.SelectedPath;
-                string openPath = choosePath;
-                Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
-
-                if (imageDict != null && imageDict.Count > 0)
+                var image = pdfViewControl.FocusPDFViewTool.GetSelectImage();
+                if(image == null)
                 {
-                    foreach (int pageIndex in imageDict.Keys)
-                    {
-                        List<Bitmap> imageList = imageDict[pageIndex];
-                        foreach (Bitmap image in imageList)
-                        {
-                            string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
-                            image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
-                            openPath = savePath;
-                        }
-                    }
+                    return;
                 }
-                Process.Start("explorer", "/select,\"" + openPath + "\"");
+                CPDFPage page = pdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
+                string savePath = Path.Combine(folderDialog.SelectedPath, Guid.NewGuid() + ".jpg");
+                string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
+                page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
+
+                Bitmap bitmap = new Bitmap(tempPath);
+                bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
+                Process.Start("explorer", "/select,\"" + savePath + "\"");
             }
         }
 
         private void CopyImage_Click(object sender, RoutedEventArgs e)
         {
-            Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
+            var image = pdfViewControl.FocusPDFViewTool.GetSelectImage();
+            if(image == null)
+            {
+                return;
+            }
+            CPDFPage page = pdfViewControl.PDFToolManager.GetDocument().PageAtIndex(image.PageIndex);
+            string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".jpg");
+            page.GetImgSelection().GetImgBitmap(image.ImageIndex, tempPath);
 
-            if (imageDict != null && imageDict.Count > 0)
+            Bitmap bitmap = new Bitmap(tempPath);
+            BitmapImage imageData;
+            using (MemoryStream ms = new MemoryStream())
             {
-                foreach (int pageIndex in imageDict.Keys)
-                {
-                    List<Bitmap> imageList = imageDict[pageIndex];
-                    foreach (Bitmap image in imageList)
-                    {
-                        MemoryStream ms = new MemoryStream();
-                        image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
-                        BitmapImage imageData = new BitmapImage();
-                        imageData.BeginInit();
-                        imageData.StreamSource = ms;
-                        imageData.CacheOption = BitmapCacheOption.OnLoad;
-                        imageData.EndInit();
-                        imageData.Freeze();
-                        Clipboard.SetImage(imageData);
-                        break;
-                    }
-                }
+                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
+                imageData = new BitmapImage();
+                imageData.BeginInit();
+                imageData.StreamSource = ms;
+                
+                imageData.CacheOption = BitmapCacheOption.OnLoad;
+                imageData.EndInit();
+                imageData.Freeze();
+                Clipboard.SetImage(imageData);
+                bitmap.Dispose();
+                File.Delete(tempPath);
             }
         }
-
-        private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
+        
+        private void CreateImageContextMenu(object sender, ref ContextMenu menu)
         {
-            if (e != null && e.CommandType == CommandType.Context)
+            if (menu == null)
             {
-                if (e.PressOnSelectedText)
-                {
-                    e.Handle = true;
-                    e.PopupMenu = new ContextMenu();
-                    e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
-                }
-                else if (e.CommandTarget == TargetType.ImageSelection)
-                {
-                    if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
-                    {
-                        e.Handle = true;
-                        e.PopupMenu = new ContextMenu();
-
-                        MenuItem imageCopyMenu = new MenuItem();
-                        imageCopyMenu = new MenuItem();
-                        imageCopyMenu.Header = "Copy Images";
-                        WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
-                        imageCopyMenu.CommandParameter = e;
-                        e.PopupMenu.Items.Add(imageCopyMenu);
-
-                        MenuItem imageExtraMenu = new MenuItem();
-                        imageExtraMenu = new MenuItem();
-                        imageExtraMenu.Header = "Extract Images";
-                        WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
-                        imageExtraMenu.CommandParameter = e;
-                        e.PopupMenu.Items.Add(imageExtraMenu);
-                    }
-                }
-                else
-                {
-                    e.Handle = true;
-                    e.PopupMenu = new ContextMenu();
-                    //if (pdfViewControl.CheckHasForm())
-
-                    MenuItem fitWidthMenu = new MenuItem();
-                    fitWidthMenu.Header = LanguageHelper.CommonManager.GetString("Menu_AutoSize");
-                    fitWidthMenu.Click += (o, p) =>
-                    {
-                        if (pdfViewControl != null)
-                        {
-                            pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
-                        }
-                    };
-
-                    e.PopupMenu.Items.Add(fitWidthMenu);
-
-                    MenuItem fitSizeMenu = new MenuItem();
-                    fitSizeMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RealSize");
-                    fitSizeMenu.Click += (o, p) =>
-                    {
-                        if (pdfViewControl != null)
-                        {
-                            pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
-                        }
-                    };
-
-                    e.PopupMenu.Items.Add(fitSizeMenu);
-
-                    MenuItem zoomInMenu = new MenuItem();
-                    zoomInMenu.Header = LanguageHelper.CommonManager.GetString("Menu_ZoomIn");
-                    zoomInMenu.Click += (o, p) =>
-                    {
-                        if (pdfViewControl != null)
-                        {
-                            double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
-                            pdfViewControl.PDFView?.Zoom(newZoom);
-                        }
-                    };
-
-                    e.PopupMenu.Items.Add(zoomInMenu);
-
-                    MenuItem zoomOutMenu = new MenuItem();
-                    zoomOutMenu.Header = LanguageHelper.CommonManager.GetString("Menu_ZoomOut");
-                    zoomOutMenu.Click += (o, p) =>
-                    {
-                        if (pdfViewControl != null)
-                        {
-                            double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
-                            pdfViewControl.PDFView?.Zoom(newZoom);
-                        }
-                    };
-
-                    e.PopupMenu.Items.Add(zoomOutMenu);
-                    e.PopupMenu.Items.Add(new Separator());
-
-                    MenuItem singleView = new MenuItem();
-                    singleView.Header = LanguageHelper.CommonManager.GetString("Menu_SinglePage");
-                    singleView.Click += (o, p) =>
-                    {
-                        if (pdfViewControl != null)
-                        {
-                            pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
-                        }
-                    };
-
-                    e.PopupMenu.Items.Add(singleView);
-
-                    MenuItem singleContinuousView = new MenuItem();
-                    singleContinuousView.Header = LanguageHelper.CommonManager.GetString("Menu_SingleContinuous");
-                    singleContinuousView.Click += (o, p) =>
-                    {
-                        if (pdfViewControl != null)
-                        {
-                            pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
-                        }
-                    };
-
-                    e.PopupMenu.Items.Add(singleContinuousView);
-
-                    MenuItem doubleView = new MenuItem();
-                    doubleView.Header = LanguageHelper.CommonManager.GetString("Menu_DoublePage");
-                    doubleView.Click += (o, p) =>
-                    {
-                        if (pdfViewControl != null)
-                        {
-                            pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
-                        }
-                    };
-
-                    e.PopupMenu.Items.Add(doubleView);
-
-                    MenuItem doubleContinuousView = new MenuItem();
-                    doubleContinuousView.Header = LanguageHelper.CommonManager.GetString("Menu_DoubleContinuous");
-                    doubleContinuousView.Click += (o, p) =>
-                    {
-                        if (pdfViewControl != null)
-                        {
-                            pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
-                        }
-                    };
-                    e.PopupMenu.Items.Add(doubleContinuousView);
-
-                    {
-                        MenuItem resetForms = new MenuItem();
-                        resetForms.Header = LanguageHelper.CommonManager.GetString("Menu_Reset");
-                        resetForms.Click += (o, p) =>
-                        {
-                            if (pdfViewControl != null)
-                            {
-                                pdfViewControl.PDFView?.ResetForm(null);
-                            }
-                        };
-                        e.PopupMenu.Items.Add(new Separator());
-                        e.PopupMenu.Items.Add(resetForms);
-                    }
-                }
+                menu = new ContextMenu();
             }
+            MenuItem copyImage = new MenuItem();
+            copyImage.Header = "Copy Image";
+            copyImage.Click += CopyImage_Click;
+            menu.Items.Add(copyImage);
 
-            if (e != null && e.CommandType == CommandType.Copy)
+            MenuItem extractImage = new MenuItem();
+            extractImage.Header = "Extract Image";
+            extractImage.Click += ExtractImage_Click;
+            menu.Items.Add(extractImage);
+        }
+        
+        private void PDFViewControl_MouseRightButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
+        {
+            ContextMenu menu = pdfViewControl.GetRightMenu();
+            if (menu == null)
+            {
+                menu = new ContextMenu();
+            }
+            switch (e.hitTestType)
             {
-                e.DoCommand();
+                case MouseHitTestType.ImageSelect:
+                    CreateImageContextMenu(sender, ref menu);
+                    break;
+                default:
+                    pdfViewControl.CreateViewerMenu(sender,ref menu);
+                    break;
             }
+            pdfViewControl.SetRightMenu(menu);
         }
 
         #endregion
@@ -601,7 +474,7 @@ namespace Viewer
 
         protected override void OnClosing(CancelEventArgs e)
         {
-            if (pdfViewControl.PDFView.UndoManager.CanSave)
+            if (pdfViewControl.PDFViewTool.IsDocumentModified)
             {
                 MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
                 if (result == MessageBoxResult.Yes)
@@ -704,14 +577,14 @@ namespace Viewer
         }
         private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
         {
-            double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
-            pdfViewControl.PDFView?.Zoom(newZoom);
+            //double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
+            //pdfViewControl.PDFView?.Zoom(newZoom);
         }
 
         private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
         {
-            double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
-            pdfViewControl.PDFView?.Zoom(newZoom);
+            //double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
+            //pdfViewControl.PDFView?.Zoom(newZoom);
         }
 
         private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
@@ -726,7 +599,7 @@ namespace Viewer
             {
                 PasswordUI.Visibility = Visibility.Collapsed;
                 FileInfoUI.Visibility = Visibility.Visible;
-                FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
+                FileInfoControl.InitWithPDFViewer(pdfViewControl);
                 PopupBorder.Visibility = Visibility.Visible;
             }
             else

+ 15 - 3
Demo/Examples/Viewer/Viewer.csproj

@@ -113,16 +113,28 @@
     <Resource Include="ComPDFKit_Logo.ico" />
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\..\..\..\compdfkit_windows\ComPDFKit.Tool\ComPDFKit.Tool.csproj">
+      <Project>{a061ee7a-6704-4bd9-86ee-48ed5df75e2f}</Project>
+      <Name>ComPDFKit.Tool</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\..\..\..\compdfkit_windows\ComPDFKit.Viewer\ComPDFKit.Viewer.csproj">
+      <Project>{783263cf-0da3-4095-9df8-2c4a6b3ff908}</Project>
+      <Name>ComPDFKit.Viewer</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\..\..\..\compdfkit_windows\ComPDFKit\ComPDFKitCSharp\ComPDFKit.Desk.csproj">
+      <Project>{56e518ad-c126-4b48-9a09-0a64c87020e4}</Project>
+      <Name>ComPDFKit.Desk</Name>
+    </ProjectReference>
     <ProjectReference Include="..\Compdfkit_Tools\Compdfkit_Tools.csproj">
       <Project>{9363bcb1-3a67-446a-8093-5708b86bf418}</Project>
       <Name>Compdfkit_Tools</Name>
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
-    <None Include="..\license_key_windows.xml">
-      <Link>license_key_windows.xml</Link>
+    <Content Include="..\..\..\..\x64\ComPDFKit.dll">
+      <Link>x64\ComPDFKit.dll</Link>
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
+    </Content>
     <Content Include="..\TestFile\ComPDFKit_Sample_File_Windows.pdf">
       <Link>ComPDFKit_Sample_File_Windows.pdf</Link>
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>