OutLineControlViewModel.cs 26 KB

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