Browse Source

ComPDFKit.Demo(win) - 文档对比框架

weixiangjie 2 weeks ago
parent
commit
bc78dab4a3

+ 21 - 0
Demo/Examples/Compdfkit.Controls/Common/BarControl/CPDFComparisonBarControl.xaml

@@ -0,0 +1,21 @@
+<UserControl x:Class="ComPDFKit.Controls.Common.CPDFComparisonBarControl"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+             xmlns:local="clr-namespace:ComPDFKit.Controls.Common"
+             mc:Ignorable="d"
+             Loaded="UserControl_Loaded"
+             d:DesignHeight="300" d:DesignWidth="300">
+    
+    <UserControl.Resources>
+        <ResourceDictionary>
+            <ResourceDictionary.MergedDictionaries>
+                <ResourceDictionary Source="../../Asset/Styles/ToggleButtonStyle.xaml"></ResourceDictionary>
+            </ResourceDictionary.MergedDictionaries>
+        </ResourceDictionary>
+    </UserControl.Resources>
+    <Border Background="#F2F3F5" Padding="0,2,0,2">
+        <Grid  Name="ComparisonBarGrid" Width="0"></Grid>
+    </Border>
+</UserControl>

File diff suppressed because it is too large
+ 146 - 0
Demo/Examples/Compdfkit.Controls/Common/BarControl/CPDFComparisonBarControl.xaml.cs


+ 48 - 0
Demo/Examples/Compdfkit.Controls/Comparison/ComparisonControl.xaml

@@ -0,0 +1,48 @@
+<UserControl x:Class="ComPDFKit.Controls.PDFView.ComparisonControl"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+             xmlns:local="clr-namespace:ComPDFKit.Controls.PDFView"
+             xmlns:common="clr-namespace:ComPDFKit.Controls.Common"
+             mc:Ignorable="d"
+             Loaded="UserControl_Loaded"
+             Unloaded="UserControl_Unloaded"
+             d:DesignHeight="720" d:DesignWidth="1080">
+    <Grid>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="auto"></RowDefinition>
+            <RowDefinition Height="auto"></RowDefinition>
+            <RowDefinition Height="*"></RowDefinition>
+        </Grid.RowDefinitions>
+        <common:CPDFComparisonBarControl x:Name="ComparisonBarControl" Visibility="Visible"></common:CPDFComparisonBarControl>
+        <Border Grid.Row="1" x:Name="SignatureStatusBorder" Visibility="Collapsed"/>
+        <Grid Grid.Row="2" x:Name="BodyGrid">
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="0"></ColumnDefinition>
+                <ColumnDefinition Width="0"></ColumnDefinition>
+                <ColumnDefinition Width="*"></ColumnDefinition>
+                <ColumnDefinition Width="auto"></ColumnDefinition>
+            </Grid.ColumnDefinitions>
+                <Border Visibility="Collapsed" x:Name="BotaContainer"></Border>
+                <GridSplitter Name="Splitter" Grid.Column="1" Width="15" ResizeBehavior="PreviousAndNext" Visibility="Collapsed">
+                    <GridSplitter.Template>
+                        <ControlTemplate TargetType="{x:Type GridSplitter}">
+                            <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
+                                    BorderBrush="{TemplateBinding Border.BorderBrush}"
+                                    Background="{TemplateBinding Panel.Background}" >
+                                <Path Fill="Gray" VerticalAlignment="Center">
+                                    <Path.Data>
+                                        M15 3.75H1V2.25H15V3.75ZM15 8.75H1V7.25H15V8.75ZM1 13.75H15V12.25H1V13.75Z
+                                    </Path.Data>
+                                </Path>
+                            </Border>
+                        </ControlTemplate>
+                    </GridSplitter.Template>
+                </GridSplitter>
+                <Border Grid.Column="2" x:Name="PDFGrid" Background="#CECECE"></Border>
+                <Border Grid.Column="3" Name="PropertyContainer" Visibility="Collapsed" ></Border>
+        </Grid>
+        <common:PageNumberControl Grid.Row="2" x:Name="FloatPageTool" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,20"></common:PageNumberControl>
+    </Grid>
+</UserControl>

+ 325 - 0
Demo/Examples/Compdfkit.Controls/Comparison/ComparisonControl.xaml.cs

@@ -0,0 +1,325 @@
+using System;
+using System.ComponentModel;
+using System.Diagnostics;
+using System.Drawing;
+using System.IO;
+using System.Runtime.CompilerServices;
+using System.Windows;
+using System.Windows.Input;
+using System.Windows.Media.Imaging;
+using ComPDFKit.Controls.Helper;
+using ComPDFKit.Controls.PDFControl;
+using ComPDFKit.PDFPage;
+using ComPDFKit.Tool;
+using ContextMenu = System.Windows.Controls.ContextMenu;
+using MenuItem = System.Windows.Controls.MenuItem;
+using UserControl = System.Windows.Controls.UserControl;
+using System.Collections.Generic;
+using ComPDFKit.Tool.DrawTool;
+
+namespace ComPDFKit.Controls.PDFView
+{
+    public partial class ComparisonControl : UserControl, INotifyPropertyChanged
+    {
+        public PDFViewControl PdfViewControl;
+        public CPDFAnnotationControl PDFAnnotationControl = new CPDFAnnotationControl();
+        private SignatureStatusBarControl signatureStatusBarControl;
+        private CPDFDisplaySettingsControl displaySettingsControl = null;
+        private PanelState panelState = PanelState.GetInstance();
+        private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
+        public event PropertyChangedEventHandler PropertyChanged;
+        public event EventHandler<bool> OnCanSaveChanged;
+
+        private bool CanSave
+        {
+            get
+            {
+                if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
+                {
+                    if (PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
+                        PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
+                    {
+                        return true;
+                    }
+                }
+
+                return false;
+            }
+        }
+
+        public ComparisonControl()
+        {
+            InitializeComponent();
+            panelState.PropertyChanged -= PanelState_PropertyChanged;
+            panelState.PropertyChanged += PanelState_PropertyChanged;
+        }
+
+        public void SetPropertyContainer(UIElement uiElement)
+        {
+            PropertyContainer.Child = uiElement;
+        }
+
+        private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
+        {
+            if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
+            {
+                ExpandLeftPanel(panelState.IsLeftPanelExpand);
+            }
+            else if (e.PropertyName == nameof(PanelState.RightPanel))
+            {
+                if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
+                {
+                    ExpandRightPropertyPanel(PDFAnnotationControl, Visibility.Visible);
+                }
+                else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
+                {
+                    ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
+                }
+                else
+                {
+                    ExpandRightPropertyPanel(null, Visibility.Collapsed);
+
+                }
+            }
+        }
+
+        public void ExpandLeftPanel(bool isExpand)
+        {
+            BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
+            Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
+            if (isExpand)
+            {
+                BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
+                BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
+            }
+            else
+            {
+                BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
+                BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
+            }
+        }
+
+
+
+        public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
+        {
+            PropertyContainer.Width = 260;
+            PropertyContainer.Child = propertytPanel;
+            PropertyContainer.Visibility = visible;
+        }
+
+        #region Init PDFViewer
+
+        private void InitialControl()
+        {
+            PDFGrid.Child = PdfViewControl;
+            PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
+            PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged;
+        }
+
+        private void PdfViewControl_MouseRightButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
+        {
+            ContextMenu ContextMenu = PdfViewControl.GetRightMenu();
+            if (ContextMenu == null)
+            {
+                ContextMenu = new ContextMenu();
+            }
+            switch (e.hitTestType)
+            {
+                case MouseHitTestType.Annot:
+                case MouseHitTestType.SelectRect:
+                    break;
+                case MouseHitTestType.Text:
+                    CreateSelectTextContextMenu(sender, ref ContextMenu);
+                    break;
+                case MouseHitTestType.ImageSelect:
+                    CreateSelectImageContextMenu(sender, ref ContextMenu);
+                    break;
+                default:
+                    PdfViewControl.CreateViewerMenu(sender, ref ContextMenu);
+                    break;
+            }
+            PdfViewControl.SetRightMenu(ContextMenu);
+        }
+
+        private void CreateSelectImageContextMenu(object sender, ref ContextMenu menu)
+        {
+            if (menu == null)
+            {
+                menu = new ContextMenu();
+            }
+            MenuItem copyImage = new MenuItem();
+            copyImage.Header = "Copy Image";
+            copyImage.Click += CopyImage_Click;
+            menu.Items.Add(copyImage);
+
+            MenuItem extractImage = new MenuItem();
+            extractImage.Header = "Extract Image";
+            extractImage.Click += ExtractImage_Click;
+            menu.Items.Add(extractImage);
+        }
+
+        private void CreateSelectTextContextMenu(object sender, ref ContextMenu menu)
+        {
+            menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
+        }
+
+        public void InitWithPDFViewer(PDFViewControl pdfViewer)
+        {
+            PdfViewControl = pdfViewer;
+            PDFGrid.Child = PdfViewControl;
+            FloatPageTool.InitWithPDFViewer(pdfViewer);
+            InitialControl();
+            DataContext = this;
+            if (PdfViewControl != null && PdfViewControl.PDFViewTool.GetCPDFViewer() != null)
+            {
+                PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
+                PdfViewControl.MouseRightButtonDownHandler += PdfViewControl_MouseRightButtonDownHandler;
+            }
+        }
+        
+
+        public void SetBOTAContainer(CPDFBOTABarControl botaControl)
+        {
+            this.BotaContainer.Child = botaControl;
+        }
+
+        public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
+        {
+            this.displaySettingsControl = displaySettingsControl;
+        }
+
+        public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
+        {
+            this.signatureStatusBarControl = signatureStatusBarControl;
+            SignatureStatusBorder.Child = this.signatureStatusBarControl;
+            if (signatureStatusBarControl.Status != SignatureStatus.None)
+            {
+                SignatureStatusBorder.Visibility = Visibility.Visible;
+            }
+            else
+            {
+                SignatureStatusBorder.Visibility = Visibility.Collapsed;
+            }
+        }
+
+        #endregion
+
+        public void ClearViewerControl()
+        {
+            PDFGrid.Child = null;
+            BotaContainer.Child = null;
+            PropertyContainer.Child = null;
+            SignatureStatusBorder.Child = null;
+            displaySettingsControl = null;
+        }
+
+        #region PropertyChanged
+
+        /// <summary>
+        /// Undo Redo Event Noitfy
+        /// </summary>
+        private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
+        {
+            OnPropertyChanged(e.PropertyName);
+            if (e.PropertyName == "CanSave")
+            {
+                OnCanSaveChanged?.Invoke(this, CanSave);
+            }
+        }
+
+        protected void OnPropertyChanged([CallerMemberName] string name = null)
+        {
+            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
+        }
+
+        #endregion
+
+        #region Context Menu
+
+        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)
+            {
+                PageImageItem image = null;
+                Dictionary<int, List<PageImageItem>> pageImageDict = PdfViewControl.FocusPDFViewTool.GetSelectImageItems();
+                if (pageImageDict != null && pageImageDict.Count > 0)
+                {
+                    foreach (int pageIndex in pageImageDict.Keys)
+                    {
+                        List<PageImageItem> imageItemList = pageImageDict[pageIndex];
+                        image = imageItemList[0];
+                        break;
+                    }
+                }
+
+                if (image == null)
+                {
+                    return;
+                }
+
+                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)
+        {
+            PageImageItem image = null;
+            Dictionary<int, List<PageImageItem>> pageImageDict = PdfViewControl.FocusPDFViewTool.GetSelectImageItems();
+            if (pageImageDict != null && pageImageDict.Count > 0)
+            {
+                foreach (int pageIndex in pageImageDict.Keys)
+                {
+                    List<PageImageItem> imageItemList = pageImageDict[pageIndex];
+                    image = imageItemList[0];
+                    break;
+                }
+            }
+
+            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);
+
+            Bitmap bitmap = new Bitmap(tempPath);
+            BitmapImage imageData;
+            using (MemoryStream ms = new MemoryStream())
+            {
+                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);
+            }
+        }
+        #endregion
+
+        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
+        {
+            PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler;
+        }
+
+        private void UserControl_Loaded(object sender, RoutedEventArgs e)
+        {
+            
+        }
+    }
+}

+ 12 - 7
Demo/Examples/Compdfkit.Controls/Compdfkit.Controls.csproj

@@ -166,6 +166,9 @@
       <DependentUpon>CPDFDeleteDialog.xaml</DependentUpon>
     </Compile>
     <Compile Include="Asset\Styles\TCIRadioButton.cs" />
+    <Compile Include="Common\BarControl\CPDFComparisonBarControl.xaml.cs">
+      <DependentUpon>CPDFComparisonBarControl.xaml</DependentUpon>
+    </Compile>
     <Compile Include="Common\BarControl\CPDFDigitalSignatureBarControl.xaml.cs">
       <DependentUpon>CPDFDigitalSignatureBarControl.xaml</DependentUpon>
     </Compile>
@@ -323,6 +326,7 @@
     <Compile Include="Common\PropertyControl\WritableComboBoxControl.xaml.cs">
       <DependentUpon>WritableComboBoxControl.xaml</DependentUpon>
     </Compile>
+    <Compile Include="Comparison\ComparisonControl.xaml.cs" />
     <Compile Include="Compress\CompressDialog.xaml.cs">
       <DependentUpon>CompressDialog.xaml</DependentUpon>
     </Compile>
@@ -901,6 +905,7 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </Page>
+    <Page Include="Common\BarControl\CPDFComparisonBarControl.xaml" />
     <Page Include="Common\BarControl\CPDFDigitalSignatureBarControl.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
@@ -1061,6 +1066,7 @@
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
     </Page>
+    <Page Include="Comparison\ComparisonControl.xaml" />
     <Page Include="Compress\CompressDialog.xaml">
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
@@ -1397,19 +1403,18 @@
     </Page>
   </ItemGroup>
   <ItemGroup>
-    <ProjectReference Include="..\..\..\..\ComPDFKit-ForRebuild\ComPDFKit\ComPDFKitCSharp\ComPDFKit.NET.csproj">
+    <ProjectReference Include="..\..\..\..\ComPDFKit_Rebuild\ComPDFKit.Viewer\ComPDFKit.Viewer.csproj">
+      <Project>{783263cf-0da3-4095-9df8-2c4a6b3ff908}</Project>
+      <Name>ComPDFKit.Viewer</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\..\..\..\compdfkit_rebuild\ComPDFKit\ComPDFKitCSharp\ComPDFKit.NET.csproj">
       <Project>{122caf49-dcf7-49b1-8872-80b363e187e4}</Project>
       <Name>ComPDFKit.NET</Name>
     </ProjectReference>
-    <ProjectReference Include="..\..\..\..\ComPDFKit-Rebuild\ComPDFKit.Tool\ComPDFKit.Tool.csproj">
+    <ProjectReference Include="..\ComPDFKit.Tool\ComPDFKit.Tool.csproj">
       <Project>{a061ee7a-6704-4bd9-86ee-48ed5df75e2f}</Project>
       <Name>ComPDFKit.Tool</Name>
     </ProjectReference>
-    <ProjectReference Include="..\..\..\..\ComPDFKit-Rebuild\ComPDFKit.Viewer\ComPDFKit.Viewer.csproj">
-      <Project>{783263cf-0da3-4095-9df8-2c4a6b3ff908}</Project>
-      <Name>ComPDFKit.Viewer</Name>
-    </ProjectReference>
   </ItemGroup>
-  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 2 - 0
Demo/Examples/PDFViewer/MainPage.xaml

@@ -196,6 +196,8 @@
                     <ComboBoxItem Tag="Document Editor" Content="{Binding Converter={StaticResource CommonResourceConverter},ConverterParameter=Func_DocEditor}"></ComboBoxItem>
                     <ComboBoxItem Tag="Digital Signature" Content="{Binding Converter={StaticResource CommonResourceConverter},ConverterParameter=Func_Signatures}"></ComboBoxItem>
                     <ComboBoxItem Tag="Measurement" Content="{Binding Converter={StaticResource CommonResourceConverter},ConverterParameter=Func_Measurement}"></ComboBoxItem>
+                    <ComboBoxItem Tag="Comparison" Content="Compare Documents"></ComboBoxItem>
+
                 </ComboBox>
 
                 <StackPanel Orientation="Horizontal" Grid.Column="2">

+ 19 - 0
Demo/Examples/PDFViewer/MainPage.xaml.cs

@@ -34,6 +34,7 @@ namespace PDFViewer
         private PDFViewControl viewControl;
         private PDFViewControl passwordViewer;
         private RegularViewerControl regularViewerControl = new RegularViewerControl();
+        private ComparisonControl comparisonControl = new ComparisonControl();
         private AnnotationControl annotationControl = new AnnotationControl();
         private FormControl formControl = new FormControl();
         private ContentEditControl contentEditControl = new ContentEditControl();
@@ -569,6 +570,10 @@ namespace PDFViewer
                 measureControl.ClearViewerControl();
                 measureControl.UnloadEvent();
             }
+            else if(currentMode == "Comparison")
+            {
+                comparisonControl.ClearViewerControl();
+            }
 
             if (item.Tag as string == "Viewer")
             {
@@ -688,6 +693,20 @@ namespace PDFViewer
                     measureControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
                 }
             }
+            else if(item.Tag as string == "Comparison")
+            {
+                comparisonControl.PdfViewControl = viewControl;
+                comparisonControl.InitWithPDFViewer(viewControl);
+                if (comparisonControl.PdfViewControl != null)
+                {
+                    PDFGrid.Child = comparisonControl;
+                    viewControl?.SetToolType(ToolType.Viewer);
+                    comparisonControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
+                    comparisonControl.OnCanSaveChanged += ControlOnCanSaveChanged;
+                    comparisonControl.SetBOTAContainer(botaBarControl);
+                    comparisonControl.SetDisplaySettingsControl(displaySettingsControl);
+                }
+            }
 
             currentMode = item.Tag as string;
             RightToolPanelButtonIsChecked = false;