ScanPanel.xaml.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using Syncfusion.Pdf.Graphics;
  5. using Syncfusion.Pdf.Parsing;
  6. using Syncfusion.Pdf;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing.Imaging;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. using InABox.DynamicGrid;
  25. using InABox.WPF;
  26. using Encoder = System.Drawing.Imaging.Encoder;
  27. using Path = System.IO.Path;
  28. using Image = System.Windows.Controls.Image;
  29. using com.sun.tools.doclets.@internal.toolkit.util;
  30. using Syncfusion.Windows.PdfViewer;
  31. using javax.xml.crypto;
  32. namespace PRSDesktop
  33. {
  34. public static class PDFExtensions
  35. {
  36. public static IEnumerable<PdfPageBase> GetPages(this PdfDocumentBase doc)
  37. {
  38. if (doc is PdfLoadedDocument lDoc)
  39. return lDoc.Pages.Cast<PdfPageBase>();
  40. if (doc is PdfDocument pdfDoc)
  41. return pdfDoc.Pages.Cast<PdfPageBase>();
  42. throw new Exception($"Unsupported PDF Document type {doc.GetType()}");
  43. }
  44. public static PdfPageBase GetPage(this PdfDocumentBase doc, int index)
  45. {
  46. if (doc is PdfLoadedDocument lDoc)
  47. return lDoc.Pages[index];
  48. if (doc is PdfDocument pdfDoc)
  49. return pdfDoc.Pages[index];
  50. throw new Exception($"Unsupported PDF Document type {doc.GetType()}");
  51. }
  52. public static int PageCount(this PdfDocumentBase doc)
  53. {
  54. if (doc is PdfLoadedDocument lDoc)
  55. return lDoc.Pages.Count;
  56. if (doc is PdfDocument pdfDoc)
  57. return pdfDoc.Pages.Count;
  58. throw new Exception($"Unsupported PDF Document type {doc.GetType()}");
  59. }
  60. public static PdfLoadedDocument AsLoadedDocument(this PdfDocumentBase doc)
  61. {
  62. if (doc is PdfLoadedDocument lDoc)
  63. return lDoc;
  64. if (doc is PdfDocument pdfDoc)
  65. {
  66. using var ms = new MemoryStream();
  67. pdfDoc.Save(ms);
  68. var array = ms.ToArray();
  69. return new PdfLoadedDocument(array);
  70. }
  71. throw new Exception($"Unsupported PDF Document type {doc.GetType()}");
  72. }
  73. public static byte[] SaveToBytes(this PdfDocumentBase doc)
  74. {
  75. using var ms = new MemoryStream();
  76. doc.Save(ms);
  77. return ms.ToArray();
  78. }
  79. }
  80. /// <summary>
  81. /// Interaction logic for ScanPanel.xaml
  82. /// </summary>
  83. public partial class ScanPanel : UserControl, ICorePanel, IDockPanel
  84. {
  85. private List<Scan> SelectedScans = new();
  86. public delegate void SelectScans(Scan[]? scans);
  87. public event SelectScans? OnSelectScans;
  88. public ScanPanel()
  89. {
  90. InitializeComponent();
  91. }
  92. #region Panel
  93. public void Setup()
  94. {
  95. ScanGrid.HiddenColumns.Add(x => x.Document.ID);
  96. ScanGrid.Refresh(true, false);
  97. }
  98. public void Refresh()
  99. {
  100. ScanGrid.Refresh(false, true);
  101. }
  102. public void Shutdown()
  103. {
  104. }
  105. #endregion
  106. #region View List
  107. private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  108. {
  109. if (sender != TabControl) return;
  110. if (TabControl.SelectedItem == ViewList)
  111. {
  112. UpdateViewList();
  113. }
  114. }
  115. private static List<byte[]> RenderTextFile(string textData)
  116. {
  117. var pdfDocument = new PdfDocument();
  118. var page = pdfDocument.Pages.Add();
  119. var font = new PdfStandardFont(PdfFontFamily.Courier, 14);
  120. var textElement = new PdfTextElement(textData, font);
  121. var layoutFormat = new PdfLayoutFormat
  122. {
  123. Layout = PdfLayoutType.Paginate,
  124. Break = PdfLayoutBreakType.FitPage
  125. };
  126. textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), layoutFormat);
  127. using var docStream = new MemoryStream();
  128. pdfDocument.Save(docStream);
  129. var loadeddoc = new PdfLoadedDocument(docStream.ToArray());
  130. Bitmap[] bmpImages = loadeddoc.ExportAsImage(0, loadeddoc.Pages.Count - 1);
  131. var jpgEncoder = ImageUtils.GetEncoder(ImageFormat.Jpeg)!;
  132. var quality = Encoder.Quality;
  133. var encodeParams = new EncoderParameters(1);
  134. encodeParams.Param[0] = new EncoderParameter(quality, 100L);
  135. var images = new List<byte[]>();
  136. if (bmpImages != null)
  137. foreach (var image in bmpImages)
  138. {
  139. using var data = new MemoryStream();
  140. image.Save(data, jpgEncoder, encodeParams);
  141. images.Add(data.ToArray());
  142. }
  143. return images;
  144. }
  145. private void UpdateViewList()
  146. {
  147. var selected = ScanGrid.SelectedRows.Select(x => x.ToObject<Scan>()).ToList();
  148. if (selected.Count == SelectedScans.Count && !selected.Any(x => !SelectedScans.Any(y => x.ID == y.ID)))
  149. {
  150. return;
  151. }
  152. SelectedScans = selected;
  153. ViewListPanel.Children.Clear();
  154. var docIDs = SelectedScans.Select(x => x.Document.ID).Distinct().ToArray();
  155. var documents = new Client<Document>()
  156. .Query(
  157. new Filter<Document>(x => x.ID).InList(docIDs),
  158. new Columns<Document>(x => x.ID).Add(x => x.Data).Add(x => x.FileName))
  159. .ToObjects<Document>();
  160. var bitmaps = new Dictionary<Guid, List<BitmapImage>>();
  161. foreach (var document in documents)
  162. {
  163. List<byte[]> images;
  164. var extension = Path.GetExtension(document.FileName).ToLower();
  165. if (extension == ".pdf")
  166. {
  167. try
  168. {
  169. images = ImageUtils.RenderPDFToImages(document.Data);
  170. }
  171. catch (Exception e)
  172. {
  173. MessageBox.Show($"Cannot load document '{document.FileName}': {e.Message}");
  174. images = new List<byte[]>();
  175. }
  176. }
  177. else if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".bmp")
  178. {
  179. images = new List<byte[]> { document.Data };
  180. }
  181. else
  182. {
  183. images = RenderTextFile(Encoding.UTF8.GetString(document.Data));
  184. }
  185. foreach (var imageData in images)
  186. {
  187. try
  188. {
  189. if(!bitmaps.TryGetValue(document.ID, out var list))
  190. {
  191. list = new List<BitmapImage>();
  192. bitmaps[document.ID] = list;
  193. }
  194. list.Add(ImageUtils.LoadImage(imageData));
  195. }
  196. catch (Exception e)
  197. {
  198. MessageBox.Show($"Cannot load document '{document.FileName}': {e.Message}");
  199. }
  200. }
  201. }
  202. foreach(var scan in selected)
  203. {
  204. if(bitmaps.TryGetValue(scan.Document.ID, out var list))
  205. {
  206. foreach(var bitmap in list)
  207. {
  208. var image = new Image
  209. {
  210. Source = bitmap,
  211. Margin = new Thickness(0, 0, 0, 20)
  212. };
  213. ViewListPanel.Children.Add(image);
  214. }
  215. }
  216. }
  217. }
  218. #endregion
  219. #region Uploading
  220. private void DynamicTabItem_Drop(object sender, DragEventArgs e)
  221. {
  222. Task.Run(() =>
  223. {
  224. var docs = DocumentManipulationWindow.HandleFileDrop(e);
  225. if (docs is not null)
  226. {
  227. foreach(var (filename, doc) in docs)
  228. {
  229. ScanGrid.UploadDocument(Path.ChangeExtension(filename, ".pdf"), doc.SaveToBytes(), Guid.Empty);
  230. }
  231. /*Dispatcher.Invoke(() =>
  232. {
  233. ScanGrid.ShowDocumentWindow(pages, filename);
  234. });*/
  235. }
  236. });
  237. }
  238. private void DynamicTabItem_DragOver(object sender, DragEventArgs e)
  239. {
  240. if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent("FileGroupDescriptor"))
  241. {
  242. e.Effects = DragDropEffects.Copy;
  243. }
  244. else
  245. {
  246. e.Effects = DragDropEffects.None;
  247. }
  248. e.Handled = true;
  249. }
  250. #endregion
  251. private void ScanGrid_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  252. {
  253. OnSelectScans?.Invoke(e.Rows?.Select(x => x.ToObject<Scan>()).ToArray());
  254. }
  255. }
  256. }