OutLineControlViewModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. /// <summary>
  31. /// 是否为插入最顶部
  32. /// </summary>
  33. public bool IsInsertHead
  34. {
  35. get { return isInsertHead; }
  36. set
  37. {
  38. SetProperty(ref isInsertHead, value);
  39. }
  40. }
  41. private bool isOnDrop = false;
  42. public bool IsOnDrop
  43. {
  44. get { return isOnDrop; }
  45. set { isOnDrop = value; }
  46. }
  47. public DelegateCommand<ObservableCollection<OutlineNode>> CollapseAllCommand { get; set; }
  48. public DelegateCommand<ObservableCollection<OutlineNode>> ExpandAllCommand { get; set; }
  49. public DelegateCommand DeleteAllCommand { get; set; }
  50. public DelegateCommand<OutlineNode> DowngradeCommand { get; set; }
  51. public OutLineControlViewModel()
  52. {
  53. Outlinelist = new ObservableCollection<OutlineNode>();
  54. DeleteAllCommand = new DelegateCommand(DeleteAll);
  55. CollapseAllCommand = new DelegateCommand<ObservableCollection<OutlineNode>>(CollapseAll);
  56. ExpandAllCommand = new DelegateCommand<ObservableCollection<OutlineNode>>(ExpandAll);
  57. }
  58. public bool IsNavigationTarget(NavigationContext navigationContext)
  59. {
  60. return true;
  61. }
  62. public void OnNavigatedFrom(NavigationContext navigationContext)
  63. {
  64. return;
  65. }
  66. public void OnNavigatedTo(NavigationContext navigationContext)
  67. {
  68. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  69. if (PDFViewer == null)
  70. {
  71. return;
  72. }
  73. List<CPDFOutline> datasource = PDFViewer.Document.GetOutlineList();
  74. Outlinelist.Clear();
  75. foreach (CPDFOutline item in datasource)
  76. {
  77. OutlineNode dto = ConvertCPDFToOutlineNode(item, null, false);
  78. if (dto != null)
  79. {
  80. Outlinelist.Add(dto);
  81. }
  82. }
  83. PDFViewer.GetCreateOutLineInfo();
  84. }
  85. /// <summary>
  86. /// 更新大纲数据,并保存状态设置为true
  87. /// </summary>
  88. public void Updata(bool IsMoveData)
  89. {
  90. PDFViewer.Document.ReleaseOutlineList();
  91. ObservableCollection<OutlineNode> list = new ObservableCollection<OutlineNode>();
  92. List<CPDFOutline> datasource = PDFViewer.Document.GetOutlineList();
  93. //遍历,转换UI状态存入临时数组
  94. foreach (CPDFOutline item in datasource)
  95. {
  96. OutlineNode dto = ConvertCPDFToOutlineNode(item, null, IsMoveData);
  97. if (dto != null)
  98. {
  99. list.Add(dto);
  100. }
  101. }
  102. //清空绑定数组,并填充新数据
  103. Outlinelist.Clear();
  104. foreach (OutlineNode item in list)
  105. {
  106. Outlinelist.Add(item);
  107. }
  108. PDFViewer.UndoManager.CanSave = true;
  109. }
  110. /// <summary>
  111. /// 设置当前大纲对象的Title
  112. /// </summary>
  113. public bool SetTitle(CPDFOutline cPDFOutline, string Title)
  114. {
  115. if (cPDFOutline == null)
  116. {
  117. return false;
  118. }
  119. return cPDFOutline.SetTitle(Title);
  120. }
  121. /// <summary>
  122. /// 跳转到指定位置或者URL动作
  123. /// </summary>
  124. public void GoToPage(TreeViewItem treeViewItem)
  125. {
  126. OutlineNode outline = treeViewItem.DataContext as OutlineNode;
  127. if (outline == null)
  128. {
  129. return;
  130. }
  131. CPDFAction action = outline.Outline.GetAction();
  132. //if (action != null && action.ActionType != C_ACTION_TYPE.ACTION_TYPE_UNKNOWN)
  133. // PDFViewer.ProcessAction(action);
  134. //else
  135. //{
  136. Size size = PDFViewer.Document.GetPageSize(Convert.ToInt32(outline.PageIndex) - 1);
  137. PDFViewer.GoToPage(Convert.ToInt32(outline.PageIndex) - 1, new Point(size.Width - outline.PositionX, size.Height - outline.PositionY));
  138. //}
  139. }
  140. /// <summary>
  141. /// 收起所有展开
  142. /// </summary>
  143. public void CollapseAll(ObservableCollection<OutlineNode> outlineNodes)
  144. {
  145. foreach (var item in outlineNodes)
  146. {
  147. item.IsExpanded = false;
  148. if (item.Chlidlist.Count > 0)
  149. {
  150. CollapseAll(item.Chlidlist);
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// 展开所有大纲
  156. /// </summary>
  157. public void ExpandAll(ObservableCollection<OutlineNode> outlineNodes)
  158. {
  159. foreach (var item in outlineNodes)
  160. {
  161. item.IsExpanded = true;
  162. if (item.Chlidlist.Count > 0)
  163. {
  164. ExpandAll(item.Chlidlist);
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// 更改目标位置
  170. /// </summary>
  171. public void ChangeOutLineDestination(OutlineNode outline)
  172. {
  173. List<TextSelectNode> textSelectNodes = PDFViewer.GetCreateOutLineInfo();
  174. CPDFDestination info = new CPDFDestination();
  175. info.PageIndex = textSelectNodes[0].PageIndex;
  176. Size size = PDFViewer.Document.GetPageSize(textSelectNodes[0].PageIndex);
  177. info.Position_X = (float)(size.Width-textSelectNodes[0].StartPoint.X);
  178. info.Position_Y = (float)(size.Height-textSelectNodes[0].StartPoint.Y);
  179. outline.Outline.SetDestination(PDFViewer.Document, info);
  180. PDFViewer.Document.ReleaseOutlineList();
  181. Updata(false);
  182. }
  183. /// <summary>
  184. /// 添加大纲
  185. /// </summary>
  186. public void AddOutLine()
  187. {
  188. List<TextSelectNode> textSelectNodes = PDFViewer.GetCreateOutLineInfo();
  189. CPDFDestination info = new CPDFDestination();
  190. info.PageIndex = textSelectNodes[0].PageIndex;
  191. info.Position_X = (float)textSelectNodes[0].StartPoint.X;
  192. info.Position_Y = (float)textSelectNodes[0].StartPoint.Y;
  193. CPDFOutline dto = null;
  194. if (string.IsNullOrEmpty(textSelectNodes[0].SelectText))
  195. {
  196. dto.SetTitle(textSelectNodes[0].PageIndex.ToString());
  197. }
  198. else
  199. {
  200. dto.SetTitle(textSelectNodes[0].SelectText);
  201. }
  202. dto.SetDestination(PDFViewer.Document, info);
  203. PDFViewer.Document.ReleaseOutlineList();
  204. Updata(false);
  205. }
  206. /// <summary>
  207. /// 删除所有大纲
  208. /// </summary>
  209. public void DeleteAll()
  210. {
  211. foreach (var item in Outlinelist)
  212. {
  213. item.Outline.RemoveFromParent(this.PDFViewer.Document);
  214. }
  215. Updata(false);
  216. }
  217. /// <summary>
  218. /// 升级当前大纲
  219. /// </summary>
  220. public void Downgrade(OutlineNode outline)
  221. {
  222. if (outline != null)
  223. {
  224. CPDFOutline parent = outline.Outline.GetParent();
  225. int index = GetOutlinesIndexFormParent(parent, outline.Outline);
  226. CPDFOutline newparent = parent.ChildList[index - 2];
  227. int insertindex = newparent.ChildList.Count;
  228. newparent.MoveChildAtIndex(PDFViewer.Document, outline.Outline, insertindex);
  229. Updata(false);
  230. }
  231. }
  232. /// <summary>
  233. /// 降级当前大纲
  234. /// </summary>
  235. public void Upgrade(OutlineNode outline)
  236. {
  237. if (outline != null)
  238. {
  239. CPDFOutline parent = outline.Outline.GetParent();
  240. CPDFOutline newparent = parent.GetParent();
  241. int index = GetOutlinesIndexFormParent(parent.GetParent(), parent);
  242. newparent.MoveChildAtIndex(PDFViewer.Document, outline.Outline, index);
  243. Updata(false);
  244. }
  245. }
  246. /// <summary>
  247. /// 删除当前大纲
  248. /// </summary>
  249. public void RemoveOutline(OutlineNode outline)
  250. {
  251. outline.Outline.RemoveFromParent(PDFViewer.Document);
  252. Updata(false);
  253. }
  254. /// <summary>
  255. /// 插入大纲到目标节点
  256. /// </summary>
  257. /// <param name="target">目标节点</param>
  258. /// <param name="soure">需要插入的数据</param>
  259. /// <returns></returns>
  260. public bool InsertOutLine(OutlineNode target, OutlineNode soure)
  261. {
  262. bool Tag = true;
  263. if (IsInsertHead)
  264. {
  265. CPDFOutline root = PDFViewer.Document.GetOutlineRoot();
  266. if (root != null)
  267. {
  268. Tag = root.MoveChildAtIndex(PDFViewer.Document, soure.Outline, 0);
  269. }
  270. }
  271. else
  272. {
  273. if (target.IsInsertNextLayer)
  274. {
  275. target.IsExpanded = true;
  276. Tag = target.Outline.MoveChildAtIndex(PDFViewer.Document, soure.Outline, target.Outline.ChildList.Count);
  277. }
  278. else
  279. {
  280. CPDFOutline parent = target.Outline.GetParent();
  281. if (parent != null && parent.ChildList.Count > 0)
  282. {
  283. int index = parent.ChildList.IndexOf(target.Outline);
  284. int sourceindex = parent.ChildList.IndexOf(soure.Outline);
  285. //源数据不在目标节点树中的情况
  286. if (sourceindex == -1)
  287. {
  288. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1);
  289. }
  290. //分向上和向下移动
  291. else if (sourceindex > index)
  292. {
  293. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1);
  294. }
  295. else
  296. {
  297. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index);
  298. }
  299. }
  300. }
  301. }
  302. return Tag;
  303. }
  304. /// <summary>
  305. /// 获取对应大纲所需的缩略图数据
  306. /// </summary>
  307. public WriteableBitmap LoadPreview(OutlineNode outlineNode)
  308. {
  309. CPDFPage page = PDFViewer.Document.PageAtIndex(Convert.ToInt32(outlineNode.PageIndex) - 1);
  310. Size size = PDFViewer.Document.GetPageSize(Convert.ToInt32(outlineNode.PageIndex) - 1);
  311. Point zoomXY = new Point(outlineNode.PositionX * outlineNode.Zoom, outlineNode.PositionY * outlineNode.Zoom);
  312. //数据校验,暂不确定SDK会不会给垃圾值。说是从底层返回的。
  313. if (zoomXY.X > size.Width || zoomXY.X < 0)
  314. {
  315. zoomXY.X = 0;
  316. }
  317. if (zoomXY.Y > size.Height || zoomXY.Y < 0)
  318. {
  319. zoomXY.Y = 0;
  320. }
  321. Size zoomSize = new Size((int)(size.Width * outlineNode.Zoom), (int)(size.Height * outlineNode.Zoom));
  322. bmpData = new byte[(int)(zoomSize.Width * zoomSize.Height * 4)];
  323. WirteBitmap = new WriteableBitmap((int)zoomSize.Width, (int)zoomSize.Height, 96, 96, PixelFormats.Bgra32, null);
  324. if (!page.IsValid())
  325. {
  326. return null;
  327. }
  328. page.RenderPageBitmapWithMatrix((float)outlineNode.Zoom, new Rect(zoomXY.X, zoomXY.Y, zoomSize.Width, zoomSize.Height), 0xFFFFFFFF, bmpData, 1, true);
  329. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)zoomSize.Width, (int)zoomSize.Height), bmpData, WirteBitmap.BackBufferStride, 0);
  330. WirteBitmap.Freeze();
  331. return WirteBitmap;
  332. }
  333. /// <summary>
  334. /// 将PDF对象转换为自定义数据类型,并存储
  335. /// </summary>
  336. private OutlineNode ConvertCPDFToOutlineNode(CPDFOutline outline, OutlineNode parent, bool IsMoveData)
  337. {
  338. OutlineNode node = new OutlineNode();
  339. if (outline != null)
  340. {
  341. node.IsInsertNextLayer = false;
  342. node.IsInsertCurrentLayer = false;
  343. node.IsExpanded = false;
  344. node.IsSelected = false;
  345. node.Outline = outline;
  346. node.CanUp = outline.Level == 0 ? false : true;
  347. node.CanAddParent = outline.Level == 0 ? false : true;
  348. CPDFAction action = outline.GetAction();
  349. if (action != null)
  350. {
  351. if (action.ActionType != C_ACTION_TYPE.ACTION_TYPE_URI)
  352. {
  353. CPDFDestination cPDFDestination = outline.GetDestination(PDFViewer.Document);
  354. if (cPDFDestination != null)
  355. {
  356. //存储大纲相关属性
  357. node.PageIndex = (cPDFDestination.PageIndex + 1).ToString();
  358. node.PositionX = cPDFDestination.Position_X;
  359. node.PositionY = cPDFDestination.Position_Y;
  360. if (cPDFDestination.Zoom <= 0)
  361. {
  362. node.Zoom = 1;
  363. }
  364. else
  365. {
  366. node.Zoom = cPDFDestination.Zoom;
  367. }
  368. }
  369. else
  370. {
  371. node.PageIndex = "";
  372. node.PositionX = 0;
  373. node.PositionY = 0;
  374. node.Zoom = 1;
  375. }
  376. }
  377. else
  378. {
  379. node.PageIndex = "";
  380. node.PositionX = 0;
  381. node.PositionY = 0;
  382. node.Zoom = 1;
  383. }
  384. }
  385. else
  386. {
  387. node.PageIndex = "";
  388. node.PositionX = 0;
  389. node.PositionY = 0;
  390. node.Zoom = 1;
  391. }
  392. if (parent != null)
  393. {
  394. node.Parent = parent;
  395. }
  396. if (Outlinelist != null && Outlinelist.Count > 0)
  397. {
  398. OutlineNode oldnode = FindOutlineFromDto(Outlinelist, node.Outline, IsMoveData);
  399. if (oldnode != null)
  400. {
  401. node.IsExpanded = oldnode.IsExpanded;
  402. node.IsSelected = oldnode.IsSelected;
  403. }
  404. }
  405. node.Chlidlist = new ObservableCollection<OutlineNode>();
  406. if (outline.ChildList.Count > 0)
  407. {
  408. foreach (CPDFOutline item in outline.ChildList)
  409. {
  410. node.Chlidlist.Add(ConvertCPDFToOutlineNode(item, node, IsMoveData));
  411. }
  412. }
  413. else
  414. {
  415. node.CanDown = false;
  416. }
  417. }
  418. return node;
  419. }
  420. /// <summary>
  421. /// 从当前列表中找到和SDK大纲对象相同的数据
  422. /// </summary>
  423. public OutlineNode FindOutlineFromDto(ObservableCollection<OutlineNode> list, CPDFOutline outline, bool IsMoveData)
  424. {
  425. foreach (OutlineNode item in list)
  426. {
  427. CPDFOutline parent = outline.GetParent();
  428. CPDFOutline PARENT = item.Outline.GetParent();
  429. int i = GetIndexFromParent(parent.ChildList, outline);
  430. int I = GetIndexFromParent(PARENT.ChildList, item.Outline);
  431. if (IsMoveData)
  432. {
  433. if (item.Outline.Title == outline.Title)
  434. {
  435. return item;
  436. }
  437. else if (item.Chlidlist.Count > 0)
  438. {
  439. OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData);
  440. if (retdto != null)
  441. {
  442. return retdto;
  443. }
  444. }
  445. }
  446. else
  447. {
  448. if (item.Outline.Title == outline.Title && i == I && outline.Level == item.Outline.Level && ((parent.Title == PARENT.Title) || (parent.Title == null && PARENT.Title == null)))
  449. {
  450. return item;
  451. }
  452. else if (item.Chlidlist.Count > 0)
  453. {
  454. OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData);
  455. if (retdto != null)
  456. {
  457. return retdto;
  458. }
  459. }
  460. }
  461. }
  462. return null;
  463. }
  464. private int GetIndexFromParent(List<CPDFOutline> parentlist, CPDFOutline outline)
  465. {
  466. for (int i = 0; i < parentlist.Count; i++)
  467. {
  468. if (parentlist[i] == outline)
  469. {
  470. return i;
  471. }
  472. }
  473. return -1;
  474. }
  475. private int GetOutlinesIndexFormParent(CPDFOutline parentoutline, CPDFOutline outline)
  476. {
  477. if (parentoutline != null)
  478. {
  479. if (parentoutline.ChildList.Count > 0)
  480. {
  481. for (int i = 0; i < parentoutline.ChildList.Count; i++)
  482. {
  483. if (parentoutline.ChildList[i] == outline)
  484. {
  485. return i + 1;
  486. }
  487. }
  488. }
  489. return parentoutline.ChildList.Count;
  490. }
  491. return 0;
  492. }
  493. }
  494. }