OutLineControlViewModel.cs 16 KB

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