OutLineControlViewModel.cs 30 KB

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