ScanPanel.xaml.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 event ScanGrid.SelectAppliesTo? OnSelectAppliesTo;
  87. public ScanPanel()
  88. {
  89. InitializeComponent();
  90. }
  91. #region Panel
  92. public void Setup()
  93. {
  94. ScanGrid.HiddenColumns.Add(x => x.Document.ID);
  95. ScanGrid.Refresh(true, false);
  96. }
  97. public void Refresh()
  98. {
  99. ScanGrid.Refresh(false, true);
  100. }
  101. public void Shutdown()
  102. {
  103. }
  104. #endregion
  105. #region View List
  106. private static List<byte[]> RenderTextFile(string textData)
  107. {
  108. var pdfDocument = new PdfDocument();
  109. var page = pdfDocument.Pages.Add();
  110. var font = new PdfStandardFont(PdfFontFamily.Courier, 14);
  111. var textElement = new PdfTextElement(textData, font);
  112. var layoutFormat = new PdfLayoutFormat
  113. {
  114. Layout = PdfLayoutType.Paginate,
  115. Break = PdfLayoutBreakType.FitPage
  116. };
  117. textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), layoutFormat);
  118. using var docStream = new MemoryStream();
  119. pdfDocument.Save(docStream);
  120. var loadeddoc = new PdfLoadedDocument(docStream.ToArray());
  121. Bitmap[] bmpImages = loadeddoc.ExportAsImage(0, loadeddoc.Pages.Count - 1);
  122. var jpgEncoder = ImageUtils.GetEncoder(ImageFormat.Jpeg)!;
  123. var quality = Encoder.Quality;
  124. var encodeParams = new EncoderParameters(1);
  125. encodeParams.Param[0] = new EncoderParameter(quality, 100L);
  126. var images = new List<byte[]>();
  127. if (bmpImages != null)
  128. foreach (var image in bmpImages)
  129. {
  130. using var data = new MemoryStream();
  131. image.Save(data, jpgEncoder, encodeParams);
  132. images.Add(data.ToArray());
  133. }
  134. return images;
  135. }
  136. private void UpdateViewList()
  137. {
  138. var selected = ScanGrid.SelectedRows.Select(x => x.ToObject<Scan>()).ToList();
  139. if (selected.Count == SelectedScans.Count && !selected.Any(x => !SelectedScans.Any(y => x.ID == y.ID)))
  140. {
  141. return;
  142. }
  143. SelectedScans = selected;
  144. ViewListPanel.Children.Clear();
  145. var docIDs = SelectedScans.Select(x => x.Document.ID).Distinct().ToArray();
  146. var documents = new Client<Document>()
  147. .Query(
  148. new Filter<Document>(x => x.ID).InList(docIDs),
  149. new Columns<Document>(x => x.ID).Add(x => x.Data).Add(x => x.FileName))
  150. .ToObjects<Document>();
  151. var bitmaps = new Dictionary<Guid, List<BitmapImage>>();
  152. foreach (var document in documents)
  153. {
  154. List<byte[]> images;
  155. var extension = Path.GetExtension(document.FileName).ToLower();
  156. if (extension == ".pdf")
  157. {
  158. try
  159. {
  160. images = ImageUtils.RenderPDFToImages(document.Data);
  161. }
  162. catch (Exception e)
  163. {
  164. MessageBox.Show($"Cannot load document '{document.FileName}': {e.Message}");
  165. images = new List<byte[]>();
  166. }
  167. }
  168. else if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".bmp")
  169. {
  170. images = new List<byte[]> { document.Data };
  171. }
  172. else
  173. {
  174. images = RenderTextFile(Encoding.UTF8.GetString(document.Data));
  175. }
  176. foreach (var imageData in images)
  177. {
  178. try
  179. {
  180. if(!bitmaps.TryGetValue(document.ID, out var list))
  181. {
  182. list = new List<BitmapImage>();
  183. bitmaps[document.ID] = list;
  184. }
  185. list.Add(ImageUtils.LoadImage(imageData));
  186. }
  187. catch (Exception e)
  188. {
  189. MessageBox.Show($"Cannot load document '{document.FileName}': {e.Message}");
  190. }
  191. }
  192. }
  193. foreach(var scan in selected)
  194. {
  195. if(bitmaps.TryGetValue(scan.Document.ID, out var list))
  196. {
  197. foreach(var bitmap in list)
  198. {
  199. var image = new Image
  200. {
  201. Source = bitmap,
  202. Margin = new Thickness(0, 0, 0, 20)
  203. };
  204. ViewListPanel.Children.Add(image);
  205. }
  206. }
  207. }
  208. }
  209. #endregion
  210. #region Uploading
  211. private void DynamicTabItem_Drop(object sender, DragEventArgs e)
  212. {
  213. Task.Run(() =>
  214. {
  215. var docs = DocumentManipulationWindow.HandleFileDrop(e);
  216. if (docs is not null)
  217. {
  218. foreach(var (filename, doc) in docs)
  219. {
  220. ScanGrid.UploadDocument(Path.ChangeExtension(filename, ".pdf"), doc.SaveToBytes(), Guid.Empty);
  221. }
  222. /*Dispatcher.Invoke(() =>
  223. {
  224. ScanGrid.ShowDocumentWindow(pages, filename);
  225. });*/
  226. }
  227. });
  228. }
  229. private void DynamicTabItem_DragOver(object sender, DragEventArgs e)
  230. {
  231. if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent("FileGroupDescriptor"))
  232. {
  233. e.Effects = DragDropEffects.Copy;
  234. }
  235. else
  236. {
  237. e.Effects = DragDropEffects.None;
  238. }
  239. e.Handled = true;
  240. }
  241. #endregion
  242. private void ScanGrid_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  243. {
  244. UpdateViewList();
  245. }
  246. private void ScanGrid_OnSelectAppliesTo(string appliesTo)
  247. {
  248. OnSelectAppliesTo?.Invoke(appliesTo);
  249. }
  250. }
  251. }