Переглянути джерело

文件列表 - 加载加密文档的加锁图标

chenrongqian 2 роки тому
батько
коміт
5f8674ab51

+ 192 - 0
PDF Office/Helper/ToolMethod.cs

@@ -0,0 +1,192 @@
+using ComPDFKit.PDFDocument;
+using ComPDFKit.PDFPage;
+using PDF_Office.CustomControl;
+using PDFSettings.Settings;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Media.Imaging;
+
+namespace PDF_Office.Helper
+{
+    public static class ToolMethod
+    {
+        /// <summary>
+        /// 返回跟DPi 无关的图片
+        /// </summary>
+        /// <param name="document"></param>
+        /// <param name="width"></param>
+        /// <param name="height"></param>
+        /// <param name="pageIndex"></param>
+        /// <returns></returns>
+        public static async Task<Bitmap> RenderPageBitmap(CPDFDocument document,int width,int height,int pageIndex,bool annot,bool form)
+        {
+            if (width <= 0.0 || height<= 0.0)
+            {
+                return null;
+            }
+
+            Bitmap bitmap = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb);
+            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, (int)width, (int)height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
+            try
+            {
+                CPDFPage kMPDFPage = document.PageAtIndex(pageIndex);
+                await Task.Run(()=> kMPDFPage.RenderPageBitmap(0,0,width,height, uint.MaxValue, bitmapData.Scan0,annot?1:0,form));
+               // kMPDFPage.RenderPageBitmapWithMatrix((float)1, new System.Windows.Rect(), uint.MaxValue, bitmapData.Scan0, 1, form: true);
+            }
+            catch (Exception)
+            {
+                return null;
+            }
+
+            bitmap.UnlockBits(bitmapData);
+            return bitmap;
+        }
+
+        public static Bitmap RenderPageBitmapNoWait(CPDFDocument document, int width, int height, int pageIndex,bool annote,bool form,uint custombackground=0xFFFFFFFF)
+        {
+            if (width <= 0.0 || height <= 0.0)
+            {
+                return null;
+            }
+
+            Bitmap bitmap = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb);
+            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, (int)width, (int)height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
+            try
+            {
+                CPDFPage kMPDFPage = document.PageAtIndex(pageIndex);
+                //1-显示注释 0-不显示注释  form-true 显示表单   form-false  不显示表单
+                if (custombackground != 0xFFFFFFFF)
+                {
+                    kMPDFPage.RenderPageBitmap(0, 0, width, height, custombackground, bitmapData.Scan0, annote ? 1 : 0, form);
+                }
+                else
+                {
+                    kMPDFPage.RenderPageBitmap(0, 0, width, height, uint.MaxValue, bitmapData.Scan0, annote ? 1 : 0, form);
+                }
+                //kMPDFPage.RenderPageBitmapWithMatrix((float)1, new System.Windows.Rect(), uint.MaxValue, bitmapData.Scan0, 1, form: true);
+            }
+            catch (Exception)
+            {
+                return null;
+            }
+
+            bitmap.UnlockBits(bitmapData);
+            return bitmap;
+        }
+
+
+        public static void SetFileThumbImg(string fileName)
+        {
+
+            bool isNewDocument = false;
+            //检查是否是新文档
+            OpenFileInfo isnew = SettingHelper.GetFileInfo(fileName);
+            if (isnew == null)
+                isNewDocument = true;
+
+            //检查是否是加密文档  要在open前检查
+            bool isLockedfile = false;
+            CPDFDocument tempdoc = CPDFDocument.InitWithFilePath(fileName);
+            if (tempdoc != null && (bool)(tempdoc.IsLocked))
+                isLockedfile = true;
+
+            OpenFileInfo fileInfo = SettingHelper.GetFileInfo(fileName);
+
+
+            if (fileInfo != null)
+            {
+                try
+                {
+                    if (string.IsNullOrWhiteSpace(fileInfo.ThumbImgPath) || isLockedfile)//未保存文档首页缩略图时,提取第一页当缩略图
+                    {
+                        if (isLockedfile)
+                        {
+                            fileInfo.ThumbImgPath = "pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png";
+                        }
+                        else
+                        {
+                            if (tempdoc.PageCount > 0)
+                            {
+                                var size = tempdoc.GetPageSize(0);
+                                System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(tempdoc, (int)size.Width, (int)size.Height, 0, true, true);
+                                string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
+                                //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除文件
+                                //保险措施(猜测)
+                                if (File.Exists(folderPath))
+                                    File.Delete(folderPath);
+                                DirectoryInfo folder = new DirectoryInfo(folderPath);
+                                if (!folder.Exists)
+                                    folder.Create();
+                                string imageName = Guid.NewGuid().ToString();
+                                string imagePath = System.IO.Path.Combine(folderPath, imageName);
+                                using (FileStream stream = new FileStream(imagePath, FileMode.Create))
+                                {
+                                    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
+                                }
+                                fileInfo.ThumbImgPath = imagePath;
+                            }
+                        }
+
+
+                    }
+                    else//解锁文件覆盖了加密文件的情况 或者本地缩略图文件被删除
+                    {
+                        if (tempdoc.PageCount > 0 && (!File.Exists(fileInfo.ThumbImgPath) || (!tempdoc.IsEncrypted && fileInfo.ThumbImgPath.Equals("pack://application:,,,/Resources/Image/Home/FilesType/ic_propertybar_file_pdf_lock.png"))))
+                        {
+                            var size = tempdoc.GetPageSize(0);
+                            System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(tempdoc, (int)size.Width, (int)size.Height, 0, true, true);
+                            string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
+                            if (File.Exists(folderPath))
+                                File.Delete(folderPath);
+                            DirectoryInfo folder = new DirectoryInfo(folderPath);
+                            if (!folder.Exists)
+                                folder.Create();
+                            string imageName = Guid.NewGuid().ToString();
+                            string imagePath = System.IO.Path.Combine(folderPath, imageName);
+                            using (FileStream stream = new FileStream(imagePath, FileMode.Create))
+                            {
+                                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
+                            }
+                            fileInfo.ThumbImgPath = imagePath;
+                        }
+                    }
+                }
+                catch
+                {
+                    fileInfo.ThumbImgPath = null;
+                    MessageBoxEx.Show("LoadFile_GetThumbnailFailedWarning");
+                }
+            }
+
+        }
+
+
+        public static BitmapImage GetFileThumbImg(string path)
+        {
+            BitmapImage bitmap = new BitmapImage();
+            if (File.Exists(path))
+            {
+                bitmap.BeginInit();
+                bitmap.CacheOption = BitmapCacheOption.OnLoad;
+                using (Stream ms = new MemoryStream(File.ReadAllBytes(path)))
+                {
+                    bitmap.StreamSource = ms;
+                    bitmap.EndInit();
+                    bitmap.Freeze();
+                }
+            }
+            else if (path.Equals("pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png"))
+                bitmap = new BitmapImage(new Uri("pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png"));
+            else//最近文档 但是删除了缩略图的情况
+                bitmap = new BitmapImage(new Uri("pack://application:,,,/Resources/FilesType/ic_propertybar_file_png_Large.png"));
+            return bitmap;
+        }
+
+    }
+}

+ 2 - 0
PDF Office/PDF Office.csproj

@@ -82,6 +82,7 @@
     <Reference Include="System" />
     <Reference Include="System.Configuration" />
     <Reference Include="System.Data" />
+    <Reference Include="System.Drawing" />
     <Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
       <HintPath>packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
     </Reference>
@@ -120,6 +121,7 @@
     <Compile Include="EventAggregators\PageEditNotifyEvent.cs" />
     <Compile Include="EventAggregators\PageEditRefreshEvent.cs" />
     <Compile Include="Helper\SettingHelper.cs" />
+    <Compile Include="Helper\ToolMethod.cs" />
     <Compile Include="Model\DialogNames.cs" />
     <Compile Include="CustomControl\SystemControl\InterTabClient.cs" />
     <Compile Include="CustomControl\LoadingControl.xaml.cs">

+ 3 - 0
PDF Office/ViewModels/HomeContentViewModel.cs

@@ -1,5 +1,6 @@
 using Microsoft.Win32;
 using PDF_Office.EventAggregators;
+using PDF_Office.Helper;
 using PDF_Office.Model;
 using Prism.Commands;
 using Prism.Events;
@@ -102,6 +103,7 @@ namespace PDF_Office.ViewModels
                     {
                         mainContentViewModel.OpenFile(openFileDialog.FileName);
                     }
+                    ToolMethod.SetFileThumbImg(openFileDialog.FileName);
                 }
                 else
                 {
@@ -116,6 +118,7 @@ namespace PDF_Office.ViewModels
                         {
                             App.mainWindowViewModel.AddTabItem(fileList[i]);
                         }
+                        ToolMethod.SetFileThumbImg(fileList[i]);
                     }
                 }
                 IsLoading = Visibility.Collapsed;

+ 22 - 2
PDF Office/Views/HomePanel/RecentFiles/DocItemControl.xaml.cs

@@ -13,6 +13,9 @@ using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
 using System.IO;
+using PDFSettings.Settings;
+using PDF_Office.Helper;
+using ComPDFKit.PDFDocument;
 
 namespace PDF_Office.Views.HomePanel.RecentFiles
 {
@@ -28,9 +31,26 @@ namespace PDF_Office.Views.HomePanel.RecentFiles
 
         private void Grid_Loaded(object sender, RoutedEventArgs e)
         {
-           
+            var grid = sender as Grid;
+            if (grid != null)
+            {
+                var data = grid.DataContext as OpenFileInfo;
+                if (data == null) return;
+                if (!string.IsNullOrEmpty(data.ThumbImgPath))
+                    CoverImage.Source = ToolMethod.GetFileThumbImg(data.ThumbImgPath);
+                else
+                {
+                    CPDFDocument tempdoc = CPDFDocument.InitWithFilePath(data.FilePath);
+                    if (tempdoc != null && (bool)(tempdoc.IsLocked))
+                    {
+                        CoverImage.Source = ToolMethod.GetFileThumbImg("pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png");
+                    }
+                    else
+                    CoverImage.Source = ToolMethod.GetFileThumbImg("pack://application:,,,/Resources//FilesType/ic_propertybar_file_png_Large.png");
+                }
+                  
+            }
         }
 
-       
     }
 }

+ 3 - 3
PDF Office/Views/HomePanel/RecentFiles/DocItemListViewControl.xaml

@@ -24,8 +24,8 @@
             <convert:FilePathToSizeConvert x:Key="FilePathToSizeConvert"/>
         </ResourceDictionary>-->
     </UserControl.Resources>
-    
-    <Grid HorizontalAlignment="Stretch">
+
+    <Grid HorizontalAlignment="Stretch"  Loaded="Grid_Loaded" DataContext="{Binding}" ToolTip="{Binding FilePath}">
         <Grid.Background>
             <SolidColorBrush Color="White" Opacity="0.01"/>
         </Grid.Background>
@@ -42,7 +42,7 @@
                     <ColumnDefinition />
                 </Grid.ColumnDefinitions>
                 <!--Source="{Binding FileName,Converter={StaticResource FileFormatToIconConvert}}"-->
-                <Image HorizontalAlignment="Left" Width="32" Height="32"/>
+                <Image x:Name="CoverImage" HorizontalAlignment="Left" Width="32" Height="32" RenderOptions.BitmapScalingMode="HighQuality" UseLayoutRounding="True"/>
                 <StackPanel Grid.Column="1" Margin="8,0,0,0" VerticalAlignment="Center">
                     <TextBlock x:Name="FileName" Grid.Column="0" VerticalAlignment="Center" Text="{Binding FileName}" FontSize="16" Foreground="Black" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis"/>
                     <TextBlock x:Name="FilePath" Margin="0,4,0,0" Text="{Binding FilePath}" FontSize="12" Foreground="#FF666666" TextTrimming="WordEllipsis"/>

+ 28 - 2
PDF Office/Views/HomePanel/RecentFiles/DocItemListViewControl.xaml.cs

@@ -14,7 +14,10 @@ using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
+using ComPDFKit.PDFDocument;
 using ComPDFKitViewer.PdfViewer;
+using PDF_Office.Helper;
+using PDFSettings.Settings;
 
 namespace PDF_Office.Views.HomePanel.RecentFiles
 {
@@ -37,7 +40,7 @@ namespace PDF_Office.Views.HomePanel.RecentFiles
 
         private void SetLangText()
         {
-            
+
         }
 
         #region UI
@@ -108,8 +111,31 @@ namespace PDF_Office.Views.HomePanel.RecentFiles
 
         private void MenuMore_Loaded(object sender, RoutedEventArgs e)
         {
-      
+
 
         }
+
+        private void Grid_Loaded(object sender, RoutedEventArgs e)
+        {
+            var grid = sender as Grid;
+            if (grid != null)
+            {
+                var data = grid.DataContext as OpenFileInfo;
+                if (data == null) return;
+
+
+                if (File.Exists(data.ThumbImgPath))
+                {
+                    CoverImage.Source = ToolMethod.GetFileThumbImg(data.ThumbImgPath);
+                }
+                else if (data.ThumbImgPath.Equals("pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png"))
+                    CoverImage.Source = ToolMethod.GetFileThumbImg("pack://application:,,,/Resources/FilesType/ic_propertybar_file_pdf_lock.png");
+                else//最近文档 但是删除了缩略图的情况
+                    CoverImage.Source = ToolMethod.GetFileThumbImg("pack://application:,,,/Resources/FilesType/ic_propertybar_file_png_Large.png");
+            }
+
+        }
+
+     
     }
 }

+ 8 - 2
PDF Office/Views/MainWindow.xaml.cs

@@ -20,6 +20,9 @@ using PDF_Office.CustomControl;
 using Prism.Ioc;
 using PDF_Office.Helper;
 using System.IO;
+using ComPDFKit.PDFDocument;
+using PDFSettings.Settings;
+using PDF_Office.Properties;
 
 namespace PDF_Office.Views
 {
@@ -77,6 +80,7 @@ namespace PDF_Office.Views
             aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Closed });
         }
 
+        private bool isNewDocument = false;
         public void LoadPdfViewer(string[] filePaths)
         {
             var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
@@ -88,10 +92,9 @@ namespace PDF_Office.Views
                 }
                 else
                 {
-                   
-
                     content.OpenFile(filePaths[0]);
                 }
+                ToolMethod.SetFileThumbImg(filePaths[0]);
             }
             else
             {
@@ -106,9 +109,12 @@ namespace PDF_Office.Views
                     {
                         App.mainWindowViewModel.AddTabItem(fileList[i]);
                     }
+                    ToolMethod.SetFileThumbImg(fileList[i]);
                 }
             }
 
+            Settings.Default.Save();
         }
+
     }
 }