OutLineControlViewModel.cs 28 KB

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