فهرست منبع

DYNAMICGRID - added save document actioncolumn to DynamicDocumentGrid

Nick-PRSDigital@bitbucket.org 2 سال پیش
والد
کامیت
9779f3dbff
1فایلهای تغییر یافته به همراه75 افزوده شده و 3 حذف شده
  1. 75 3
      InABox.DynamicGrid/DynamicDocumentGrid.cs

+ 75 - 3
InABox.DynamicGrid/DynamicDocumentGrid.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Data;
 using System.Diagnostics;
+using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Windows;
@@ -10,12 +11,15 @@ using InABox.Clients;
 using InABox.Core;
 using InABox.WPF;
 using Microsoft.Win32;
+using Syncfusion.Pdf.Interactive;
+using Syncfusion.Pdf.Parsing;
+using Syncfusion.Pdf;
 
 namespace InABox.DynamicGrid
 {
 
     public delegate String OnGetWatermark(CoreRow row);
-    
+
     public class DynamicDocumentGrid<TDocument, TEntity, TEntityLink> : DynamicManyToManyGrid<TDocument, TEntity>
         where TEntity : Entity, IPersistent, IRemotable, new()
         where TDocument : Entity, IEntityDocument<TEntityLink>, IPersistent, IRemotable, new() // Entity, IPersistent, IRemotable, IManyToMany<TEntity, Document>, new()
@@ -44,10 +48,78 @@ namespace InABox.DynamicGrid
             HiddenColumns.Add(x => x.Superceded);
             HiddenColumns.Add(x => x.DocumentLink.FileName);
             ActionColumns.Add(new DynamicImageColumn(DocumentImage, ViewDocument) { Position = DynamicActionColumnPosition.Start });
+            ActionColumns.Add(new DynamicImageColumn(DiskImage, SaveDocument) { Position = DynamicActionColumnPosition.Start });
             supercedecolumn = new DynamicImageColumn(SupercededImage, SupercedeDocument);
             ActionColumns.Add(supercedecolumn);
         }
 
+        private bool SaveDocument(CoreRow? row)
+        {
+            var filename = row.Get<TDocument, string>(x => x.DocumentLink.FileName);
+            if (Path.GetExtension(filename).ToUpper().Equals(".PDF"))
+            {
+                var dlg = new SaveFileDialog();
+                dlg.Filter = "PDF Files (*.pdf)|*.pdf";
+                dlg.FileName = Path.ChangeExtension(filename, ".pdf");
+                if (dlg.ShowDialog() == true)
+                {
+                    var imageid = row.Get<TDocument, Guid>(x => x.DocumentLink.ID);
+                    var data = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(imageid)).Rows.FirstOrDefault().Get<Document, byte[]>(x => x.Data);
+                    var name = dlg.FileName;
+                    File.WriteAllBytes(name, data);
+
+                    var gsProcessInfo = new ProcessStartInfo();
+                    gsProcessInfo.Verb = "open";
+                    gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
+                    gsProcessInfo.FileName = name;
+                    gsProcessInfo.UseShellExecute = true;
+
+                    Process.Start(gsProcessInfo);
+                }
+
+            }
+            else if (Path.GetExtension(filename).ToUpper().Equals(".PNG") || Path.GetExtension(filename).ToUpper().Equals(".JPG") || Path.GetExtension(filename).ToUpper().Equals(".GIF"))
+            {
+                var imageid = row.Get<TDocument, Guid>(x => x.DocumentLink.ID);
+                if (imageid == Guid.Empty)
+                    return false;
+
+                var dlg = new SaveFileDialog();
+                dlg.Filter = "Image Files (*.png)|*.png";
+                dlg.FileName = filename;
+                if (dlg.ShowDialog() == true)
+                {
+                    var bmp = LoadBitmapFromDatabase(imageid);
+                    bmp?.Save(dlg.FileName);
+                }
+            }
+
+            return false;
+        }
+
+        private Bitmap LoadBitmapFromDatabase(Guid imageid)
+        {
+            if (imageid == Guid.Empty)
+                return null;
+            Bitmap result = null;
+            var image = new Client<Document>().Query(
+                new Filter<Document>(x => x.ID).IsEqualTo(imageid),
+                new Columns<Document>(x => x.ID, x => x.Data)
+            ).Rows.FirstOrDefault();
+            if (image != null)
+            {
+                var ms = new MemoryStream(image.Get<Document, byte[]>(x => x.Data));
+                result = new Bitmap(ms);
+            }
+
+            return result;
+        }
+
+        private BitmapImage? DiskImage(CoreRow? arg)
+        {
+            return Properties.Resources.disk.AsBitmapImage();
+        }
+
         public override int Order()
         {
             return int.MaxValue;
@@ -138,14 +210,14 @@ namespace InABox.DynamicGrid
 
         protected override void OnDragEnd(Type entity, CoreTable table)
         {
-            if(entity == typeof(Document))
+            if (entity == typeof(Document))
             {
                 var refresh = false;
 
                 var docIDS = table.Rows.Select(x => x.Get<Document, Guid>(x => x.ID)).ToArray();
 
                 var columns = new Columns<Document>(x => x.ID);
-                foreach(var column in VisibleColumns)
+                foreach (var column in VisibleColumns)
                 {
                     if (column.ColumnName.StartsWith("DocumentLink."))
                     {