123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using Syncfusion.Pdf.Graphics;
- using Syncfusion.Pdf.Parsing;
- using Syncfusion.Pdf;
- using System;
- using System.Collections.Generic;
- using System.Drawing.Imaging;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using Encoder = System.Drawing.Imaging.Encoder;
- using Path = System.IO.Path;
- using Image = System.Windows.Controls.Image;
- using com.sun.tools.doclets.@internal.toolkit.util;
- using Syncfusion.Windows.PdfViewer;
- using javax.xml.crypto;
- namespace PRSDesktop
- {
- public static class PDFExtensions
- {
- public static IEnumerable<PdfPageBase> GetPages(this PdfDocumentBase doc)
- {
- if (doc is PdfLoadedDocument lDoc)
- return lDoc.Pages.Cast<PdfPageBase>();
- if (doc is PdfDocument pdfDoc)
- return pdfDoc.Pages.Cast<PdfPageBase>();
- throw new Exception($"Unsupported PDF Document type {doc.GetType()}");
- }
- public static PdfPageBase GetPage(this PdfDocumentBase doc, int index)
- {
- if (doc is PdfLoadedDocument lDoc)
- return lDoc.Pages[index];
- if (doc is PdfDocument pdfDoc)
- return pdfDoc.Pages[index];
- throw new Exception($"Unsupported PDF Document type {doc.GetType()}");
- }
- public static int PageCount(this PdfDocumentBase doc)
- {
- if (doc is PdfLoadedDocument lDoc)
- return lDoc.Pages.Count;
- if (doc is PdfDocument pdfDoc)
- return pdfDoc.Pages.Count;
- throw new Exception($"Unsupported PDF Document type {doc.GetType()}");
- }
- public static PdfLoadedDocument AsLoadedDocument(this PdfDocumentBase doc)
- {
- if (doc is PdfLoadedDocument lDoc)
- return lDoc;
- if (doc is PdfDocument pdfDoc)
- {
- using var ms = new MemoryStream();
- pdfDoc.Save(ms);
- var array = ms.ToArray();
- return new PdfLoadedDocument(array);
- }
- throw new Exception($"Unsupported PDF Document type {doc.GetType()}");
- }
- public static byte[] SaveToBytes(this PdfDocumentBase doc)
- {
- using var ms = new MemoryStream();
- doc.Save(ms);
- return ms.ToArray();
- }
- }
- /// <summary>
- /// Interaction logic for ScanPanel.xaml
- /// </summary>
- public partial class ScanPanel : UserControl, ICorePanel, IDockPanel
- {
- private List<Scan> SelectedScans = new();
- public event ScanGrid.SelectAppliesTo? OnSelectAppliesTo;
- public ScanPanel()
- {
- InitializeComponent();
- }
- #region Panel
- public void Setup()
- {
- ScanGrid.HiddenColumns.Add(x => x.Document.ID);
- ScanGrid.Refresh(true, false);
- }
- public void Refresh()
- {
- ScanGrid.Refresh(false, true);
- }
- public void Shutdown()
- {
- }
- #endregion
- #region View List
- private static List<byte[]> RenderTextFile(string textData)
- {
- var pdfDocument = new PdfDocument();
- var page = pdfDocument.Pages.Add();
- var font = new PdfStandardFont(PdfFontFamily.Courier, 14);
- var textElement = new PdfTextElement(textData, font);
- var layoutFormat = new PdfLayoutFormat
- {
- Layout = PdfLayoutType.Paginate,
- Break = PdfLayoutBreakType.FitPage
- };
- textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), layoutFormat);
- using var docStream = new MemoryStream();
- pdfDocument.Save(docStream);
- var loadeddoc = new PdfLoadedDocument(docStream.ToArray());
- Bitmap[] bmpImages = loadeddoc.ExportAsImage(0, loadeddoc.Pages.Count - 1);
- var jpgEncoder = ImageUtils.GetEncoder(ImageFormat.Jpeg)!;
- var quality = Encoder.Quality;
- var encodeParams = new EncoderParameters(1);
- encodeParams.Param[0] = new EncoderParameter(quality, 100L);
- var images = new List<byte[]>();
- if (bmpImages != null)
- foreach (var image in bmpImages)
- {
- using var data = new MemoryStream();
- image.Save(data, jpgEncoder, encodeParams);
- images.Add(data.ToArray());
- }
- return images;
- }
- private void UpdateViewList()
- {
- var selected = ScanGrid.SelectedRows.Select(x => x.ToObject<Scan>()).ToList();
- if (selected.Count == SelectedScans.Count && !selected.Any(x => !SelectedScans.Any(y => x.ID == y.ID)))
- {
- return;
- }
- SelectedScans = selected;
- ViewListPanel.Children.Clear();
- var docIDs = SelectedScans.Select(x => x.Document.ID).Distinct().ToArray();
- var documents = new Client<Document>()
- .Query(
- new Filter<Document>(x => x.ID).InList(docIDs),
- new Columns<Document>(x => x.ID).Add(x => x.Data).Add(x => x.FileName))
- .ToObjects<Document>();
- var bitmaps = new Dictionary<Guid, List<BitmapImage>>();
- foreach (var document in documents)
- {
- List<byte[]> images;
- var extension = Path.GetExtension(document.FileName).ToLower();
- if (extension == ".pdf")
- {
- try
- {
- images = ImageUtils.RenderPDFToImages(document.Data);
- }
- catch (Exception e)
- {
- MessageBox.Show($"Cannot load document '{document.FileName}': {e.Message}");
- images = new List<byte[]>();
- }
- }
- else if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".bmp")
- {
- images = new List<byte[]> { document.Data };
- }
- else
- {
- images = RenderTextFile(Encoding.UTF8.GetString(document.Data));
- }
- foreach (var imageData in images)
- {
- try
- {
- if(!bitmaps.TryGetValue(document.ID, out var list))
- {
- list = new List<BitmapImage>();
- bitmaps[document.ID] = list;
- }
- list.Add(ImageUtils.LoadImage(imageData));
- }
- catch (Exception e)
- {
- MessageBox.Show($"Cannot load document '{document.FileName}': {e.Message}");
- }
- }
- }
- foreach(var scan in selected)
- {
- if(bitmaps.TryGetValue(scan.Document.ID, out var list))
- {
- foreach(var bitmap in list)
- {
- var image = new Image
- {
- Source = bitmap,
- Margin = new Thickness(0, 0, 0, 20)
- };
- ViewListPanel.Children.Add(image);
- }
- }
- }
- }
- #endregion
- #region Uploading
- private void DynamicTabItem_Drop(object sender, DragEventArgs e)
- {
- Task.Run(() =>
- {
- var docs = DocumentManipulationWindow.HandleFileDrop(e);
- if (docs is not null)
- {
- foreach(var (filename, doc) in docs)
- {
- ScanGrid.UploadDocument(Path.ChangeExtension(filename, ".pdf"), doc.SaveToBytes(), Guid.Empty);
- }
- /*Dispatcher.Invoke(() =>
- {
- ScanGrid.ShowDocumentWindow(pages, filename);
- });*/
- }
- });
- }
- private void DynamicTabItem_DragOver(object sender, DragEventArgs e)
- {
- if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent("FileGroupDescriptor"))
- {
- e.Effects = DragDropEffects.Copy;
- }
- else
- {
- e.Effects = DragDropEffects.None;
- }
- e.Handled = true;
- }
- #endregion
- private void ScanGrid_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
- {
- UpdateViewList();
- }
- private void ScanGrid_OnSelectAppliesTo(string appliesTo)
- {
- OnSelectAppliesTo?.Invoke(appliesTo);
- }
- }
- }
|