OutLineControlViewModel.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFDocument.Action;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Master.CustomControl;
  6. using PDF_Master.EventAggregators;
  7. using PDF_Master.Model;
  8. using PDF_Master.Model.BOTA;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. namespace PDF_Master.ViewModels.BOTA
  24. {
  25. class OutLineControlViewModel : BindableBase, INavigationAware
  26. {
  27. #region 文案
  28. private string T_title;
  29. public string T_Title
  30. {
  31. get { return T_title; }
  32. set
  33. {
  34. SetProperty(ref T_title, value);
  35. }
  36. }
  37. private string T_expand;
  38. public string T_Expand
  39. {
  40. get { return T_expand; }
  41. set
  42. {
  43. SetProperty(ref T_expand, value);
  44. }
  45. }
  46. private string T_collapse;
  47. public string T_Collapse
  48. {
  49. get { return T_collapse; }
  50. set
  51. {
  52. SetProperty(ref T_collapse, value);
  53. }
  54. }
  55. private string T_removeAll;
  56. public string T_RemoveAll
  57. {
  58. get { return T_removeAll; }
  59. set
  60. {
  61. SetProperty(ref T_removeAll, value);
  62. }
  63. }
  64. private void InitString()
  65. {
  66. T_Title = App.MainPageLoader.GetString("Outline_Title");
  67. T_Expand = App.MainPageLoader.GetString("Outline_Expand");
  68. T_Collapse = App.MainPageLoader.GetString("Outline_Collapse");
  69. T_RemoveAll = App.MainPageLoader.GetString("Outline_RemoveAll");
  70. }
  71. #endregion
  72. private string PageDefaultName = "";
  73. //缩略图相关全局变量,减少内存申请次数
  74. private WriteableBitmap WirteBitmap;
  75. private byte[] bmpData;
  76. private CPDFViewer PDFViewer;
  77. private IEventAggregator events;
  78. private string unicode;
  79. public ObservableCollection<OutlineNode> Outlinelist { get; set; }
  80. private bool isInsertHead = false;
  81. /// <summary>
  82. /// 是否为插入最顶部
  83. /// </summary>
  84. public bool IsInsertHead
  85. {
  86. get { return isInsertHead; }
  87. set
  88. {
  89. SetProperty(ref isInsertHead, value);
  90. }
  91. }
  92. private bool isOnDrop = false;
  93. public bool IsOnDrop
  94. {
  95. get { return isOnDrop; }
  96. set { isOnDrop = value; }
  97. }
  98. public DelegateCommand<ObservableCollection<OutlineNode>> CollapseAllCommand { get; set; }
  99. public DelegateCommand<ObservableCollection<OutlineNode>> ExpandAllCommand { get; set; }
  100. public DelegateCommand DeleteAllCommand { get; set; }
  101. public DelegateCommand<OutlineNode> DowngradeCommand { get; set; }
  102. public OutLineControlViewModel(IEventAggregator eventAggregator)
  103. {
  104. events = eventAggregator;
  105. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  106. Outlinelist = new ObservableCollection<OutlineNode>();
  107. DeleteAllCommand = new DelegateCommand(DeleteAll);
  108. CollapseAllCommand = new DelegateCommand<ObservableCollection<OutlineNode>>(CollapseAll);
  109. ExpandAllCommand = new DelegateCommand<ObservableCollection<OutlineNode>>(ExpandAll);
  110. InitString();
  111. events.GetEvent<PageEditNotifyEvent>().Subscribe(OneNotifyEvent, e =>e.Unicode == unicode&&e.Type == NotifyType.PageCountChanged);
  112. }
  113. public bool IsNavigationTarget(NavigationContext navigationContext)
  114. {
  115. return true;
  116. }
  117. public void OnNavigatedFrom(NavigationContext navigationContext)
  118. {
  119. return;
  120. }
  121. public void OnNavigatedTo(NavigationContext navigationContext)
  122. {
  123. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  124. if (PDFViewer == null)
  125. {
  126. return;
  127. }
  128. List<CPDFOutline> datasource = PDFViewer.Document.GetOutlineList();
  129. Outlinelist.Clear();
  130. foreach (CPDFOutline item in datasource)
  131. {
  132. OutlineNode dto = ConvertCPDFToOutlineNode(item, null, false);
  133. if (dto != null)
  134. {
  135. Outlinelist.Add(dto);
  136. }
  137. }
  138. if (Outlinelist.Count>0)
  139. {
  140. //如果是第一条数据不允许点击降级
  141. Outlinelist[0].CanDown = false;
  142. }
  143. PDFViewer.GetCreateOutLineInfo();
  144. }
  145. /// <summary>
  146. /// 缩略图或页面编辑的页面数量变化时 刷新大纲列表
  147. /// </summary>
  148. /// <param name="obj"></param>
  149. private void OneNotifyEvent(PageEditNotifyEventArgs obj)
  150. {
  151. Updata(false);
  152. }
  153. /// <summary>
  154. /// 更新大纲数据,并保存状态设置为true
  155. /// </summary>
  156. public void Updata(bool IsMoveData)
  157. {
  158. PDFViewer.Document.ReleaseOutlineList();
  159. ObservableCollection<OutlineNode> list = new ObservableCollection<OutlineNode>();
  160. List<CPDFOutline> datasource = PDFViewer.Document.GetOutlineList();
  161. //遍历,转换UI状态存入临时数组
  162. foreach (CPDFOutline item in datasource)
  163. {
  164. OutlineNode dto = ConvertCPDFToOutlineNode(item, null, IsMoveData);
  165. if (dto != null)
  166. {
  167. list.Add(dto);
  168. }
  169. }
  170. if (list.Count > 0)
  171. {
  172. //如果是第一条数据不允许点击降级
  173. list[0].CanDown = false;
  174. }
  175. //清空绑定数组,并填充新数据
  176. Outlinelist.Clear();
  177. foreach (OutlineNode item in list)
  178. {
  179. Outlinelist.Add(item);
  180. }
  181. PDFViewer.UndoManager.CanSave = true;
  182. }
  183. /// <summary>
  184. /// 设置当前大纲对象的Title
  185. /// </summary>
  186. public bool SetTitle(CPDFOutline cPDFOutline, string Title)
  187. {
  188. if (cPDFOutline == null)
  189. {
  190. return false;
  191. }
  192. return cPDFOutline.SetTitle(Title);
  193. }
  194. /// <summary>
  195. /// 跳转到指定位置或者URL动作
  196. /// </summary>
  197. public void GoToPage(TreeViewItem treeViewItem)
  198. {
  199. OutlineNode outline = treeViewItem.DataContext as OutlineNode;
  200. if (outline == null)
  201. {
  202. return;
  203. }
  204. CPDFAction action = outline.Outline.GetAction();
  205. if (action != null && action.ActionType != C_ACTION_TYPE.ACTION_TYPE_UNKNOWN)
  206. {
  207. PDFViewer.ProcessAction(action);
  208. }
  209. else if(!string.IsNullOrEmpty(outline.PageIndex))
  210. {
  211. Size size = PDFViewer.Document.GetPageSize(Convert.ToInt32(outline.PageIndex) - 1);
  212. if (outline.PositionX == -1)
  213. {
  214. outline.PositionX = 0;
  215. }
  216. if (outline.PositionY == -1)
  217. {
  218. outline.PositionY = size.Height;
  219. }
  220. PDFViewer.GoToPage(Convert.ToInt32(outline.PageIndex) - 1, new Point(outline.PositionX, size.Height - outline.PositionY));
  221. }
  222. }
  223. /// <summary>
  224. /// 收起所有展开
  225. /// </summary>
  226. public void CollapseAll(ObservableCollection<OutlineNode> outlineNodes)
  227. {
  228. foreach (var item in outlineNodes)
  229. {
  230. item.IsExpanded = false;
  231. if (item.Chlidlist.Count > 0)
  232. {
  233. CollapseAll(item.Chlidlist);
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// 展开所有大纲
  239. /// </summary>
  240. public void ExpandAll(ObservableCollection<OutlineNode> outlineNodes)
  241. {
  242. foreach (var item in outlineNodes)
  243. {
  244. item.IsExpanded = true;
  245. if (item.Chlidlist.Count > 0)
  246. {
  247. ExpandAll(item.Chlidlist);
  248. }
  249. }
  250. }
  251. /// <summary>
  252. /// 更改目标位置
  253. /// </summary>
  254. public void ChangeOutLineDestination(OutlineNode outline)
  255. {
  256. List<TextSelectNode> textSelectNodes = PDFViewer.GetCreateOutLineInfo();
  257. CPDFDestination info = new CPDFDestination();
  258. info.PageIndex = textSelectNodes[0].PageIndex;
  259. Size size = PDFViewer.Document.GetPageSize(textSelectNodes[0].PageIndex);
  260. info.Position_X = (float)(textSelectNodes[0].StartPoint.X);
  261. info.Position_Y = (float)(size.Height - textSelectNodes[0].StartPoint.Y);
  262. outline.Outline.SetDestination(PDFViewer.Document, info);
  263. Updata(false);
  264. }
  265. /// <summary>
  266. /// 添加大纲
  267. /// </summary>
  268. public int AddOutLine(OutlineNode outline)
  269. {
  270. int ItemIndex = 0;
  271. List<TextSelectNode> textSelectNodes = PDFViewer.GetCreateOutLineInfo();
  272. CPDFDestination info = new CPDFDestination();
  273. if (textSelectNodes.Count <= 0)
  274. {
  275. //SDK出错了
  276. return -1;
  277. }
  278. info.PageIndex = textSelectNodes[0].PageIndex;
  279. Size size = PDFViewer.Document.GetPageSize(info.PageIndex);
  280. info.Position_X = (float)(textSelectNodes[0].StartPoint.X);
  281. info.Position_Y = (float)(size.Height - textSelectNodes[0].StartPoint.Y);
  282. CPDFOutline dto = null;
  283. //当前有选中大纲列表
  284. if (outline != null)
  285. {
  286. CPDFOutline parentoutline = outline.Outline.GetParent();
  287. if (parentoutline == null)
  288. {
  289. //获取父级失败,直接添加在根节点最下方
  290. PDFViewer.Document.GetOutlineList();
  291. CPDFOutline parent = PDFViewer.Document.GetOutlineRoot();
  292. if (!parent.InsertChildAtIndex(PDFViewer.Document, parent.ChildList.Count, ref dto))
  293. {
  294. //SDK出错了
  295. return -1;
  296. }
  297. ItemIndex = parent.ChildList.Count;
  298. }
  299. else
  300. {
  301. //获取父节点成功,添加在父节点中选中项下方
  302. int index = GetOutlinesIndexFormParent(parentoutline, outline.Outline);
  303. parentoutline.InsertChildAtIndex(PDFViewer.Document, index, ref dto);
  304. ItemIndex = index + 1;
  305. }
  306. }
  307. else
  308. {
  309. //未选中数据,直接添加在根节点最下方
  310. PDFViewer.Document.GetOutlineList();
  311. CPDFOutline parent = PDFViewer.Document.GetOutlineRoot();
  312. if (!parent.InsertChildAtIndex(PDFViewer.Document, parent.ChildList.Count, ref dto))
  313. {
  314. //SDK出错了
  315. return -1;
  316. }
  317. ItemIndex = parent.ChildList.Count;
  318. }
  319. ///当前没有选中文字
  320. if (string.IsNullOrEmpty(textSelectNodes[0].SelectText))
  321. {
  322. string addPageName = PageDefaultName + (textSelectNodes[0].PageIndex + 1).ToString();
  323. if (!dto.SetTitle(addPageName))
  324. {
  325. //SDK出错了
  326. return -1;
  327. }
  328. }
  329. else
  330. {
  331. string addPageName = PageDefaultName + textSelectNodes[0].SelectText;
  332. if (!dto.SetTitle(addPageName))
  333. {
  334. //SDK出错了
  335. return -1;
  336. }
  337. }
  338. dto.SetDestination(PDFViewer.Document, info);
  339. Updata(false);
  340. return ItemIndex;
  341. }
  342. /// <summary>
  343. /// 添加大纲到子节点的最后一位
  344. /// </summary>
  345. public int InsertParentOutline(OutlineNode outline)
  346. {
  347. int ItemIndex = 0;
  348. List<TextSelectNode> textSelectNodes = PDFViewer.GetCreateOutLineInfo();
  349. CPDFDestination info = new CPDFDestination();
  350. if (textSelectNodes.Count <= 0)
  351. {
  352. //SDK出错了
  353. return -1;
  354. }
  355. info.PageIndex = textSelectNodes[0].PageIndex;
  356. info.Position_X = (float)textSelectNodes[0].StartPoint.X;
  357. info.Position_Y = (float)textSelectNodes[0].StartPoint.Y;
  358. CPDFOutline dto = null;
  359. CPDFOutline parent = outline.Outline.GetParent();
  360. if (parent != null)
  361. {
  362. CPDFOutline trueparent = parent.GetParent();
  363. if (trueparent != null)
  364. {
  365. ItemIndex = GetOutlinesIndexFormParent(trueparent, parent);
  366. if (!trueparent.InsertChildAtIndex(PDFViewer.Document, ItemIndex, ref dto))
  367. {
  368. return -1;
  369. }
  370. ItemIndex = ItemIndex + 1;
  371. }
  372. }
  373. ///当前没有选中文字
  374. if (string.IsNullOrEmpty(textSelectNodes[0].SelectText))
  375. {
  376. string addPageName = PageDefaultName + (textSelectNodes[0].PageIndex + 1).ToString();
  377. if (!dto.SetTitle(addPageName))
  378. {
  379. //SDK出错了
  380. return -1;
  381. }
  382. }
  383. else
  384. {
  385. string addPageName = PageDefaultName + textSelectNodes[0].SelectText;
  386. if (!dto.SetTitle(addPageName))
  387. {
  388. //SDK出错了
  389. return -1;
  390. }
  391. }
  392. dto.SetDestination(PDFViewer.Document, info);
  393. Updata(false);
  394. return ItemIndex;
  395. }
  396. /// <summary>
  397. /// 添加大纲到父节点的下一位
  398. /// </summary>
  399. public int InsertChlidOutline(OutlineNode outline)
  400. {
  401. int ItemIndex = 0;
  402. if (outline == null)
  403. {
  404. return -1;
  405. }
  406. if (outline.Outline.ChildList != null)
  407. {
  408. ItemIndex = outline.Outline.ChildList.Count;
  409. }
  410. List<TextSelectNode> textSelectNodes = PDFViewer.GetCreateOutLineInfo();
  411. CPDFDestination info = new CPDFDestination();
  412. if (textSelectNodes.Count <= 0)
  413. {
  414. //SDK出错了
  415. return -1;
  416. }
  417. info.PageIndex = textSelectNodes[0].PageIndex;
  418. info.Position_X = (float)textSelectNodes[0].StartPoint.X;
  419. info.Position_Y = (float)textSelectNodes[0].StartPoint.Y;
  420. CPDFOutline dto = null;
  421. if (!outline.Outline.InsertChildAtIndex(PDFViewer.Document, ItemIndex, ref dto))
  422. {
  423. return -1;
  424. }
  425. ///当前没有选中文字
  426. if (string.IsNullOrEmpty(textSelectNodes[0].SelectText))
  427. {
  428. string addPageName = PageDefaultName + (textSelectNodes[0].PageIndex + 1).ToString();
  429. if (!dto.SetTitle(addPageName))
  430. {
  431. //SDK出错了
  432. return -1;
  433. }
  434. }
  435. else
  436. {
  437. string addPageName = PageDefaultName + textSelectNodes[0].SelectText;
  438. if (!dto.SetTitle(addPageName))
  439. {
  440. //SDK出错了
  441. return -1;
  442. }
  443. }
  444. dto.SetDestination(PDFViewer.Document, info);
  445. Updata(false);
  446. return ItemIndex;
  447. }
  448. /// <summary>
  449. /// 删除所有大纲
  450. /// </summary>
  451. public void DeleteAll()
  452. {
  453. AlertsMessage alertsMessage = new AlertsMessage();
  454. alertsMessage.ShowDialog("", "This action will permanently delete all outlines,Whether to continue?", "No", "Yes");
  455. if (alertsMessage.result == ContentResult.Ok)
  456. {
  457. foreach (var item in Outlinelist)
  458. {
  459. item.Outline.RemoveFromParent(this.PDFViewer.Document);
  460. }
  461. Updata(false);
  462. }
  463. }
  464. /// <summary>
  465. /// 降级当前大纲
  466. /// </summary>
  467. public void Downgrade(OutlineNode outline)
  468. {
  469. if (outline != null)
  470. {
  471. CPDFOutline parent = outline.Outline.GetParent();
  472. int index = GetOutlinesIndexFormParent(parent, outline.Outline);
  473. CPDFOutline newparent = parent.ChildList[index - 2];
  474. int insertindex = newparent.ChildList.Count;
  475. newparent.MoveChildAtIndex(PDFViewer.Document, outline.Outline, insertindex);
  476. Updata(false);
  477. }
  478. }
  479. /// <summary>
  480. /// 升级当前大纲
  481. /// </summary>
  482. public void Upgrade(OutlineNode outline)
  483. {
  484. if (outline != null)
  485. {
  486. CPDFOutline parent = outline.Outline.GetParent();
  487. CPDFOutline newparent = parent.GetParent();
  488. int index = GetOutlinesIndexFormParent(parent.GetParent(), parent);
  489. newparent.MoveChildAtIndex(PDFViewer.Document, outline.Outline, index);
  490. Updata(false);
  491. }
  492. }
  493. /// <summary>
  494. /// 删除当前大纲
  495. /// </summary>
  496. public void RemoveOutline(OutlineNode outline)
  497. {
  498. outline.Outline.RemoveFromParent(PDFViewer.Document);
  499. Updata(false);
  500. }
  501. /// <summary>
  502. /// 移动大纲到目标节点
  503. /// </summary>
  504. /// <param name="target">目标节点</param>
  505. /// <param name="soure">需要插入的数据</param>
  506. /// <returns></returns>
  507. public bool MoveOutLine(OutlineNode target, OutlineNode soure)
  508. {
  509. bool Tag = true;
  510. if (IsInsertHead)
  511. {
  512. CPDFOutline root = PDFViewer.Document.GetOutlineRoot();
  513. if (root != null)
  514. {
  515. Tag = root.MoveChildAtIndex(PDFViewer.Document, soure.Outline, 0);
  516. }
  517. }
  518. else
  519. {
  520. if (target.IsInsertNextLayer)
  521. {
  522. target.IsExpanded = true;
  523. Tag = target.Outline.MoveChildAtIndex(PDFViewer.Document, soure.Outline, target.Outline.ChildList.Count);
  524. }
  525. else
  526. {
  527. CPDFOutline parent = target.Outline.GetParent();
  528. if (parent != null && parent.ChildList.Count > 0)
  529. {
  530. int index = parent.ChildList.IndexOf(target.Outline);
  531. int sourceindex = parent.ChildList.IndexOf(soure.Outline);
  532. //源数据不在目标节点树中的情况
  533. if (sourceindex == -1)
  534. {
  535. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1);
  536. }
  537. //分向上和向下移动
  538. else if (sourceindex > index)
  539. {
  540. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1);
  541. }
  542. else
  543. {
  544. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index);
  545. }
  546. }
  547. }
  548. }
  549. return Tag;
  550. }
  551. /// <summary>
  552. /// 获取对应大纲所需的缩略图数据
  553. /// </summary>
  554. public WriteableBitmap LoadPreview(OutlineNode outlineNode,int Width,int Height)
  555. {
  556. CPDFPage page = PDFViewer.Document.PageAtIndex(Convert.ToInt32(outlineNode.PageIndex) - 1);
  557. if (page==null)
  558. {
  559. return null;
  560. }
  561. Size size = PDFViewer.Document.GetPageSize(Convert.ToInt32(outlineNode.PageIndex) - 1);
  562. //当前对象添加时没创建默认对象,SDK会返回(-1,-1)
  563. if (outlineNode.PositionX == -1)
  564. {
  565. outlineNode.PositionX = 0;
  566. }
  567. if (outlineNode.PositionY == -1)
  568. {
  569. outlineNode.PositionY = size.Height;
  570. }
  571. Point zoomXY = new Point(outlineNode.PositionX * outlineNode.Zoom, outlineNode.PositionY * outlineNode.Zoom);
  572. Size zoomSize = new Size((int)(Width * outlineNode.Zoom), (int)(Height * outlineNode.Zoom));
  573. bmpData = new byte[(int)(zoomSize.Width * zoomSize.Height * 4)];
  574. WirteBitmap = new WriteableBitmap((int)zoomSize.Width, (int)zoomSize.Height, 96, 96, PixelFormats.Bgra32, null);
  575. if (!page.IsValid())
  576. {
  577. return null;
  578. }
  579. page.RenderPageBitmapWithMatrix((float)outlineNode.Zoom, new Rect(outlineNode.PositionX, size.Height- outlineNode.PositionY, zoomSize.Width, zoomSize.Height), 0xFFFFFFFF, bmpData, 1, true);
  580. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)zoomSize.Width, (int)zoomSize.Height), bmpData, WirteBitmap.BackBufferStride, 0);
  581. WirteBitmap.Freeze();
  582. return WirteBitmap;
  583. }
  584. /// <summary>
  585. /// 将PDF对象转换为自定义数据类型,并存储
  586. /// </summary>
  587. private OutlineNode ConvertCPDFToOutlineNode(CPDFOutline outline, OutlineNode parent, bool IsMoveData)
  588. {
  589. OutlineNode node = new OutlineNode();
  590. if (outline != null)
  591. {
  592. node.IsInsertNextLayer = false;
  593. node.IsInsertCurrentLayer = false;
  594. node.IsExpanded = false;
  595. node.IsSelected = false;
  596. node.Outline = outline;
  597. node.CanUp = outline.Level == 0 ? false : true;
  598. node.CanAddParent = outline.Level == 0 ? false : true;
  599. CPDFAction action = outline.GetAction();
  600. if (action != null)
  601. {
  602. if (action.ActionType != C_ACTION_TYPE.ACTION_TYPE_URI)
  603. {
  604. CPDFDestination cPDFDestination = outline.GetDestination(PDFViewer.Document);
  605. if (cPDFDestination != null&& cPDFDestination.PageIndex >=0)
  606. {
  607. //存储大纲相关属性
  608. node.PageIndex = (cPDFDestination.PageIndex + 1).ToString();
  609. node.PositionX = cPDFDestination.Position_X;
  610. node.PositionY = cPDFDestination.Position_Y;
  611. if (cPDFDestination.Zoom <= 0)
  612. {
  613. node.Zoom = 1;
  614. }
  615. else
  616. {
  617. node.Zoom = cPDFDestination.Zoom;
  618. }
  619. }
  620. else
  621. {
  622. node.PageIndex = "";
  623. node.PositionX = 0;
  624. node.PositionY = 0;
  625. node.Zoom = 1;
  626. }
  627. }
  628. else
  629. {
  630. node.PageIndex = "";
  631. node.PositionX = 0;
  632. node.PositionY = 0;
  633. node.Zoom = 1;
  634. }
  635. }
  636. else
  637. {
  638. node.PageIndex = "";
  639. node.PositionX = 0;
  640. node.PositionY = 0;
  641. node.Zoom = 1;
  642. }
  643. if (parent != null)
  644. {
  645. node.Parent = parent;
  646. }
  647. if (Outlinelist != null && Outlinelist.Count > 0)
  648. {
  649. OutlineNode oldnode = FindOutlineFromDto(Outlinelist, node.Outline, IsMoveData);
  650. if (oldnode != null)
  651. {
  652. node.IsExpanded = oldnode.IsExpanded;
  653. node.IsSelected = oldnode.IsSelected;
  654. }
  655. }
  656. node.Chlidlist = new ObservableCollection<OutlineNode>();
  657. if (outline.ChildList.Count > 0)
  658. {
  659. foreach (CPDFOutline item in outline.ChildList)
  660. {
  661. node.Chlidlist.Add(ConvertCPDFToOutlineNode(item, node, IsMoveData));
  662. }
  663. //如果是第一条数据不允许点击降级
  664. node.Chlidlist[0].CanDown = false;
  665. }
  666. else
  667. {
  668. node.CanDown = true;
  669. }
  670. }
  671. return node;
  672. }
  673. /// <summary>
  674. /// 从当前列表中找到和SDK大纲对象相同的数据
  675. /// </summary>
  676. public OutlineNode FindOutlineFromDto(ObservableCollection<OutlineNode> list, CPDFOutline outline, bool IsMoveData)
  677. {
  678. foreach (OutlineNode item in list)
  679. {
  680. CPDFOutline parent = outline.GetParent();
  681. CPDFOutline PARENT = item.Outline.GetParent();
  682. int i = GetIndexFromParent(parent.ChildList, outline);
  683. int I = GetIndexFromParent(PARENT.ChildList, item.Outline);
  684. if (IsMoveData)
  685. {
  686. if (item.Outline.Title == outline.Title)
  687. {
  688. return item;
  689. }
  690. else if (item.Chlidlist.Count > 0)
  691. {
  692. OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData);
  693. if (retdto != null)
  694. {
  695. return retdto;
  696. }
  697. }
  698. }
  699. else
  700. {
  701. if (item.Outline.Title == outline.Title && i == I && outline.Level == item.Outline.Level && ((parent.Title == PARENT.Title) || (parent.Title == null && PARENT.Title == null)))
  702. {
  703. return item;
  704. }
  705. else if (item.Chlidlist.Count > 0)
  706. {
  707. OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData);
  708. if (retdto != null)
  709. {
  710. return retdto;
  711. }
  712. }
  713. }
  714. }
  715. return null;
  716. }
  717. /// <summary>
  718. /// 从当前列表中找到对应的OutlineNode对象
  719. /// </summary>
  720. public OutlineNode FindOutlineFromList(ObservableCollection<OutlineNode> list, OutlineNode outline, int outlineindex)
  721. {
  722. //如果传入比对数据为null,则返回最后一项
  723. if (outline == null)
  724. {
  725. return list.Last();
  726. }
  727. if (list.Count<=0)
  728. {
  729. return null;
  730. }
  731. int index = list.IndexOf(outline);
  732. if (index >= 0)
  733. {
  734. if (outlineindex <= index)
  735. {
  736. return list[index];
  737. }
  738. else
  739. {
  740. return list[outlineindex - 1];
  741. }
  742. }
  743. else
  744. {
  745. foreach (var item in list)
  746. {
  747. OutlineNode node = FindOutlineFromList(item.Chlidlist, outline, outlineindex);
  748. if (node != null)
  749. {
  750. return node;
  751. }
  752. }
  753. }
  754. return null;
  755. }
  756. private int GetIndexFromParent(List<CPDFOutline> parentlist, CPDFOutline outline)
  757. {
  758. for (int i = 0; i < parentlist.Count; i++)
  759. {
  760. if (parentlist[i] == outline)
  761. {
  762. return i;
  763. }
  764. }
  765. return -1;
  766. }
  767. private int GetOutlinesIndexFormParent(CPDFOutline parentoutline, CPDFOutline outline)
  768. {
  769. if (parentoutline != null)
  770. {
  771. if (parentoutline.ChildList.Count > 0)
  772. {
  773. for (int i = 0; i < parentoutline.ChildList.Count; i++)
  774. {
  775. if (parentoutline.ChildList[i] == outline)
  776. {
  777. return i + 1;
  778. }
  779. }
  780. }
  781. return parentoutline.ChildList.Count;
  782. }
  783. return 0;
  784. }
  785. }
  786. }