using Comal.Stores; using InABox.Core; using System; using System.Linq; using System.IO; using Syncfusion.Pdf.Parsing; using System.Drawing; using System.Drawing.Imaging; namespace PRSStores { //In order to have BeforeSave active for a specific EntityDocument, it has to be concretized from this abstract class public abstract class EntityDocumentStore : BaseStore where TEntityDocument : EntityDocument, new() where TEntityLink : EntityLink, new() where TEntity : Entity, new() { protected override void BeforeSave(TEntityDocument entity) { SaveThumbnail(entity); //current size for saved thumbnails is 256*256 base.BeforeSave(entity); } private void SaveThumbnail(TEntityDocument entity) { if (entity.DocumentLink.ID == Guid.Empty) return; if (entity.Thumbnail?.Any() == true) return; CoreTable table = Provider.Query(new Filter(x => x.ID).IsEqualTo(entity.DocumentLink.ID), new Columns(x => x.Data, x => x.FileName)); if (table.Rows.Count == 0) return; Document doc = table.Rows.FirstOrDefault().ToObject(); if (!doc.FileName.ToLower().EndsWith("pdf")) return; try { PdfLoadedDocument loadeddoc = new PdfLoadedDocument(doc.Data); Bitmap image = loadeddoc.ExportAsImage(0, new SizeF(256, 256), true); MemoryStream stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); entity.Thumbnail = stream.ToArray(); } catch (Exception ex) { Logger.Send(LogType.Information, UserID, ex.Message); } } } }