BatesDocumentContentViewModel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.EventAggregators;
  6. using PDF_Office.Helper;
  7. using PDF_Office.Model.EditTools.Bates;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text.RegularExpressions;
  16. using System.Windows;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Media;
  19. using System.Windows.Controls;
  20. using PDF_Office.Model;
  21. using PDF_Office.Model.EditTools.HeaderFooter;
  22. using PDF_Office.Model.EditTools.Background;
  23. namespace PDF_Office.ViewModels.EditTools.Bates
  24. {
  25. public class BatesDocumentContentViewModel : BindableBase, INavigationAware
  26. {
  27. public IEventAggregator eventAggregator;
  28. public IRegionManager regionManager;
  29. public CPDFViewer PDFViewer;
  30. public CPDFDocument Document;
  31. private ViewContentViewModel viewContentViewModel;
  32. public EnumDelete EnumDelete = EnumDelete.StatusCreate;
  33. public CPDFViewer pdfViewer;
  34. private CPDFBates bates;
  35. private BatesInfo batesInfo;
  36. public bool firstin = true;
  37. public string ViewerRegionName { get; set; }
  38. public string unicode = null;
  39. public string Unicode = null;
  40. private int _pageSize;
  41. public int PageRangeNumber
  42. {
  43. get { return _pageSize; }
  44. set { SetProperty(ref _pageSize, value); }
  45. }
  46. private int _currentPageIndex;
  47. public int CurrentPageIndex
  48. {
  49. get { return _currentPageIndex; }
  50. set
  51. {
  52. SetProperty(ref _currentPageIndex, value);
  53. }
  54. }
  55. private Visibility _inputIndexBoxVisible = Visibility.Collapsed;
  56. public Visibility InputIndexBoxVisible
  57. {
  58. get
  59. {
  60. return _inputIndexBoxVisible;
  61. }
  62. set
  63. {
  64. SetProperty(ref _inputIndexBoxVisible, value);
  65. }
  66. }
  67. public DelegateCommand<object> ShowInputIndexBoxCommand { set; get; }
  68. public DelegateCommand<object> GoToPageCommand { set; get; }
  69. public BatesDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
  70. {
  71. this.regionManager = regionManager;
  72. this.eventAggregator = eventAggregator;
  73. ShowInputIndexBoxCommand = new DelegateCommand<object>(ShowInputIndexBox);
  74. GoToPageCommand = new DelegateCommand<object>(GoToPage);
  75. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  76. ViewerRegionName = RegionNames.BatesViewerRegionName;
  77. eventAggregator.GetEvent<SetBatesEvent>().Subscribe(SetBates, e => e.Unicode == Unicode);
  78. eventAggregator.GetEvent<ConfirmEditToolsBatesEvent>().Subscribe(ConfirmEditToolsBates, e => e == Unicode);
  79. eventAggregator.GetEvent<DeleteBatesEvent>().Subscribe(DeleteBates, e => e.Unicode == Unicode);
  80. }
  81. public void ShowInputIndexBox(object obj)
  82. {
  83. if (obj != null)
  84. {
  85. InputIndexBoxVisible = Visibility.Visible;
  86. }
  87. }
  88. public void GoToPage(object obj)
  89. {
  90. if (obj != null)
  91. {
  92. var args = obj as TextBox;
  93. if (args.Text == null)
  94. {
  95. return;
  96. }
  97. if (int.Parse(args.Text) > 0 && int.Parse(args.Text) <= PDFViewer.Document.PageCount && Regex.Match(args.Text, "^\\d+$").Success)
  98. {
  99. PDFViewer.GoToPage(int.Parse(args.Text) - 1);
  100. InputIndexBoxVisible = Visibility.Collapsed;
  101. }
  102. }
  103. }
  104. public void ConfirmEditToolsBates(string unicode)
  105. {
  106. if (EnumDelete == EnumDelete.StatusDeleteAll)
  107. {
  108. PDFViewer.Document.GetBates().Clear();
  109. }
  110. else
  111. {
  112. if (batesInfo != null)
  113. {
  114. CreateBates(viewContentViewModel.PDFViewer.Document);
  115. }
  116. }
  117. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  118. }
  119. public void DeleteBates(EnumDeleteUnicode enumDeleteunicode)
  120. {
  121. if (enumDeleteunicode.Status == EnumDelete.StatusDeleteAll)
  122. {
  123. EnumDelete enumDelete = enumDeleteunicode.Status;
  124. EnumDelete = enumDelete;
  125. PDFViewer.Document.GetBates().Clear();
  126. PDFViewer.Document.ReleasePages();
  127. PDFViewer.ReloadDocument();
  128. }
  129. }
  130. public void SetBates(BatesInfoUnicode batesInfoUnicode)
  131. {
  132. EnumDelete = EnumDelete.StatusCreate;
  133. BatesInfo batesInfo = batesInfoUnicode.Status;
  134. this.batesInfo = batesInfo;
  135. PDFViewer.InvalidChildVisual(false);
  136. }
  137. public void CreateBates(CPDFDocument document, bool IsNewDoc = false)
  138. {
  139. if (batesInfo != null)
  140. {
  141. if (bates != null) { bates.Release(); }
  142. bates = document.GetBates();
  143. for (int i = 0; i < 6; i++)
  144. {
  145. bates.SetText(i, batesInfo.TextData[i].text);
  146. bates.SetFontName(i, batesInfo.TextData[i].fontName);
  147. bates.SetFontSize(i, batesInfo.TextData[i].fontSize);
  148. bates.SetTextColor(i, batesInfo.TextData[i].Color);
  149. }
  150. bates.SetPageOffset(batesInfo.StarPagetNumber + 1);
  151. if (IsNewDoc) { bates.SetPages("0"); }
  152. else
  153. {
  154. bates.SetPages(batesInfo.PageRange);
  155. }
  156. bates.SetMargin(batesInfo.margin);
  157. bates.Update();
  158. }
  159. }
  160. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  161. {
  162. }
  163. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  164. {
  165. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  166. {
  167. List<int> PageIndexLists = new List<int>();
  168. foreach (DrawPageData drawPageData in e.DrawPages)
  169. {
  170. if (batesInfo != null)
  171. {
  172. char[] enumerationSeparator = new char[] { ',' };
  173. char[] rangeSeparator = new char[] { '-' };
  174. if (CommonHelper.GetPagesInRange(ref PageIndexLists, batesInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
  175. { //TODO
  176. if (PageIndexLists.Contains(drawPageData.PageIndex - 1))
  177. {
  178. WriteableBitmap backgroundBitmap = GetBates(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  179. e.DrawContext.DrawImage(backgroundBitmap, drawPageData.PageBound);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. CurrentPageIndex = PDFViewer.CurrentIndex + 1;
  186. }
  187. private WriteableBitmap GetBates(CPDFDocument oldDoc, double zoom, int pageIndex)
  188. {
  189. Size pageSize = oldDoc.GetPageSize(pageIndex);
  190. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  191. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  192. CreateBates(newDoc, true);
  193. CPDFPage newPage = newDoc.PageAtIndex(0);
  194. double scale = 96.0 / 72.0;
  195. zoom = zoom * 1.5;
  196. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  197. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  198. newPage.RenderPageBitmapWithMatrix(1 / (float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  199. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  200. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  201. return WirteBitmap;
  202. }
  203. public bool IsNavigationTarget(NavigationContext navigationContext)
  204. {
  205. return true;
  206. }
  207. public void OnNavigatedFrom(NavigationContext navigationContext)
  208. {
  209. }
  210. public void OnNavigatedTo(NavigationContext navigationContext)
  211. {
  212. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  213. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  214. if (pdfViewer != null)
  215. {
  216. PDFViewer = new CPDFViewer();
  217. PDFViewer.InitDocument(pdfViewer.Document);
  218. Document = PDFViewer.Document;
  219. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  220. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  221. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  222. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  223. PDFViewer.Load();
  224. PDFViewer.ChangeViewMode(ViewMode.Single);
  225. PDFViewer.SetMouseMode(MouseModes.Default);
  226. PageRangeNumber = PDFViewer.Document.PageCount;
  227. PDFViewer.Zoom(0.5);
  228. }
  229. }
  230. }
  231. }