OutLineControlViewModel.cs 27 KB

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