LinkAnnotPropertyViewModel.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.Model;
  6. using PDF_Office.ViewModels.Tools;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Media;
  16. using System.Windows;
  17. using System.Windows.Media.Imaging;
  18. using Prism.Commands;
  19. using PDF_Office.Helper;
  20. using Microsoft.Office.Interop.Word;
  21. using static Dropbox.Api.Files.FileCategory;
  22. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  23. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  24. {
  25. internal class LinkAnnotPropertyViewModel : BindableBase, INavigationAware
  26. {
  27. public AnnotAttribEvent AnnotAttribEvent { get; set; }
  28. private AnnotHandlerEventArgs Annot;
  29. private AnnotPropertyPanel PropertyPanel;
  30. private AnnotArgsType annotType;
  31. private LinkAnnotArgs _annotArgs;
  32. public LinkAnnotArgs AnnotArgs
  33. {
  34. get
  35. {
  36. return _annotArgs;
  37. }
  38. set
  39. {
  40. if (_annotArgs != null)
  41. {
  42. _annotArgs.LinkDrawFinished -= _annotArgs_LinkDrawFinished;
  43. }
  44. _annotArgs = value;
  45. if (_annotArgs != null)
  46. {
  47. _annotArgs.LinkDrawFinished += _annotArgs_LinkDrawFinished;
  48. }
  49. }
  50. }
  51. public AnnotArgsType AnnotType
  52. {
  53. get { return annotType; }
  54. set
  55. {
  56. SetProperty(ref annotType, value);
  57. }
  58. }
  59. private WriteableBitmap previewImage;
  60. public WriteableBitmap PreviewImage
  61. {
  62. get { return previewImage; }
  63. set
  64. {
  65. SetProperty(ref previewImage, value);
  66. }
  67. }
  68. private CPDFViewer pdfViewer;
  69. private CPDFDocument document;
  70. private LinkAnnotProperty linkAnnot;
  71. public DelegateCommand<object> LoadedCommand { get; set; }
  72. public LinkAnnotPropertyViewModel()
  73. {
  74. LoadedCommand = new DelegateCommand<object>(Loaded);
  75. }
  76. private void _annotArgs_LinkDrawFinished(object sender, bool e)
  77. {
  78. linkAnnot.SetTextBoxEnableOrNot(e);
  79. //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  80. }
  81. private void Loaded(object obj)
  82. {
  83. if (obj is CompositeCommandParameter composite)
  84. {
  85. if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
  86. {
  87. linkAnnot = linkAnnotProperty;
  88. //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  89. int dpiHeight = (int)linkAnnot.ImagePreview.Height;
  90. int dpiWidth = (int)linkAnnot.ImagePreview.Width;
  91. if (dpiHeight <= 0 || dpiWidth <= 0)
  92. {
  93. return;
  94. }
  95. int pageIndex = pdfViewer.CurrentIndex;
  96. Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
  97. System.Windows.Size pageSize = GetPageSize(document, pageIndex);
  98. double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
  99. scale = Math.Min(scale, 1);
  100. int cropWidth = (int)(scale * pageSize.Width);
  101. int cropHeight = (int)(scale * pageSize.Height);
  102. byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
  103. WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
  104. WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
  105. PreviewImage = WirteBitmap;
  106. }
  107. }
  108. }
  109. public bool IsNavigationTarget(NavigationContext navigationContext)
  110. {
  111. return true;
  112. }
  113. public void OnNavigatedFrom(NavigationContext navigationContext)
  114. {
  115. }
  116. public void OnNavigatedTo(NavigationContext navigationContext)
  117. {
  118. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  119. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  120. if (PropertyPanel != null && pdfViewer != null)
  121. {
  122. AnnotAttribEvent = PropertyPanel.AnnotEvent;
  123. AnnotType = PropertyPanel.annot.EventType;
  124. //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  125. //if (Annot is ComPDFKitViewer.AnnotEvent.LineAnnotArgs lineAnnotArgs)
  126. //{
  127. // AnnotArgs = lineAnnotArgs;
  128. //}
  129. //GetAnnotProperty();
  130. //LoadPropertyHandler?.Invoke(null, Annot);
  131. document = pdfViewer.Document;
  132. if (linkAnnot != null)
  133. {
  134. if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
  135. {
  136. if (AnnotAttribEvent.IsAnnotCreateReset)
  137. {
  138. AnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  139. }
  140. }
  141. linkAnnot.SetTextBoxEnableOrNot(!AnnotAttribEvent.IsAnnotCreateReset);
  142. }
  143. else
  144. {
  145. AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  146. }
  147. }
  148. }
  149. public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
  150. {
  151. if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
  152. {
  153. return null;
  154. }
  155. try
  156. {
  157. byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
  158. currentZoom = currentZoom * 96 / 72;
  159. int flag = 0;
  160. if (renderAnnot)
  161. {
  162. flag = 1;
  163. }
  164. uint bgColor = 0xFFFFFFFF;
  165. CPDFPage page = document.PageAtIndex(pageIndex);
  166. if (page.IsValid())
  167. {
  168. page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
  169. }
  170. return bmpData;
  171. }
  172. catch (Exception ex)
  173. {
  174. return null;
  175. }
  176. }
  177. private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
  178. {
  179. CPDFPage currentPage = document.PageAtIndex(pageIndex);
  180. Rect boundBox = currentPage.GetBoundsForBox(displayBox);
  181. Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
  182. (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
  183. return boundRect;
  184. }
  185. private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
  186. {
  187. System.Windows.Size pageSize = document.GetPageSize(pageIndex);
  188. pageSize.Width = pageSize.Width / 72.0 * 96;
  189. pageSize.Height = pageSize.Height / 72.0 * 96;
  190. return pageSize;
  191. }
  192. }
  193. }