Browse Source

图章-自定义图章相关内容(测试用)

zhuyi 2 years ago
parent
commit
cf63669796

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

@@ -56,6 +56,8 @@ namespace PDF_Office
 
         public static List<string> OpenedFileList = new List<string>();
 
+        public static CacheFilePath CachePath;
+
         public static bool IsFirstOpen = true;
         public static bool IsBookMode = false;
 
@@ -71,7 +73,7 @@ namespace PDF_Office
             LicenseVerify();
 
             InitSettings();
-
+            CachePath = CacheFilePath.Instance;
             try
             {
                 DirectoryInfo info = new DirectoryInfo(CurrentPath);

+ 1 - 1
PDF Office/Helper/CacheFilePath.cs

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace PDF_Office.Helper
 {
-    class CacheFilePath
+    public class CacheFilePath
     {
         private static readonly CacheFilePath instance = new CacheFilePath();
         private CacheFilePath()

+ 11 - 0
PDF Office/Model/AnnotPanel/Stamp.cs

@@ -120,5 +120,16 @@ namespace PDF_Office.Model.AnnotPanel
             }
         }
 
+        private TextStampSharp textSharp;
+
+        public TextStampSharp TextSharp
+        {
+            get { return textSharp; }
+            set
+            {
+                SetProperty(ref textSharp, value);
+            }
+        }
+
     }
 }

+ 12 - 1
PDF Office/Properties/Settings.Designer.cs

@@ -12,7 +12,7 @@ namespace PDF_Office.Properties {
     
     
     [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
-    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.1.0.0")]
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.10.0.0")]
     internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
         
         private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
@@ -77,5 +77,16 @@ namespace PDF_Office.Properties {
                 this["QuickPDFToolsList"] = value;
             }
         }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        public global::PDFSettings.CustomStampList CustomStampList {
+            get {
+                return ((global::PDFSettings.CustomStampList)(this["CustomStampList"]));
+            }
+            set {
+                this["CustomStampList"] = value;
+            }
+        }
     }
 }

+ 3 - 0
PDF Office/Properties/Settings.settings

@@ -17,5 +17,8 @@
     <Setting Name="QuickPDFToolsList" Type="PDFSettings.QuickPDFToolsList" Scope="User">
       <Value Profile="(Default)" />
     </Setting>
+    <Setting Name="CustomStampList" Type="PDFSettings.CustomStampList" Scope="User">
+      <Value Profile="(Default)" />
+    </Setting>
   </Settings>
 </SettingsFile>

+ 35 - 4
PDF Office/ViewModels/PropertyPanel/AnnotPanel/CustomCreateDialogViewModel.cs

@@ -1,4 +1,5 @@
 using ComPDFKit.PDFAnnotation;
+using ComPDFKitViewer.AnnotEvent;
 using ComPDFKitViewer.PdfViewer;
 using PDF_Office.Model;
 using Prism.Commands;
@@ -6,6 +7,7 @@ using Prism.Mvvm;
 using Prism.Services.Dialogs;
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -87,6 +89,9 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
             set { color = value; }
         }
 
+        public string SaveToPath { get; private set; }
+        public string StampTextDate { get; private set; }
+        public StampType Type { get; private set; }
 
         public void SetStampStyle(int index)
         {
@@ -156,12 +161,36 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
 
         private void Create()
         {
+            SaveImageToPath();
+            DialogParameters valuePairs = new DialogParameters();
+            valuePairs.Add(ParameterNames.DataModel, this);
+            RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
+        }
 
+        private void SaveImageToPath()
+        {
+            string path = App.CachePath.CustomStampPath;
+            string name = Guid.NewGuid().ToString();
+            if (!string.IsNullOrEmpty(path))
+            {
+                BitmapEncoder encoder = new PngBitmapEncoder();
+                encoder.Frames.Add(BitmapFrame.Create(ImageSource));
+                path = System.IO.Path.Combine(path, name);
+                using (FileStream stream = new FileStream(path, FileMode.Create))
+                {
+                    encoder.Save(stream);
+                }
+                SaveToPath = path;
+            }
+            else
+            {
+                SaveToPath = "";
+            }
         }
 
         public bool CanCloseDialog()
         {
-            return true; 
+            return true;
         }
 
         public void OnDialogClosed()
@@ -181,7 +210,7 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                 UpDataStamp();
             }
         }
-        private void UpDataStamp()
+        public void UpDataStamp()
         {
             string date = "";
             string dateType = "";
@@ -191,13 +220,13 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
             }
             if (IsCheckedTime)
             {
-                dateType= dateType+ " HH:mm:ss";
+                dateType = dateType + " HH:mm:ss";
             }
             if (!String.IsNullOrEmpty(dateType))
             {
                 date = DateTime.Now.ToString(dateType);
             }
-            
+
             var bytes = CPDFStampAnnotation.GetTempTextStampImage(StampText, date,
             Shape, Color, out int stampWidth, out int stampHeight, out int width, out int height);
             if (bytes.Length > 0)
@@ -205,6 +234,8 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                 PixelFormat fmt = PixelFormats.Bgra32;
                 BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
                 ImageSource = bps;
+                StampTextDate = date;
+                Type = StampType.TEXT_STAMP;
             }
             else
             {

+ 77 - 4
PDF Office/ViewModels/PropertyPanel/AnnotPanel/StampAnnotPropertyViewModel.cs

@@ -3,8 +3,10 @@ using ComPDFKit.PDFDocument;
 using ComPDFKit.PDFPage;
 using ComPDFKitViewer.AnnotEvent;
 using ComPDFKitViewer.PdfViewer;
+using Microsoft.Win32;
 using PDF_Office.Model;
 using PDF_Office.Model.AnnotPanel;
+using PDF_Office.Properties;
 using Prism.Commands;
 using Prism.Mvvm;
 using Prism.Regions;
@@ -12,6 +14,7 @@ using Prism.Services.Dialogs;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -158,10 +161,25 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
             ShowDialogCommand = new DelegateCommand(ShowDialog);
             InitStandardStamp();
             UpDataDynamicStampList();
-            //CPDFDocument doc = CPDFDocument.CreateDocument();
-            //bool tt= doc.InsertPage(0, 300, 500, Path[0]);
-            //doc.WriteToFilePath("D:\\fafdas.pdf");
-            //doc.Release();
+        }
+
+        private void LoadSettings()
+        {
+            PDFSettings.CustomStampList stamps = Settings.Default.CustomStampList;
+            for (int i = 0; i < stamps.Count; i++)
+            {
+                Stamp customStamp = new Stamp();
+                customStamp.Opacity = 1;
+                customStamp.Author = stamps[i].Author;
+                customStamp.StampText = stamps[i].StampText;
+                customStamp.StampTextDate = stamps[i].StampTextDate;
+                customStamp.MaxWidth = stamps[i].ImageWidth;
+                customStamp.MaxHeight = stamps[i].ImageHeight;
+                customStamp.SourcePath = stamps[i].SourcePath;
+                customStamp.Type = stamps[i].Type;
+                customStamp.TextColor = stamps[i].TextColor;
+                CustomStampList.Add(customStamp);
+            }
         }
 
         /// <summary>
@@ -308,6 +326,46 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
             }
         }
 
+        public void SaveToPath(string FileType, Stamp Item)
+        {
+            SaveFileDialog saveFileDialog = new SaveFileDialog();
+            saveFileDialog.Title = "保存" + FileType + "文件";
+            saveFileDialog.Filter = "(*)|*." + FileType;
+
+            if (saveFileDialog.ShowDialog() == false)
+            {
+                return;
+            }
+            BitmapEncoder encoder;
+            if (FileType.ToUpper() == "JPG")
+            {
+                encoder = new JpegBitmapEncoder();
+            }
+            else
+            {
+                encoder = new PngBitmapEncoder();
+            }
+            encoder.Frames.Add(BitmapFrame.Create(new Uri(Item.SourcePath)));
+
+            if (FileType.ToUpper() != "PDF")
+            {
+                string path = saveFileDialog.FileName;
+                using (FileStream stream = new FileStream(path, FileMode.Create))
+                {
+                    encoder.Save(stream);
+                }
+
+                CPDFDocument doc = CPDFDocument.CreateDocument();
+                bool tt = doc.InsertPage(0, 300, 500, path);
+                doc.WriteToFilePath("C:\\Users\\93131\\Desktop\\test\\cache\\fafdas.pdf");
+                doc.Release();
+                System.Diagnostics.Process.Start("explorer", "/select,\"" + path + "\"");
+            }
+            else
+            {
+
+            }
+        }
         private void ShowDialog()
         {
             switch (TabControlSelectedIndex)
@@ -362,6 +420,7 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                 CustomCreateDialogViewModel CustomVM = e.Parameters.GetValue<CustomCreateDialogViewModel>(ParameterNames.DataModel);
                 if (CustomVM != null)
                 {
+                    CreateCustomStamp(CustomVM);
                 }
             });
             if (!result)
@@ -369,5 +428,19 @@ namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
                 return;
             }
         }
+
+        private void CreateCustomStamp(CustomCreateDialogViewModel viewModel)
+        {
+            Stamp stamp = new Stamp();
+            stamp.Author = "";
+            stamp.Opacity = 1;
+            stamp.SourcePath = viewModel.SaveToPath;
+            stamp.StampText = viewModel.StampText;
+            stamp.MaxWidth = viewModel.ImageSource.PixelWidth;
+            stamp.MaxHeight = viewModel.ImageSource.PixelHeight;
+            stamp.StampTextDate = viewModel.StampTextDate;
+            stamp.Type = viewModel.Type;
+            CustomStampList.Add(stamp);
+        }
     }
 }

+ 2 - 2
PDF Office/Views/PropertyPanel/AnnotPanel/CustomCreateDialog.xaml

@@ -8,7 +8,7 @@
 
     <cus:DialogContent Header="新建图章">
         <cus:DialogContent.Content>
-            <TabControl Grid.Row="1" Name="StampTabControl" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Style="{StaticResource TabControlWithUnderLineStyle}">
+            <TabControl Grid.Row="1" Name="StampTabControl"  HorizontalAlignment="Center" HorizontalContentAlignment="Center" Style="{StaticResource TabControlWithUnderLineStyle}">
                 <TabItem x:Name="文字图章"
                         Header="Standard"
                         FontFamily="Segoe UI" Foreground="#FF666666"
@@ -25,7 +25,7 @@
                             <RowDefinition Height="*"/>
                         </Grid.RowDefinitions>
                         <Image Height="50" Source="{Binding ImageSource}"/>
-                        <TextBox Grid.Row="1" Text="{Binding StampText,Mode=TwoWay}">
+                        <TextBox Grid.Row="1" Text="{Binding StampText,Mode=TwoWay}" TextChanged="TextBox_TextChanged">
                             <i:Interaction.Triggers>
                                 <i:EventTrigger EventName="LostFocus">
                                     <i:InvokeCommandAction Command="{Binding UpDataDynamicCommnad}" PassEventArgsToCommand="True" />

+ 6 - 0
PDF Office/Views/PropertyPanel/AnnotPanel/CustomCreateDialog.xaml.cs

@@ -30,5 +30,11 @@ namespace PDF_Office.Views.PropertyPanel.AnnotPanel
         {
             (DataContext as CustomCreateDialogViewModel).SetStampStyle(Convert.ToInt32((sender as RadioButton).Tag));
         }
+
+        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
+        {
+            (DataContext as CustomCreateDialogViewModel).StampText = (e.OriginalSource as TextBox).Text;
+            (DataContext as CustomCreateDialogViewModel).UpDataStamp();
+        }
     }
 }

+ 21 - 2
PDF Office/Views/PropertyPanel/AnnotPanel/StampAnnotProperty.xaml

@@ -48,7 +48,16 @@
                 <ListBox ItemsSource="{Binding StandardStampList}" SelectionMode="Single" Width="256" Height="auto" VerticalAlignment="Top"  BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                     <ListBox.ItemTemplate>
                         <DataTemplate >
-                            <ListBoxItem>
+                            <ListBoxItem >
+                                <ListBoxItem.ContextMenu>
+                                    <ContextMenu>
+                                        <MenuItem Header="导出图章">
+                                            <MenuItem x:Name="SavePNG" Header="PNG" Tag="PNG" Click="Save_Click"/>
+                                            <MenuItem x:Name="SaveJPG" Header="JPG" Tag="JPG" Click="Save_Click"/>
+                                            <MenuItem x:Name="SavePDF" Header="PDF" Tag="PDF" Click="Save_Click"/>
+                                        </MenuItem>
+                                    </ContextMenu>
+                                </ListBoxItem.ContextMenu>
                             <Image Source="{Binding SourcePath}" Stretch="Uniform" Height="48"/>
                             </ListBoxItem>
                         </DataTemplate>
@@ -100,12 +109,22 @@
                              Width="256" Height="auto"
                              BorderThickness="0"
                              ScrollViewer.HorizontalScrollBarVisibility="Disabled"
+                             ItemsSource="{Binding CustomStampList}"
                              >
                     <ListBox.ItemTemplate>
                         <HierarchicalDataTemplate  DataType="{x:Type model:Stamp}" >
-                            <Image Source="{Binding ImageSource}"/>
+                            <Image Source="{Binding SourcePath}"/>
                         </HierarchicalDataTemplate>
                     </ListBox.ItemTemplate>
+
+                    <ListBox.ItemContainerStyle>
+                        <Style TargetType="ListBoxItem">
+                            <Setter Property="Height" Value="64"/>
+                            <Setter Property="Padding" Value="12 8 12 8"/>
+                            <Setter Property="HorizontalContentAlignment" Value="Center"/>
+                            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBoxItem_PreviewMouseLeftButtonDown"/>
+                        </Style>
+                    </ListBox.ItemContainerStyle>
                 </ListBox>
             </TabItem>
         </TabControl>

+ 6 - 0
PDF Office/Views/PropertyPanel/AnnotPanel/StampAnnotProperty.xaml.cs

@@ -36,5 +36,11 @@ namespace PDF_Office.Views.PropertyPanel.AnnotPanel
         {
             (DataContext as StampAnnotPropertyViewModel).UpDataDynamicStampList();
         }
+
+        private void Save_Click(object sender, RoutedEventArgs e)
+        {
+            MenuItem item = sender as MenuItem;
+            (DataContext as StampAnnotPropertyViewModel).SaveToPath(item.Tag.ToString(), item.DataContext as Stamp);
+        }
     }
 }

+ 1 - 0
PDFSettings/CustomStampList.cs

@@ -14,6 +14,7 @@ namespace PDFSettings
 
     public class StampAnnote
     {
+        public string Author;
         public string StampText;
         public string StampTextDate;
         public int ImageWidth;