OutLineControlViewModel.cs 27 KB

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