LinkAnnotPropertyViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. using System.Windows.Controls;
  24. using PDF_Office.CustomControl;
  25. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  26. {
  27. internal class LinkAnnotPropertyViewModel : BindableBase, INavigationAware
  28. {
  29. public AnnotAttribEvent AnnotAttribEvent { get; set; }
  30. private int totalPage = 0;
  31. private AnnotPropertyPanel PropertyPanel;
  32. private AnnotArgsType annotType;
  33. private LinkAnnotArgs _annotArgs;
  34. public LinkAnnotArgs AnnotArgs
  35. {
  36. get
  37. {
  38. return _annotArgs;
  39. }
  40. set
  41. {
  42. if (_annotArgs != null)
  43. {
  44. _annotArgs.LinkDrawFinished -= _annotArgs_LinkDrawFinished;
  45. }
  46. _annotArgs = value;
  47. if (_annotArgs != null)
  48. {
  49. _annotArgs.LinkDrawFinished += _annotArgs_LinkDrawFinished;
  50. }
  51. }
  52. }
  53. public AnnotArgsType AnnotType
  54. {
  55. get { return annotType; }
  56. set
  57. {
  58. SetProperty(ref annotType, value);
  59. }
  60. }
  61. private string pageNumTextContent = "Enter target page";
  62. public string PageNumTextContent
  63. {
  64. get
  65. {
  66. return pageNumTextContent;
  67. }
  68. set
  69. {
  70. SetProperty(ref pageNumTextContent, value);
  71. }
  72. }
  73. private WriteableBitmap previewImage;
  74. public WriteableBitmap PreviewImage
  75. {
  76. get { return previewImage; }
  77. set
  78. {
  79. SetProperty(ref previewImage, value);
  80. }
  81. }
  82. private Visibility imagePreviewVisibility = Visibility.Collapsed;
  83. public Visibility ImagePreviewVisibility
  84. {
  85. get { return imagePreviewVisibility; }
  86. set
  87. {
  88. SetProperty(ref imagePreviewVisibility, value);
  89. }
  90. }
  91. private Visibility btnGOorBackVisibility = Visibility.Collapsed;
  92. public Visibility BtnGOorBackVisibility
  93. {
  94. get { return btnGOorBackVisibility; }
  95. set
  96. {
  97. SetProperty(ref btnGOorBackVisibility, value);
  98. }
  99. }
  100. private Visibility errorNumTipsVisibility = Visibility.Collapsed;
  101. public Visibility ErrorNumTipsVisibility
  102. {
  103. get { return errorNumTipsVisibility; }
  104. set
  105. {
  106. SetProperty(ref errorNumTipsVisibility, value);
  107. }
  108. }
  109. private Visibility errorRangeTipsVisibility = Visibility.Collapsed;
  110. public Visibility ErrorRangeTipsVisibility
  111. {
  112. get { return errorRangeTipsVisibility; }
  113. set
  114. {
  115. SetProperty(ref errorRangeTipsVisibility, value);
  116. }
  117. }
  118. private CPDFViewer pdfViewer;
  119. private CPDFDocument document;
  120. private LinkAnnotProperty linkAnnot;
  121. public DelegateCommand<object> LoadedCommand { get; set; }
  122. public DelegateCommand<object> PageNumTextChangedCommand { get; set; }
  123. public DelegateCommand<object> ToggleButtonTabCommand { get; set; }
  124. public LinkAnnotPropertyViewModel()
  125. {
  126. LoadedCommand = new DelegateCommand<object>(Loaded);
  127. PageNumTextChangedCommand = new DelegateCommand<object>(PageNumTextChanged);
  128. ToggleButtonTabCommand = new DelegateCommand<object>(ToggleButtonTabSelected);
  129. }
  130. private void ToggleButtonTabSelected(object obj)
  131. {
  132. if (obj is object[] array)
  133. {
  134. }
  135. }
  136. private void PageNumTextChanged(object obj)
  137. {
  138. if (obj is TextChangedEventArgs textChangedEventArgs)
  139. {
  140. if (textChangedEventArgs.Source is TextBoxWithTip textBox)
  141. {
  142. int pageNum = -1;
  143. if (CheckPageNumVaild(out pageNum, textBox))
  144. {
  145. if (AnnotArgs != null)
  146. {
  147. AnnotArgs.DestIndex = pageNum - 1;
  148. SetImagePreview();
  149. }
  150. }
  151. }
  152. }
  153. }
  154. private bool CheckPageNumVaild(out int pageNum, TextBoxWithTip textBox)
  155. {
  156. pageNum = -1;
  157. ErrorNumTipsVisibility = Visibility.Collapsed;
  158. ErrorRangeTipsVisibility = Visibility.Collapsed;
  159. BtnGOorBackVisibility = Visibility.Collapsed;
  160. if (string.IsNullOrEmpty(textBox.Text))
  161. {
  162. return false;
  163. }
  164. if (textBox.Text.Trim() != string.Empty)
  165. {
  166. if (!int.TryParse(textBox.Text.Trim(), out pageNum))
  167. {
  168. //ErrorNumTipsVisibility = Visibility.Visible;
  169. //ErrorRangeTipsVisibility = Visibility.Collapsed;
  170. textBox.IsError = true;
  171. textBox.ShowTip = Visibility.Visible;
  172. textBox.TipText = " Page number error.";
  173. return false;
  174. }
  175. }
  176. if (pageNum < 1 || pageNum > totalPage)
  177. {
  178. //ErrorNumTipsVisibility = Visibility.Collapsed;
  179. //ErrorRangeTipsVisibility = Visibility.Visible;
  180. textBox.IsError = true;
  181. textBox.ShowTip = Visibility.Visible;
  182. textBox.TipText = " Page number out of range.";
  183. return false;
  184. }
  185. else
  186. {
  187. textBox.IsError = false;
  188. textBox.ShowTip = Visibility.Collapsed;
  189. }
  190. BtnGOorBackVisibility = Visibility.Visible;
  191. return true;
  192. }
  193. private void SetImagePreview()
  194. {
  195. ImagePreviewVisibility = Visibility.Visible;
  196. BtnGOorBackVisibility = Visibility.Visible;
  197. int dpiHeight = (int)linkAnnot.ImagePreview.Height;
  198. int dpiWidth = (int)linkAnnot.ImagePreview.Width;
  199. if (dpiHeight <= 0 || dpiWidth <= 0)
  200. {
  201. return;
  202. }
  203. int pageIndex = AnnotArgs.DestIndex;
  204. Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
  205. System.Windows.Size pageSize = GetPageSize(document, pageIndex);
  206. double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
  207. scale = Math.Min(scale, 1);
  208. int cropWidth = (int)(scale * pageSize.Width);
  209. int cropHeight = (int)(scale * pageSize.Height);
  210. byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
  211. WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
  212. WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
  213. PreviewImage = WirteBitmap;
  214. }
  215. private void _annotArgs_LinkDrawFinished(object sender, bool e)
  216. {
  217. linkAnnot.SetTextBoxEnableOrNot(e, totalPage);
  218. //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  219. }
  220. private void Loaded(object obj)
  221. {
  222. if (obj is CompositeCommandParameter composite)
  223. {
  224. if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
  225. {
  226. linkAnnot = linkAnnotProperty;
  227. }
  228. }
  229. }
  230. public bool IsNavigationTarget(NavigationContext navigationContext)
  231. {
  232. return true;
  233. }
  234. public void OnNavigatedFrom(NavigationContext navigationContext)
  235. {
  236. }
  237. public void OnNavigatedTo(NavigationContext navigationContext)
  238. {
  239. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  240. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  241. if (PropertyPanel != null && pdfViewer != null)
  242. {
  243. AnnotAttribEvent = PropertyPanel.AnnotEvent;
  244. AnnotType = PropertyPanel.annot.EventType;
  245. document = pdfViewer.Document;
  246. totalPage = pdfViewer.Document.PageCount;
  247. if (linkAnnot != null)
  248. {
  249. if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
  250. {
  251. if (AnnotAttribEvent.IsAnnotCreateReset)
  252. {
  253. AnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  254. }
  255. }
  256. linkAnnot.SetTextBoxEnableOrNot(!AnnotAttribEvent.IsAnnotCreateReset, totalPage);
  257. }
  258. else
  259. {
  260. AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  261. }
  262. }
  263. }
  264. public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
  265. {
  266. if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
  267. {
  268. return null;
  269. }
  270. try
  271. {
  272. byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
  273. currentZoom = currentZoom * 96 / 72;
  274. int flag = 0;
  275. if (renderAnnot)
  276. {
  277. flag = 1;
  278. }
  279. uint bgColor = 0xFFFFFFFF;
  280. CPDFPage page = document.PageAtIndex(pageIndex);
  281. if (page.IsValid())
  282. {
  283. page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
  284. }
  285. return bmpData;
  286. }
  287. catch (Exception ex)
  288. {
  289. return null;
  290. }
  291. }
  292. private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
  293. {
  294. CPDFPage currentPage = document.PageAtIndex(pageIndex);
  295. Rect boundBox = currentPage.GetBoundsForBox(displayBox);
  296. Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
  297. (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
  298. return boundRect;
  299. }
  300. private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
  301. {
  302. System.Windows.Size pageSize = document.GetPageSize(pageIndex);
  303. pageSize.Width = pageSize.Width / 72.0 * 96;
  304. pageSize.Height = pageSize.Height / 72.0 * 96;
  305. return pageSize;
  306. }
  307. }
  308. }