OutLineControlViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. namespace PDF_Office.ViewModels.BOTA
  19. {
  20. class OutLineControlViewModel : BindableBase, INavigationAware
  21. {
  22. private CPDFViewer PDFViewer;
  23. public ObservableCollection<OutlineNode> Outlinelist { get; set; }
  24. private bool isInsertHead = false;
  25. public bool IsInsertHead
  26. {
  27. get { return isInsertHead; }
  28. set
  29. {
  30. SetProperty(ref isInsertHead, value);
  31. }
  32. }
  33. public OutLineControlViewModel()
  34. {
  35. Outlinelist = new ObservableCollection<OutlineNode>();
  36. }
  37. public bool IsNavigationTarget(NavigationContext navigationContext)
  38. {
  39. return true;
  40. }
  41. public void OnNavigatedFrom(NavigationContext navigationContext)
  42. {
  43. return;
  44. }
  45. public void OnNavigatedTo(NavigationContext navigationContext)
  46. {
  47. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  48. if (PDFViewer == null)
  49. {
  50. return;
  51. }
  52. List<CPDFOutline> datasource = PDFViewer.Document.GetOutlineList();
  53. Outlinelist.Clear();
  54. foreach (CPDFOutline item in datasource)
  55. {
  56. OutlineNode dto = ConvertCPDFToOutlineNode(item, null,false);
  57. if (dto != null)
  58. {
  59. Outlinelist.Add(dto);
  60. }
  61. }
  62. }
  63. public void Updata(bool IsMoveData)
  64. {
  65. PDFViewer.Document.ReleaseOutlineList();
  66. ObservableCollection<OutlineNode> list = new ObservableCollection<OutlineNode>();
  67. List<CPDFOutline> datasource = PDFViewer.Document.GetOutlineList();
  68. //遍历,转换UI状态存入临时数组
  69. foreach (CPDFOutline item in datasource)
  70. {
  71. OutlineNode dto = ConvertCPDFToOutlineNode(item, null, IsMoveData);
  72. if (dto != null)
  73. {
  74. list.Add(dto);
  75. }
  76. }
  77. //清空绑定数组,并填充新数据
  78. Outlinelist.Clear();
  79. foreach (OutlineNode item in list)
  80. {
  81. Outlinelist.Add(item);
  82. }
  83. PDFViewer.UndoManager.CanSave = true;
  84. }
  85. public bool SetTitle(CPDFOutline cPDFOutline, string Title)
  86. {
  87. if (cPDFOutline == null)
  88. {
  89. return false;
  90. }
  91. return cPDFOutline.SetTitle(Title);
  92. }
  93. /// <summary>
  94. /// 插入大纲到目标节点
  95. /// </summary>
  96. /// <param name="target">目标节点</param>
  97. /// <param name="soure">需要插入的数据</param>
  98. /// <returns></returns>
  99. public bool InsertOutLine(OutlineNode target, OutlineNode soure)
  100. {
  101. bool Tag = true;
  102. if (IsInsertHead)
  103. {
  104. CPDFOutline root = PDFViewer.Document.GetOutlineRoot();
  105. if (root != null)
  106. {
  107. Tag = root.MoveChildAtIndex(PDFViewer.Document, soure.Outline, 0);
  108. }
  109. }
  110. else
  111. {
  112. if (target.IsInsertNextLayer)
  113. {
  114. target.IsExpanded = true;
  115. Tag = target.Outline.MoveChildAtIndex(PDFViewer.Document, soure.Outline, target.Outline.ChildList.Count);
  116. }
  117. else
  118. {
  119. CPDFOutline parent = target.Outline.GetParent();
  120. if (parent != null && parent.ChildList.Count > 0)
  121. {
  122. int index = parent.ChildList.IndexOf(target.Outline);
  123. int sourceindex = parent.ChildList.IndexOf(soure.Outline);
  124. //源数据不在目标节点树中的情况
  125. if (sourceindex == -1)
  126. {
  127. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1);
  128. }
  129. //分向上和向下移动
  130. else if (sourceindex > index)
  131. {
  132. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1);
  133. }
  134. else
  135. {
  136. Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index);
  137. }
  138. }
  139. }
  140. }
  141. return Tag;
  142. }
  143. public WriteableBitmap WirteBitmap;
  144. byte[] bmpData;
  145. public WriteableBitmap LoadPreview()
  146. {
  147. CPDFPage page = PDFViewer.Document.PageAtIndex(1);
  148. Size size = PDFViewer.Document.GetPageSize(1);
  149. bmpData = new byte[(int)(size.Width * size.Height * 4)];
  150. WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
  151. if (page.IsValid())
  152. {
  153. page.RenderPageBitmapWithMatrix((float)0.5, new Rect(0, 0, (int)size.Width, (int)size.Height), 0xFFFFFFFF, bmpData, 1, true);
  154. }
  155. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
  156. WirteBitmap.Freeze();
  157. return WirteBitmap;
  158. }
  159. private OutlineNode ConvertCPDFToOutlineNode(CPDFOutline outline, OutlineNode parent,bool IsMoveData)
  160. {
  161. OutlineNode node = new OutlineNode();
  162. if (outline != null)
  163. {
  164. node.IsInsertNextLayer = false;
  165. node.IsInsertCurrentLayer = false;
  166. node.IsExpanded = false;
  167. node.Outline = outline;
  168. CPDFAction action = outline.GetAction();
  169. if (action != null)
  170. {
  171. if (action.ActionType != C_ACTION_TYPE.ACTION_TYPE_URI)
  172. {
  173. CPDFDestination cPDFDestination = outline.GetDestination(PDFViewer.Document);
  174. if (cPDFDestination != null)
  175. {
  176. node.PageIndex = (cPDFDestination.PageIndex + 1).ToString();
  177. }
  178. else
  179. {
  180. node.PageIndex = "";
  181. }
  182. }
  183. else
  184. {
  185. node.PageIndex = "";
  186. }
  187. }
  188. else
  189. {
  190. node.PageIndex = "";
  191. }
  192. if (parent != null)
  193. {
  194. node.Parent = parent;
  195. }
  196. if (Outlinelist != null && Outlinelist.Count > 0)
  197. {
  198. OutlineNode oldnode = FindOutlineFromDto(Outlinelist, node.Outline, IsMoveData);
  199. if (oldnode != null)
  200. {
  201. node.IsExpanded = oldnode.IsExpanded;
  202. }
  203. }
  204. node.Chlidlist = new ObservableCollection<OutlineNode>();
  205. if (outline.ChildList.Count > 0)
  206. {
  207. foreach (CPDFOutline item in outline.ChildList)
  208. {
  209. node.Chlidlist.Add(ConvertCPDFToOutlineNode(item, node, IsMoveData));
  210. }
  211. }
  212. }
  213. return node;
  214. }
  215. public OutlineNode FindOutlineFromDto(ObservableCollection<OutlineNode> list, CPDFOutline outline,bool IsMoveData)
  216. {
  217. foreach (OutlineNode item in list)
  218. {
  219. CPDFOutline parent = outline.GetParent();
  220. CPDFOutline PARENT = item.Outline.GetParent();
  221. int i = GetIndexFromParent(parent.ChildList, outline);
  222. int I = GetIndexFromParent(PARENT.ChildList, item.Outline);
  223. if (IsMoveData)
  224. {
  225. if (item.Outline.Title == outline.Title)
  226. {
  227. return item;
  228. }
  229. else if (item.Chlidlist.Count > 0)
  230. {
  231. OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData);
  232. if (retdto != null)
  233. {
  234. return retdto;
  235. }
  236. }
  237. }
  238. else
  239. {
  240. if (item.Outline.Title == outline.Title && i == I && outline.Level == item.Outline.Level && ((parent.Title == PARENT.Title) || (parent.Title == null && PARENT.Title == null)))
  241. {
  242. return item;
  243. }
  244. else if (item.Chlidlist.Count > 0)
  245. {
  246. OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData);
  247. if (retdto != null)
  248. {
  249. return retdto;
  250. }
  251. }
  252. }
  253. }
  254. return null;
  255. }
  256. private int GetIndexFromParent(List<CPDFOutline> parentlist, CPDFOutline outline)
  257. {
  258. for (int i = 0; i < parentlist.Count; i++)
  259. {
  260. if (parentlist[i] == outline)
  261. {
  262. return i;
  263. }
  264. }
  265. return -1;
  266. }
  267. }
  268. }