OutLineControlViewModel.cs 30 KB

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