Bladeren bron

编辑PDF - 添加文本和图像

chenrongqian 2 jaren geleden
bovenliggende
commit
5d25270656

+ 45 - 0
PDF Office/Model/PropertyPanel/AnnotPanel/FontStyleItem.cs

@@ -17,4 +17,49 @@ namespace PDF_Office.Model.PropertyPanel.AnnotPanel
         public FontWeight mFontWeight { get; set; }
 
     }
+
+    public class LoadFontStyle
+    {
+        public static List<FontStyleItem> Load()
+        {
+            List<FontStyleItem> fontStyleList = new List<FontStyleItem>();
+            FontStyleItem custom = new FontStyleItem();
+            custom.mFontSize = 32;
+            custom.mFontStyleName = "自定义";
+
+            FontStyleItem h1 = new FontStyleItem();
+            h1.mFontSize = 24;
+            h1.mFontStyleName = "H1大标题";
+
+            FontStyleItem h2 = new FontStyleItem();
+            h2.mFontSize = 16;
+            h2.mFontStyleName = "h2(标准)";
+
+            FontStyleItem h3 = new FontStyleItem();
+            h3.mFontSize = 10;
+            h3.mFontStyleName = "H3小标题";
+
+            FontStyleItem b1 = new FontStyleItem();
+            b1.mFontSize = 8;
+            b1.mFontStyleName = "B1标题";
+
+            FontStyleItem b2 = new FontStyleItem();
+            b2.mFontSize = 6;
+            b2.mFontStyleName = "B2标题";
+
+            FontStyleItem b3 = new FontStyleItem();
+            b3.mFontSize = 4;
+            b3.mFontStyleName = "B3标题";
+
+            fontStyleList.Add(custom);
+            fontStyleList.Add(h1);
+            fontStyleList.Add(h2);
+            fontStyleList.Add(h3);
+            fontStyleList.Add(b1);
+            fontStyleList.Add(b2);
+            fontStyleList.Add(b3);
+
+            return fontStyleList;
+        }
+    }
 }

+ 1 - 35
PDF Office/ViewModels/PropertyPanel/AnnotPanel/FreetextAnnotPropertyViewModel.cs

@@ -135,41 +135,7 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
 
         private void InitFontStyles()
         {
-            FontStyleItem custom = new FontStyleItem();
-            custom.mFontSize = 32;
-            custom.mFontStyleName = "自定义";
-
-            FontStyleItem h1 = new FontStyleItem();
-            h1.mFontSize = 24;
-            h1.mFontStyleName = "H1大标题";
-
-            FontStyleItem h2 = new FontStyleItem();
-            h2.mFontSize = 16;
-            h2.mFontStyleName = "h2(标准)";
-
-            FontStyleItem h3 = new FontStyleItem();
-            h3.mFontSize = 10;
-            h3.mFontStyleName = "H3小标题";
-
-            FontStyleItem b1 = new FontStyleItem();
-            b1.mFontSize = 8;
-            b1.mFontStyleName = "B1标题";
-
-            FontStyleItem b2 = new FontStyleItem();
-            b2.mFontSize = 6;
-            b2.mFontStyleName = "B2标题";
-
-            FontStyleItem b3 = new FontStyleItem();
-            b3.mFontSize = 4;
-            b3.mFontStyleName = "B3标题";
-
-            FontStyleList.Add(custom);
-            FontStyleList.Add(h1);
-            FontStyleList.Add(h2);
-            FontStyleList.Add(h3);
-            FontStyleList.Add(b1);
-            FontStyleList.Add(b2);
-            FontStyleList.Add(b3);
+            FontStyleList = LoadFontStyle.Load();
         }
         private void TextAlign_Checked(object obj)
         {

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

@@ -1,19 +1,273 @@
-using Prism.Mvvm;
+using ComPDFKitViewer;
+using ComPDFKitViewer.PdfViewer;
+using PDF_Office.Model;
+using PDF_Office.Model.PropertyPanel.AnnotPanel;
+using Prism.Commands;
+using Prism.Mvvm;
 using Prism.Regions;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+using System.Windows.Media;
 
 namespace PDF_Office.ViewModels.PropertyPanel
 {
     public class TextEditPropertyViewModel : BindableBase, INavigationAware
     {
+        #region 属性
+
+        private Brush selectColor = new SolidColorBrush(Colors.Black);
+        public Brush SelectColor
+        {
+            get { return selectColor; }
+            set
+            {
+                SetProperty(ref selectColor, value);
+
+                if (TextEditEvent != null)
+                {
+                    bool isok = TextEditEvent.FontColor.A != (SelectColor as SolidColorBrush).Color.A ||
+                         TextEditEvent.FontColor.B != (SelectColor as SolidColorBrush).Color.B ||
+                         TextEditEvent.FontColor.G != (SelectColor as SolidColorBrush).Color.G ||
+                         TextEditEvent.FontColor.R != (SelectColor as SolidColorBrush).Color.R;
+
+                    if (isok)
+                    {
+                        TextEditEvent.FontColor = (SelectColor as SolidColorBrush).Color;
+                        TextEditEvent.UpdatePDFEditByEventArgs();
+                    }
+
+                }
+
+
+            }
+        }
+
+        private FontFamily fontFamily = new FontFamily("Courier");
+        public FontFamily TextFontFamily
+        {
+            get { return fontFamily; }
+            set
+            {
+                SetProperty(ref fontFamily, value);
+                if (TextEditEvent != null)
+                {
+                    TextEditEvent.FontFamily = fontFamily;
+                    TextEditEvent.UpdatePDFEditByEventArgs();
+                }
+            }
+        }
+
+        private FontWeight fontWeights = FontWeights.Normal;
+        public FontWeight TextFontWeights
+        {
+            get { return fontWeights; }
+            set
+            {
+                SetProperty(ref fontWeights, value);
+                if (TextEditEvent != null)
+                {
+                    TextEditEvent.FontWeight = fontWeights;
+                    TextEditEvent.UpdatePDFEditByEventArgs();
+                }
+            }
+        }
+
+        private FontStyle fontStyle = FontStyles.Normal;
+        public FontStyle TextFontStyle
+        {
+            get { return fontStyle; }
+            set
+            {
+                SetProperty(ref fontStyle, value);
+                if (TextEditEvent != null)
+                {
+                    TextEditEvent.FontStyle = fontStyle;
+                    TextEditEvent.UpdatePDFEditByEventArgs();
+                }
+            }
+        }
+
+
+        private int fontSize = 24;
+        public int TextFontSize
+        {
+            get { return fontSize; }
+            set
+            {
+                SetProperty(ref fontSize, value);
+                if (TextEditEvent != null)
+                {
+                    TextEditEvent.FontSize = fontSize;
+                    TextEditEvent.UpdatePDFEditByEventArgs();
+                }
+            }
+        }
+
+        private List<FontStyleItem> fontStyleList = new List<FontStyleItem>();
+        public List<FontStyleItem> FontStyleList
+        {
+            get { return fontStyleList; }
+            set
+            {
+                SetProperty(ref fontStyleList, value);
+            }
+        }
+        #endregion
+
+        #region Command
+
+        public DelegateCommand<object> SelectedColorCommand { get; set; }
+        public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
+        public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
+        public DelegateCommand<object> FontStyleChangedCommand { get; set; }
+        public DelegateCommand<object> FontSizeChangedCommand { get; set; }
+
+        #endregion
+
+        private ComPDFKitViewer.PDFEditEvent TextEditEvent;
         public TextEditPropertyViewModel()
         {
+            InitVariable();
+            InitCommand();
+        }
+
+        private void InitVariable()
+        {
+            InitFontStyles();
+        }
 
+        private void InitFontStyles()
+        {
+            FontStyleList = LoadFontStyle.Load();
         }
+
+        private void InitCommand()
+        {
+            SelectedColorCommand = new DelegateCommand<object>(SelectedColor);
+            SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
+            FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged);
+            FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged);
+            FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged);
+        }
+
+        private void SelectedColor(object obj)
+        {
+            if (obj != null)
+            {
+                var colorValue = (Color)obj;
+                if (colorValue != null)
+                {
+
+                    SelectColor = new SolidColorBrush(colorValue);
+                }
+            }
+        }
+
+        private void SelectedFontStyle(object obj)
+        {
+            if (obj != null && (FontStyleItem)obj != null)
+            {
+                var item = (FontStyleItem)obj;
+
+            }
+        }
+
+
+        private void FontFamilyChanged(object obj)
+        {
+            if (obj != null)
+            {
+                if ((int)obj > -1)
+                {
+                    if ((int)obj == 0)
+                    {
+
+                        TextFontFamily = new FontFamily("Courier");
+                    }
+
+
+                    if ((int)obj == 1)
+                    {
+                        TextFontFamily = new FontFamily("Helvetica");
+
+                    }
+
+
+                    if ((int)obj == 2)
+                    {
+                        TextFontFamily = new FontFamily("Times");
+
+                    }
+
+                }
+            }
+
+        }
+
+
+
+        private void FontStyleChanged(object obj)
+        {
+            if (obj != null)
+            {
+                var item = (ComboBoxItem)obj;
+                var content = (string)item.Content;
+                if (content != null)
+                {
+                    if (content == "Regular")
+                    {
+                        TextFontWeights = FontWeights.Normal;
+                        TextFontStyle = FontStyles.Normal;
+                    }
+
+
+                    if (content == "Bold")
+                    {
+                        TextFontWeights = FontWeights.Bold;
+                        TextFontStyle = FontStyles.Normal;
+                    }
+
+
+                    if (content == "Italic")
+                    {
+                        TextFontWeights = FontWeights.Normal;
+                        TextFontStyle = FontStyles.Italic;
+                    }
+
+                    if (content == "Bold Italic")
+                    {
+                        TextFontWeights = FontWeights.Bold;
+                        TextFontStyle = FontStyles.Italic;
+                    }
+
+                }
+            }
+        }
+
+
+        private void FontSizeChanged(object obj)
+        {
+            if (obj != null)
+            {
+                var item = (ComboBoxItem)obj;
+                var content = (string)item.Content;
+                if (content != null)
+                {
+                    var intData = int.Parse(content);
+                    TextFontSize = intData;
+                }
+            }
+        }
+
+
+
+
         public void OnNavigatedTo(NavigationContext navigationContext)
         {
 
@@ -24,9 +278,91 @@ namespace PDF_Office.ViewModels.PropertyPanel
             return true;
         }
 
+        private CPDFViewer PDFViewer;
         public void OnNavigatedFrom(NavigationContext navigationContext)
         {
+            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
+            if(PDFViewer != null)
+            {
+                PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
+                PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
+
+                PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
+                PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
+                
+            }
+        }
+
+        private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
+        {
+            ContextMenu popMenu = new ContextMenu();
+            var menu = new MenuItem();
+            menu.Header = "Cut";
+            popMenu.Items.Add(menu);
+            menu = new MenuItem();
+            menu.Header = "Copy";
+            popMenu.Items.Add(menu);
+            menu = new MenuItem();
+            menu.Header = "Paste";
+            popMenu.Items.Add(menu);
+            menu = new MenuItem();
+            menu.Header = "Delete";
+            popMenu.Items.Add(menu);
+            menu = new MenuItem();
+            menu.Header = "SelectAll";
+            popMenu.Items.Add(menu);
 
+
+            switch (e.CommandType)
+            {
+                case CommandType.Context:
+                    if (popMenu.Items.Count == 5)
+                    {
+                        MenuItem menuItem = popMenu.Items[0] as MenuItem;
+                        menuItem.CommandTarget = (UIElement)sender;
+                        menuItem.Command = ApplicationCommands.Cut;
+
+                        menuItem = popMenu.Items[1] as MenuItem;
+                        menuItem.CommandTarget = (UIElement)sender;
+                        menuItem.Command = ApplicationCommands.Copy;
+
+                        menuItem = popMenu.Items[2] as MenuItem;
+                        menuItem.CommandTarget = (UIElement)sender;
+                        menuItem.Command = ApplicationCommands.Paste;
+
+                        menuItem = popMenu.Items[3] as MenuItem;
+                        menuItem.CommandTarget = (UIElement)sender;
+                        menuItem.Command = ApplicationCommands.Delete;
+
+                        menuItem = popMenu.Items[4] as MenuItem;
+                        menuItem.CommandTarget = (UIElement)sender;
+                        menuItem.Command = ApplicationCommands.SelectAll;
+
+                        e.PopupMenu = popMenu;
+                        if (e.PopupMenu != null)
+                        {
+                            e.Handle = true;
+                        }
+                    }
+                    break;
+
+                default:
+                    e.DoCommand();
+                    break;
+
+            }
+        }
+
+        private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
+        {
+            if(e != null && e.Count > 0)
+            {
+                TextEditEvent = e[0];
+            }
+            else
+            {
+               // TextEditEvent = null;
+            }
         }
     }
 }

+ 70 - 3
PDF Office/ViewModels/Tools/TextEditToolContentViewModel.cs

@@ -5,22 +5,89 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows.Media;
+using System.Windows;
+using PDF_Office.Model.PropertyPanel.AnnotPanel;
+using System.Windows.Controls;
+using Prism.Regions;
+using ComPDFKitViewer.PdfViewer;
+using PDF_Office.Model;
+using Microsoft.Win32;
+using PDF_Office.CustomControl;
 
 namespace PDF_Office.ViewModels.Tools
 {
-    public class TextEditToolContentViewModel: BindableBase
+    public class TextEditToolContentViewModel: BindableBase, INavigationAware
     {
-       private ComPDFKitViewer.PDFEditEvent TextEditEvent;
+        #region Command
         public DelegateCommand<object> AddContentCommand { get; set; }
+
+        #endregion
         public TextEditToolContentViewModel()
+        {
+            InitCommand();
+        }
+
+        private void InitCommand()
         {
             AddContentCommand = new DelegateCommand<object>(AddContent);
         }
 
         public void AddContent(object obj)
         {
-           if(TextEditEvent != null)
+            if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
+
+            var btn = obj as CustomIconToggleBtn;
+
+            if(btn.IsChecked == true)
+            {
+                if (btn.Tag.ToString() == "Text")
+                {
+                    PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
+                }
+                else
+                {
+                    OpenFileDialog openFileDialog = new OpenFileDialog();
+                    openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
+                    openFileDialog.Multiselect = true;
+                    if ((bool)openFileDialog.ShowDialog())
+                    {
+                        if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
+                        {
+                            PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
+                            PDFViewer.AddPDFEditImage(openFileDialog.FileName);
+                            btn.IsChecked = false;
+                        }
+                    }
+
+                }
+            }
+            else
+            {
+                PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
+                PDFViewer.SetMouseMode(MouseModes.PDFEdit);
+            }
+
+
+        }
+
+        public void OnNavigatedTo(NavigationContext navigationContext)
+        {
+
+        }
+
+        public bool IsNavigationTarget(NavigationContext navigationContext)
+        {
+            return true;
+        }
+
+        private CPDFViewer PDFViewer;
+        public void OnNavigatedFrom(NavigationContext navigationContext)
+        {
+            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
+            if (PDFViewer != null)
             {
+             
 
             }
         }

+ 5 - 6
PDF Office/Views/PropertyPanel/TextEditProperty.xaml

@@ -8,6 +8,7 @@
              prism:ViewModelLocator.AutoWireViewModel="True"
              xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
              xmlns:Convert="clr-namespace:PDF_Office.DataConvert"
+              xmlns:cusColor="clr-namespace:PDF_Office.CustomControl.CompositeControl"
              xmlns:cus="clr-namespace:PDF_Office.CustomControl"
              xmlns:CompositeControl="clr-namespace:PDF_Office.CustomControl.CompositeControl"
              mc:Ignorable="d" 
@@ -69,15 +70,13 @@
             <TextBlock Name="AnnotTypeTitle" FontFamily="Segoe UI Semibold" FontWeight="SemiBold" FontSize="18" LineHeight="24" HorizontalAlignment="Left" Margin="10,8,0,0">Freetext</TextBlock>
             <Border Width="228" Height="100" BorderThickness="1" CornerRadius="2" BorderBrush="#DDDDDD" Background="White" Margin="0,8,0,0">
                 <Grid>
-                    <Border Name="FreeTextBorder" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="5"
-                            Opacity="{Binding FillOpacity}"
-                            Background="{Binding FillColor}">
+                    <Border Name="FreeTextBorder" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="5">
                         <TextBlock Name="SampleText"
                                    Foreground="{Binding SelectColor}" 
                                    FontFamily="{Binding TextFontFamily}"
                                    FontWeight="{Binding TextFontWeights}" 
                                    FontStyle="{Binding TextFontStyle}"
-                                   FontSize="{Binding TextFontSize}" Opacity="{Binding FillOpacity}"
+                                   FontSize="{Binding TextFontSize}" 
                                    Text="Sample" HorizontalAlignment="Center" VerticalAlignment="Center">
                         </TextBlock>
                     </Border>
@@ -162,8 +161,8 @@
                     </Grid>
                 </Border>
 
-                <cus:ColorDropBox x:Name="FontColorBox" Grid.Column="1" Grid.Row="2" SelectedColorChanged="FontColorBox_SelectedColorChanged" BorderThickness="1" BorderBrush="#E2E3E6" Width="80" Height="32" HorizontalAlignment="Left"
-                                   ></cus:ColorDropBox>
+                <cusColor:ColorSubContent x:Name="FontColorBox" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left"
+                                   ></cusColor:ColorSubContent>
 
                 <Border Grid.Row="1" BorderBrush="#E2E3E6"  Width="148" Height="32" Margin="12,8,4,0"  BorderThickness="0">
                     <Grid>

+ 10 - 3
PDF Office/Views/PropertyPanel/TextEditProperty.xaml.cs

@@ -1,4 +1,5 @@
-using System;
+using PDF_Office.ViewModels.PropertyPanel;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -23,13 +24,19 @@ namespace PDF_Office.Views.PropertyPanel
         public TextEditProperty()
         {
             InitializeComponent();
+            FontColorBox.SelectedColorHandler += FontColorBox_SelectedColorHandler;
         }
 
-        private void FontColorBox_SelectedColorChanged(object sender, Color? e)
+        private void FontColorBox_SelectedColorHandler(object sender, Color e)
         {
+            var data = this.DataContext as TextEditPropertyViewModel;
+            if (data != null)
+            {
+                data.SelectedColorCommand?.Execute(e);
+            }
 
         }
-
+        
         private void BtnTextAlign_Click(object sender, RoutedEventArgs e)
         {
 

+ 4 - 4
PDF Office/Views/Tools/TextEditToolContent.xaml

@@ -11,13 +11,13 @@
              d:DesignHeight="450" d:DesignWidth="800">
     <Grid >
         <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
-            <customControl:CustomIconToggleBtn x:Name="BtnAddText" Tag="SnapshotEdit" Style="{StaticResource ToggleBtnViewModeStyle}"  Foreground="Black" ContentStringFormat="156"
-                                               Command="{Binding AddContentCommand}" CommandParameter="{Binding ElementName=BtnAddText,Path=Tag}"  
+            <customControl:CustomIconToggleBtn x:Name="BtnAddText" Tag="Text" Style="{StaticResource ToggleBtnViewModeStyle}"  Foreground="Black" ContentStringFormat="156"
+                                               Command="{Binding AddContentCommand}" CommandParameter="{Binding ElementName=BtnAddText}"  
                                                >
                 <TextBlock Text="添加文本"/>
             </customControl:CustomIconToggleBtn>
-            <customControl:CustomIconToggleBtn x:Name="BtnAddImage" Tag="SnapshotEdit" Style="{StaticResource ToggleBtnViewModeStyle}"  Foreground="Black" ContentStringFormat="156"
-                                             Command="{Binding AddContentCommand}" CommandParameter="{Binding ElementName=BtnAddImage,Path=Tag}"  
+            <customControl:CustomIconToggleBtn x:Name="BtnAddImage" Tag="Image" Style="{StaticResource ToggleBtnViewModeStyle}"  Foreground="Black" ContentStringFormat="156"
+                                             Command="{Binding AddContentCommand}" CommandParameter="{Binding ElementName=BtnAddImage}"  
                                                >
                 <TextBlock Text="添加图像"/>
             </customControl:CustomIconToggleBtn>