Browse Source

PDFEdit(Windows) - 文字编辑Undo Redo 效果

liyuxuan 1 year ago
parent
commit
92e8370fdc

+ 1 - 0
compdfkit_demo_windows/compdfkit/compdfkit-tools/Edit/PDFTextEdit/PDFTextEditControl/PDFTextEditControl.xaml.cs

@@ -49,6 +49,7 @@ namespace compdfkit_tools.Edit
                 TextStyleUI.SelectFontName(newEvent.FontName);
                 TextStyleUI.SelectFontName(newEvent.FontName);
                 TextStyleUI.SetFontStyle(newEvent.IsBold,newEvent.IsItalic);
                 TextStyleUI.SetFontStyle(newEvent.IsBold,newEvent.IsItalic);
                 TextStyleUI.SetFontSize(newEvent.FontSize);
                 TextStyleUI.SetFontSize(newEvent.FontSize);
+                OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(newEvent.Transparency * 100/255D)));
                 TextAlignUI.SetFontAlign(newEvent.TextAlign);
                 TextAlignUI.SetFontAlign(newEvent.TextAlign);
             }
             }
             EditEvent = newEvent;
             EditEvent = newEvent;

+ 45 - 9
compdfkit_demo_windows/compdfkit/edit-ctrl-demo/MainWindow.xaml

@@ -5,6 +5,7 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:edit_ctrl_demo"
         xmlns:local="clr-namespace:edit_ctrl_demo"
         xmlns:cpdftools="clr-namespace:compdfkit_tools.PDFControl;assembly=com.compdfkit.tools"
         xmlns:cpdftools="clr-namespace:compdfkit_tools.PDFControl;assembly=com.compdfkit.tools"
+        
         mc:Ignorable="d"
         mc:Ignorable="d"
         Title="MainWindow" Height="700" Width="1200" WindowStartupLocation="CenterScreen">
         Title="MainWindow" Height="700" Width="1200" WindowStartupLocation="CenterScreen">
     <Window.Resources>
     <Window.Resources>
@@ -63,10 +64,22 @@
 
 
                     <!--保存文件-->
                     <!--保存文件-->
                     <Button BorderThickness="0" VerticalContentAlignment="Center" Margin="10,0,0,0" Width="30" Height="30" Style="{StaticResource LightButtonStyle}" Background="Transparent" 
                     <Button BorderThickness="0" VerticalContentAlignment="Center" Margin="10,0,0,0" Width="30" Height="30" Style="{StaticResource LightButtonStyle}" Background="Transparent" 
-                            Click="SaveFileBtn_Click">
+                            Click="SaveFileBtn_Click" IsEnabled="{Binding CanSave,Mode=OneWay}">
                         <Button.Content>
                         <Button.Content>
                             <Viewbox Width="20" Height="20">
                             <Viewbox Width="20" Height="20">
-                                <Path Fill="#43474D" >
+                                <Path IsEnabled="{Binding CanSave,Mode=OneWay}">
+                                    <Path.Style>
+                                        <Style TargetType="Path">
+                                            <Style.Triggers>
+                                                <Trigger Property="IsEnabled" Value="False">
+                                                    <Setter Property="Fill" Value="LightGray"/>
+                                                </Trigger>
+                                                <Trigger Property="IsEnabled" Value="True">
+                                                    <Setter Property="Fill" Value="#43474D"/>
+                                                </Trigger>
+                                            </Style.Triggers>
+                                        </Style>
+                                    </Path.Style>
                                     <Path.Data>
                                     <Path.Data>
                                         M11.707107,1 L15,4.29289322 L15,15 L1,15 L1,1 L11.707107,1 Z M11.293,2 L2,2 L2,14 L4,14 L4,8 L12,8 L12,14 L14,14 L14,4.707 L11.293,2 Z 
                                         M11.707107,1 L15,4.29289322 L15,15 L1,15 L1,1 L11.707107,1 Z M11.293,2 L2,2 L2,14 L4,14 L4,8 L12,8 L12,14 L14,14 L14,4.707 L11.293,2 Z 
                                         M11,9 L5,9 L5,14 L11,14 L11,9 Z M7,5 L7,6 L4,6 L4,5 L7,5 Z
                                         M11,9 L5,9 L5,14 L11,14 L11,9 Z M7,5 L7,6 L4,6 L4,5 L7,5 Z
@@ -112,12 +125,11 @@
 
 
                     <Line Margin="13,0,13,0" Stroke="#E0E0E0" X1="0" Y1="5" X2="0" Y2="35" Visibility="Collapsed"></Line>
                     <Line Margin="13,0,13,0" Stroke="#E0E0E0" X1="0" Y1="5" X2="0" Y2="35" Visibility="Collapsed"></Line>
 
 
-                    <Grid>
+                    <Grid Visibility="Collapsed">
                         <ComboBox Name="ZoomComboBox" VerticalContentAlignment="Center" Width="72" Height="30" Foreground="#43474D" FontSize="14"
                         <ComboBox Name="ZoomComboBox" VerticalContentAlignment="Center" Width="72" Height="30" Foreground="#43474D" FontSize="14"
                               SelectionChanged="ZoomComboBox_SelectionChanged" BorderBrush="#1E000000">
                               SelectionChanged="ZoomComboBox_SelectionChanged" BorderBrush="#1E000000">
                         </ComboBox>
                         </ComboBox>
-                        <TextBox Name="ZoomTextBox" IsHitTestVisible="False" Width="72" Height="30" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
-                             IsReadOnly="True" Background="White" Padding="0,0,15,0">100%</TextBox>
+                        <TextBox Name="ZoomTextBox" Width="72" Height="30" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Background="White" Padding="0,0,15,0">100%</TextBox>
                         <Path Fill="#43474D" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,5,0" IsHitTestVisible="False">
                         <Path Fill="#43474D" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,5,0" IsHitTestVisible="False">
                             <Path.Data>
                             <Path.Data>
                                 M0.5 0.510248L4.5041 5.5L8.5 0.5L0.5 0.510248Z
                                 M0.5 0.510248L4.5041 5.5L8.5 0.5L0.5 0.510248Z
@@ -154,7 +166,7 @@
                 <StackPanel Orientation="Horizontal" Grid.Column="2">
                 <StackPanel Orientation="Horizontal" Grid.Column="2">
 
 
                     <!--搜索按钮-->
                     <!--搜索按钮-->
-                    <Button BorderThickness="0" Width="40" Height="40" Style="{StaticResource LightButtonStyle}" Background="Transparent" Click="ExpandSearchBtn_Click">
+                    <Button BorderThickness="0" Width="40" Height="40" Style="{StaticResource LightButtonStyle}" Background="Transparent" Click="ExpandSearchBtn_Click" Focusable="False">
                         <Button.Content>
                         <Button.Content>
                             <Path Fill="#43474D">
                             <Path Fill="#43474D">
                                 <Path.Data>
                                 <Path.Data>
@@ -344,9 +356,21 @@
                             <Line Height="40" Stroke="#D5D6D8" StrokeThickness="2" X1="0" Y1="10" X2="0" Y2="30" Margin="8,0,8,0" />
                             <Line Height="40" Stroke="#D5D6D8" StrokeThickness="2" X1="0" Y1="10" X2="0" Y2="30" Margin="8,0,8,0" />
 
 
                             <!--撤销-->
                             <!--撤销-->
-                            <Button Style="{StaticResource LightButtonStyle}" BorderThickness="0"  Width="40" Height="40" IsEnabled="{Binding CanUndo,Mode=OneWay}" Click="UndoBtn_Click"
+                            <Button Name="UndoBtn" Style="{StaticResource LightButtonStyle}" BorderThickness="0"  Width="40" Height="40" IsEnabled="{Binding CanUndo,Mode=OneWay}" Click="UndoBtn_Click"
                                     Background="Transparent">
                                     Background="Transparent">
-                                <Path Fill="#43474D">
+                                <Path x:Name="UndoPath" IsEnabled="{Binding CanUndo,Mode=OneWay}">
+                                    <Path.Style>
+                                        <Style TargetType="Path">
+                                            <Style.Triggers>
+                                                <Trigger Property="IsEnabled" Value="False">
+                                                    <Setter Property="Fill" Value="LightGray"/>
+                                                </Trigger>
+                                                <Trigger Property="IsEnabled" Value="True">
+                                                    <Setter Property="Fill" Value="#43474D"/>
+                                                </Trigger>
+                                            </Style.Triggers>
+                                        </Style>
+                                    </Path.Style>
                                     <Path.Data>
                                     <Path.Data>
                                         M1.03033 3.62131L0.5 4.15164L1.03033 4.68197L4.65164 8.30328L5.7123 7.24261L3.37132 4.90164H10.0607C11.5794 4.90164 12.8107 6.13285 12.8107 
                                         M1.03033 3.62131L0.5 4.15164L1.03033 4.68197L4.65164 8.30328L5.7123 7.24261L3.37132 4.90164H10.0607C11.5794 4.90164 12.8107 6.13285 12.8107 
                                 7.65164C12.8107 9.17042 11.5794 10.4016 10.0607 10.4016H2.56066V11.9016H10.0607C12.4079 11.9016 14.3107 9.99885 14.3107 7.65164C14.3107 
                                 7.65164C12.8107 9.17042 11.5794 10.4016 10.0607 10.4016H2.56066V11.9016H10.0607C12.4079 11.9016 14.3107 9.99885 14.3107 7.65164C14.3107 
@@ -357,7 +381,19 @@
                             <!--重做-->
                             <!--重做-->
                             <Button Style="{StaticResource LightButtonStyle}" BorderThickness="0" Width="40" Height="40" Margin="10,0,0,0"  IsEnabled="{Binding CanRedo,Mode=OneWay}" Click="RedoBtn_Click"
                             <Button Style="{StaticResource LightButtonStyle}" BorderThickness="0" Width="40" Height="40" Margin="10,0,0,0"  IsEnabled="{Binding CanRedo,Mode=OneWay}" Click="RedoBtn_Click"
                                     Background="Transparent">
                                     Background="Transparent">
-                                <Path Fill="#43474D">
+                                <Path IsEnabled="{Binding CanRedo,Mode=OneWay}">
+                                    <Path.Resources>
+                                        <Style TargetType="Path">
+                                            <Style.Triggers>
+                                                <Trigger Property="IsEnabled" Value="False">
+                                                    <Setter Property="Fill" Value="LightGray"/>
+                                                </Trigger>
+                                                <Trigger Property="IsEnabled" Value="True">
+                                                    <Setter Property="Fill" Value="#43474D"/>
+                                                </Trigger>
+                                            </Style.Triggers>
+                                        </Style>
+                                    </Path.Resources>
                                     <Path.Data>
                                     <Path.Data>
                                         M13.7802 3.62131L14.3105 4.15164L13.7802 4.68197L10.1589 8.30328L9.09825 7.24261L11.4392 4.90164H4.74989C3.2311 4.90164 1.99989 6.13285 
                                         M13.7802 3.62131L14.3105 4.15164L13.7802 4.68197L10.1589 8.30328L9.09825 7.24261L11.4392 4.90164H4.74989C3.2311 4.90164 1.99989 6.13285 
                                 1.99989 7.65164C1.99989 9.17042 3.2311 10.4016 4.74989 10.4016H12.2499V11.9016H4.74989C2.40268 11.9016 0.499887 9.99885 0.499887 
                                 1.99989 7.65164C1.99989 9.17042 3.2311 10.4016 4.74989 10.4016H12.2499V11.9016H4.74989C2.40268 11.9016 0.499887 9.99885 0.499887 

+ 150 - 24
compdfkit_demo_windows/compdfkit/edit-ctrl-demo/MainWindow.xaml.cs

@@ -57,6 +57,18 @@ namespace edit_ctrl_demo
             }
             }
         }
         }
 
 
+        public bool CanSave
+        {
+            get
+            {
+                if (pdfViewControl != null && pdfViewControl.PDFView != null)
+                {
+                    return pdfViewControl.PDFView.UndoManager.CanSave;
+                }
+                return false;
+            }
+        }
+
         /// <summary>
         /// <summary>
         /// 上一次的PDF编辑对象
         /// 上一次的PDF编辑对象
         /// </summary>
         /// </summary>
@@ -153,32 +165,139 @@ namespace edit_ctrl_demo
 
 
         private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
         private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
         {
         {
-            if (e.PressOnSelectedText)
-            {
-                e.Handle = true;
-                e.PopupMenu = new ContextMenu();
-                e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
-            }
-            else if (e.CommandTarget == TargetType.ImageSelection)
+            if (e != null && e.CommandType == CommandType.Context)
             {
             {
-                if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
+                if (e.PressOnSelectedText)
+                {
+                    e.Handle = true;
+                    e.PopupMenu = new ContextMenu();
+                    e.PopupMenu.Items.Add(new MenuItem() { Header = "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.Handle = true;
                     e.PopupMenu = new ContextMenu();
                     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);
+                    MenuItem fitWidthMenu = new MenuItem();
+                    fitWidthMenu.Header = "Fit Width";
+                    fitWidthMenu.Click += (o, p) =>
+                    {
+                        if (pdfViewControl != null)
+                        {
+                            pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
+                        }
+                    };
+
+                    e.PopupMenu.Items.Add(fitWidthMenu);
+
+                    MenuItem fitSizeMenu = new MenuItem();
+                    fitSizeMenu.Header = "Actual Size";
+                    fitSizeMenu.Click += (o, p) =>
+                    {
+                        if (pdfViewControl != null)
+                        {
+                            pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
+                        }
+                    };
+
+                    e.PopupMenu.Items.Add(fitSizeMenu);
+
+                    MenuItem zoomInMenu = new MenuItem();
+                    zoomInMenu.Header = "Zoom In";
+                    zoomInMenu.Click += (o, p) =>
+                    {
+                        if (pdfViewControl != null)
+                        {
+                            double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
+                            pdfViewControl.PDFView?.Zoom(newZoom);
+                        }
+                    };
+
+                    e.PopupMenu.Items.Add(zoomInMenu);
+
+                    MenuItem zoomOutMenu = new MenuItem();
+                    zoomOutMenu.Header = "Zoom Out";
+                    zoomOutMenu.Click += (o, p) =>
+                    {
+                        if (pdfViewControl != null)
+                        {
+                            double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
+                            pdfViewControl.PDFView?.Zoom(newZoom);
+                        }
+                    };
+
+                    e.PopupMenu.Items.Add(zoomOutMenu);
+                    e.PopupMenu.Items.Add(new Separator());
+
+                    MenuItem singleView = new MenuItem();
+                    singleView.Header = "Single Page";
+                    singleView.Click += (o, p) =>
+                    {
+                        if (pdfViewControl != null)
+                        {
+                            pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
+                        }
+                    };
+
+                    e.PopupMenu.Items.Add(singleView);
+
+                    MenuItem singleContinuousView = new MenuItem();
+                    singleContinuousView.Header = "Single Continuous Page";
+                    singleContinuousView.Click += (o, p) =>
+                    {
+                        if (pdfViewControl != null)
+                        {
+                            pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
+                        }
+                    };
+
+                    e.PopupMenu.Items.Add(singleContinuousView);
+
+                    MenuItem doubleView = new MenuItem();
+                    doubleView.Header = "Double Page";
+                    doubleView.Click += (o, p) =>
+                    {
+                        if (pdfViewControl != null)
+                        {
+                            pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
+                        }
+                    };
+
+                    e.PopupMenu.Items.Add(doubleView);
+
+                    MenuItem doubleContinuousView = new MenuItem();
+                    doubleContinuousView.Header = "Double Continuous Page";
+                    doubleContinuousView.Click += (o, p) =>
+                    {
+                        if (pdfViewControl != null)
+                        {
+                            pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
+                        }
+                    };
+
+                    e.PopupMenu.Items.Add(doubleContinuousView);
                 }
                 }
             }
             }
         }
         }
@@ -299,6 +418,7 @@ namespace edit_ctrl_demo
         {
         {
             PDFEditEvent tempEvent = lastPDFEditEvent;
             PDFEditEvent tempEvent = lastPDFEditEvent;
             lastPDFEditEvent = e;
             lastPDFEditEvent = e;
+            ViewSettingBtn.IsChecked = false;
             if (e == null)
             if (e == null)
             {
             {
                 PropertyContainer.Child = null;
                 PropertyContainer.Child = null;
@@ -706,13 +826,18 @@ namespace edit_ctrl_demo
             {
             {
                 ClearPDFEditState(senderBtn);
                 ClearPDFEditState(senderBtn);
                 senderBtn.IsChecked = false;
                 senderBtn.IsChecked = false;
-                pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
-                pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
-                pdfViewControl.PDFView?.ReloadDocument();
+               
                 OpenFileDialog openFileDialog = new OpenFileDialog();
                 OpenFileDialog openFileDialog = new OpenFileDialog();
                 openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
                 openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
                 if (openFileDialog.ShowDialog() == true)
                 if (openFileDialog.ShowDialog() == true)
                 {
                 {
+                    pdfViewControl.PDFView?.ClearSelectPDFEdit();
+                    pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
+                    pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
+                    pdfViewControl.PDFView?.ReloadDocument();
+                   
+                    PropertyContainer.Child = null;
+                    PropertyContainer.Visibility = Visibility.Collapsed;
                     pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
                     pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
                     pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
                     pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
                 }
                 }
@@ -1072,6 +1197,7 @@ namespace edit_ctrl_demo
 
 
             }
             }
         }
         }
+
     }
     }
 }
 }