OutLineControlViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFDocument.Action;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.Model;
  6. using PDF_Office.Model.BOTA;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. namespace PDF_Office.ViewModels.BOTA
  21. {
  22. class OutLineControlViewModel : BindableBase, INavigationAware
  23. {
  24. //缩略图相关全局变量,减少内存申请次数
  25. private WriteableBitmap WirteBitmap;
  26. private byte[] bmpData;
  27. private CPDFViewer PDFViewer;
  28. public ObservableCollection<OutlineNode> Outlinelist { get; set; }
  29. private bool isInsertHead = false;
  30. public bool IsInsertHead
  31. {
  32. get { return isInsertHead; }
  33. set
  34. {
  35. SetProperty(ref isInsertHead, value);
  36. }
  37. }
  38. private bool isOnDrop = false;
  39. public bool IsOnDrop
  40. {
  41. get { return isOnDrop; }
  42. set { isOnDrop = value; }
  43. }
  44. public DelegateCommand<ObservableCollection<OutlineNode>> CollapseAllCommand { get; set; }
  45. public DelegateCommand<ObservableCollection<OutlineNode>> ExpandAllCommand { get; set; }
  46. public DelegateCommand DeleteAllCommand { get; set; }
  47. public DelegateCommand<OutlineNode> DowngradeCommand { get; set; }
  48. public OutLineControlViewModel()
  49. {
  50. Outlinelist = new ObservableCollection<OutlineNode>();
  51. DeleteAllCommand = new DelegateCommand(DeleteAll);
  52. CollapseAllCommand = new DelegateCommand<ObservableCollection<OutlineNode>>(CollapseAll);
  53. ExpandAllCommand = new DelegateCommand<ObservableCollection<OutlineNode>>(ExpandAll);
  54. }
  55. public bool IsNavigationTarget(NavigationContext navigationContext)
  56. {
  57. return true;
  58. }
  59. public void OnNavigatedFrom(NavigationContext navigationContext)
  60. {
  61. return;
  62. }
  63. public void OnNavigatedTo(NavigationContext navigationContext)
  64. {
  65. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  66. if (PDFViewer == null)
  67. {
  68. return;
  69. }
  70. List<CPDFOutline> datasource = PDFViewer.Document.GetOutlineList();
  71. Outlinelist.Clear();
  72. foreach (CPDFOutline item in datasource)
  73. {
  74. OutlineNode dto = ConvertCPDFToOutlineNode(item, null, false);
  75. if (dto != null)
  76. {
  77. Outlinelist.Add(dto);
  78. }
  79. }
  80. }
  81. public void Updata(bool IsMoveData)
  82. {
  83. PDFViewer.Document.ReleaseOutlineList();
  84. ObservableCollection<OutlineNode> list = new ObservableCollection<OutlineNode>();
  85. List<CPDFOutline> datasource = PDFViewer.Document.GetOutlineList();
  86. //遍历,转换UI状态存入临时数组
  87. foreach (CPDFOutline item in datasource)
  88. {
  89. OutlineNode dto = ConvertCPDFToOutlineNode(item, null, IsMoveData);
  90. if (dto != null)
  91. {
  92. list.Add(dto);
  93. }
  94. }
  95. //清空绑定数组,并填充新数据
  96. Outlinelist.Clear();
  97. foreach (OutlineNode item in list)
  98. {
  99. Outlinelist.Add(item);
  100. }
  101. PDFViewer.UndoManager.CanSave = true;
  102. }
  103. public bool SetTitle(CPDFOutline cPDFOutline, string Title)
  104. {
  105. if (cPDFOutline == null)
  106. {
  107. return false;
  108. }
  109. return cPDFOutline.SetTitle(Title);
  110. }
  111. public void GoToPage(TreeViewItem treeViewItem)
  112. {
  113. OutlineNode outline = treeViewItem.DataContext as OutlineNode;
  114. if (outline == null)
  115. {
  116. return;
  117. }
  118. CPDFAction action = outline.Outline.GetAction();
  119. if (action != null && action.ActionType != C_ACTION_TYPE.ACTION_TYPE_UNKNOWN)
  120. PDFViewer.ProcessAction(action);
  121. else
  122. {
  123. Size size = PDFViewer.Document.GetPageSize(Convert.ToInt32(outline.PageIndex) - 1);
  124. PDFViewer.GoToPage(Convert.ToInt32(outline.PageIndex) - 1, new Point(size.Width - outline.PositionX, size.Height - outline.PositionY));
  125. }
  126. }
  127. public void CollapseAll(ObservableCollection<OutlineNode> outlineNodes)
  128. {
  129. foreach (var item in outlineNodes)
  130. {
  131. item.IsExpanded = false;
  132. if (item.Chlidlist.Count > 0)
  133. {
  134. CollapseAll(item.Chlidlist);
  135. }
  136. }
  137. }
  138. public void ExpandAll(ObservableCollection<OutlineNode> outlineNodes)
  139. {
  140. foreach (var item in outlineNodes)
  141. {
  142. item.IsExpanded = true;
  143. if (item.Chlidlist.Count > 0)
  144. {
  145. ExpandAll(item.Chlidlist);
  146. }
  147. }
  148. }
  149. public void DeleteAll()
  150. {
  151. foreach (var item in Outlinelist)
  152. {
  153. item.Outline.RemoveFromParent(this.PDFViewer.Document);
  154. }
  155. Updata(false);
  156. }
  157. public void Downgrade(OutlineNode outline)
  158. {
  159. if (outline != null)
  160. {
  161. CPDFOutline parent = outline.Outline.GetParent();
  162. int index = GetOutlinesIndexFormParent(parent, outline.Outline);
  163. CPDFOutline newparent = parent.ChildList[index - 2];
  164. int insertindex = newparent.ChildList.Count;
  165. newparent.MoveChildAtIndex(PDFViewer.Document, outline.Outline, insertindex);
  166. Updata(false);
  167. }
  168. }
  169. public void Upgrade(OutlineNode outline)
  170. {
  171. if (outline != null)
  172. {
  173. CPDFOutline parent = outline.Outline.GetParent();
  174. CPDFOutline newparent = parent.GetParent();
  175. int index = GetOutlinesIndexFormParent(parent.GetParent(), parent);
  176. newparent.MoveChildAtIndex(PDFViewer.Document, outline.Outline, index);
  177. Updata(false);
  178. }
  179. }
  180. public void RemoveOutline(OutlineNode outline)
  181. {
  182. outline.Outline.RemoveFromParent(PDFViewer.Document);
  183. Updata(false);
  184. }
  185. /// <summary>
  186. /// 插入大纲到目标节点
  187. /// </summary>
  188. /// <param name="target">目标节点</param>
  189. /// <param name="soure">需要插入的数据</param>
  190. /// <returns></returns>
  191. public bool InsertOutLine(OutlineNode target, OutlineNode soure)
  192. {
  193. bool Tag = true;
  194. if (IsInsertHead)
  195. {
  196. CPDFOutline root = PDFViewer.Document.GetOutlineRoot();
  197. if (root != null)
  198. {
  199. Tag = root.MoveChildAtIndex(PDFViewer.Document, soure.Outline, 0);
  200. }
  201. }
  202. else
  203. {
  204. if (target.IsInsertNextLayer)
  205. {
  206. target.IsExpanded = true;
  207. Tag = target.Outline.MoveChildAtIndex(PDFViewer.Document, soure.Outline, target.Outline.ChildList.Count);
  208. }
  209. else
  210. {
  211. CPDFOutline parent = target.Outline.GetParent();
  212. if (parent != null && parent.ChildList.Count > 0)
  213. {
  214. int index = parent.ChildList.IndexOf(target.Outline);
  215. int sourceindex = parent.ChildList.IndexOf(soure.Outline);
  216. //源数据不在目标节点树中的情况
  217. if (sourceindex == -1)
  218. {
  219. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1);
  220. }
  221. //分向上和向下移动
  222. else if (sourceindex > index)
  223. {
  224. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1);
  225. }
  226. else
  227. {
  228. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index);
  229. }
  230. }
  231. }
  232. }
  233. return Tag;
  234. }
  235. /// <summary>
  236. /// 获取对应大纲所需的缩略图数据
  237. /// </summary>
  238. public WriteableBitmap LoadPreview(OutlineNode outlineNode)
  239. {
  240. CPDFPage page = PDFViewer.Document.PageAtIndex(Convert.ToInt32(outlineNode.PageIndex) - 1);
  241. Size size = PDFViewer.Document.GetPageSize(Convert.ToInt32(outlineNode.PageIndex) - 1);
  242. Point zoomXY = new Point(outlineNode.PositionX * outlineNode.Zoom, outlineNode.PositionY * outlineNode.Zoom);
  243. //数据校验,暂不确定SDK会不会给垃圾值。说是从底层返回的。
  244. if (zoomXY.X > size.Width || zoomXY.X < 0)
  245. {
  246. zoomXY.X = 0;
  247. }
  248. if (zoomXY.Y > size.Height || zoomXY.Y < 0)
  249. {
  250. zoomXY.Y = 0;
  251. }
  252. Size zoomSize = new Size((int)(size.Width * outlineNode.Zoom), (int)(size.Height * outlineNode.Zoom));
  253. bmpData = new byte[(int)(zoomSize.Width * zoomSize.Height * 4)];
  254. WirteBitmap = new WriteableBitmap((int)zoomSize.Width, (int)zoomSize.Height, 96, 96, PixelFormats.Bgra32, null);
  255. if (!page.IsValid())
  256. {
  257. return null;
  258. }
  259. page.RenderPageBitmapWithMatrix((float)outlineNode.Zoom, new Rect(zoomXY.X, zoomXY.Y, zoomSize.Width, zoomSize.Height), 0xFFFFFFFF, bmpData, 1, true);
  260. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)zoomSize.Width, (int)zoomSize.Height), bmpData, WirteBitmap.BackBufferStride, 0);
  261. WirteBitmap.Freeze();
  262. return WirteBitmap;
  263. }
  264. /// <summary>
  265. /// 将PDF对象转换为自定义数据类型,并存储
  266. /// </summary>
  267. private OutlineNode ConvertCPDFToOutlineNode(CPDFOutline outline, OutlineNode parent, bool IsMoveData)
  268. {
  269. OutlineNode node = new OutlineNode();
  270. if (outline != null)
  271. {
  272. node.IsInsertNextLayer = false;
  273. node.IsInsertCurrentLayer = false;
  274. node.IsExpanded = false;
  275. node.IsSelected = false;
  276. node.Outline = outline;
  277. node.CanUp = outline.Level == 0 ? false : true;
  278. node.CanAddParent = outline.Level == 0 ? false : true;
  279. CPDFAction action = outline.GetAction();
  280. if (action != null)
  281. {
  282. if (action.ActionType != C_ACTION_TYPE.ACTION_TYPE_URI)
  283. {
  284. CPDFDestination cPDFDestination = outline.GetDestination(PDFViewer.Document);
  285. if (cPDFDestination != null)
  286. {
  287. //存储大纲相关属性
  288. node.PageIndex = (cPDFDestination.PageIndex + 1).ToString();
  289. node.PositionX = cPDFDestination.Position_X;
  290. node.PositionY = cPDFDestination.Position_Y;
  291. if (cPDFDestination.Zoom <= 0)
  292. {
  293. node.Zoom = 1;
  294. }
  295. else
  296. {
  297. node.Zoom = cPDFDestination.Zoom;
  298. }
  299. }
  300. else
  301. {
  302. node.PageIndex = "";
  303. node.PositionX = 0;
  304. node.PositionY = 0;
  305. node.Zoom = 1;
  306. }
  307. }
  308. else
  309. {
  310. node.PageIndex = "";
  311. node.PositionX = 0;
  312. node.PositionY = 0;
  313. node.Zoom = 1;
  314. }
  315. }
  316. else
  317. {
  318. node.PageIndex = "";
  319. node.PositionX = 0;
  320. node.PositionY = 0;
  321. node.Zoom = 1;
  322. }
  323. if (parent != null)
  324. {
  325. node.Parent = parent;
  326. }
  327. if (Outlinelist != null && Outlinelist.Count > 0)
  328. {
  329. OutlineNode oldnode = FindOutlineFromDto(Outlinelist, node.Outline, IsMoveData);
  330. if (oldnode != null)
  331. {
  332. node.IsExpanded = oldnode.IsExpanded;
  333. node.IsSelected = oldnode.IsSelected;
  334. }
  335. }
  336. node.Chlidlist = new ObservableCollection<OutlineNode>();
  337. if (outline.ChildList.Count > 0)
  338. {
  339. foreach (CPDFOutline item in outline.ChildList)
  340. {
  341. node.Chlidlist.Add(ConvertCPDFToOutlineNode(item, node, IsMoveData));
  342. }
  343. }
  344. else
  345. {
  346. node.CanDown = false;
  347. }
  348. }
  349. return node;
  350. }
  351. public OutlineNode FindOutlineFromDto(ObservableCollection<OutlineNode> list, CPDFOutline outline, bool IsMoveData)
  352. {
  353. foreach (OutlineNode item in list)
  354. {
  355. CPDFOutline parent = outline.GetParent();
  356. CPDFOutline PARENT = item.Outline.GetParent();
  357. int i = GetIndexFromParent(parent.ChildList, outline);
  358. int I = GetIndexFromParent(PARENT.ChildList, item.Outline);
  359. if (IsMoveData)
  360. {
  361. if (item.Outline.Title == outline.Title)
  362. {
  363. return item;
  364. }
  365. else if (item.Chlidlist.Count > 0)
  366. {
  367. OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData);
  368. if (retdto != null)
  369. {
  370. return retdto;
  371. }
  372. }
  373. }
  374. else
  375. {
  376. if (item.Outline.Title == outline.Title && i == I && outline.Level == item.Outline.Level && ((parent.Title == PARENT.Title) || (parent.Title == null && PARENT.Title == null)))
  377. {
  378. return item;
  379. }
  380. else if (item.Chlidlist.Count > 0)
  381. {
  382. OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData);
  383. if (retdto != null)
  384. {
  385. return retdto;
  386. }
  387. }
  388. }
  389. }
  390. return null;
  391. }
  392. private int GetIndexFromParent(List<CPDFOutline> parentlist, CPDFOutline outline)
  393. {
  394. for (int i = 0; i < parentlist.Count; i++)
  395. {
  396. if (parentlist[i] == outline)
  397. {
  398. return i;
  399. }
  400. }
  401. return -1;
  402. }
  403. private int GetOutlinesIndexFormParent(CPDFOutline parentoutline, CPDFOutline outline)
  404. {
  405. if (parentoutline != null)
  406. {
  407. if (parentoutline.ChildList.Count > 0)
  408. {
  409. for (int i = 0; i < parentoutline.ChildList.Count; i++)
  410. {
  411. if (parentoutline.ChildList[i] == outline)
  412. {
  413. return i + 1;
  414. }
  415. }
  416. }
  417. return parentoutline.ChildList.Count;
  418. }
  419. return 0;
  420. }
  421. }
  422. }