BatesDocumentContentViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Master.EventAggregators;
  6. using PDF_Master.Helper;
  7. using PDF_Master.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_Master.Model;
  21. using PDF_Master.Model.EditTools.HeaderFooter;
  22. using PDF_Master.Model.EditTools.Background;
  23. namespace PDF_Master.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. EditToolsHelper.GetPageRange(batesInfo.PageRangeIndex, PDFViewer.Document, ref batesInfo.PageRange, batesInfo.PageRange);
  135. this.batesInfo = batesInfo;
  136. PDFViewer.InvalidChildVisual(false);
  137. }
  138. public void CreateBates(CPDFDocument document, bool IsNewDoc = false)
  139. {
  140. if (batesInfo != null)
  141. {
  142. if (bates != null) { bates.Release(); }
  143. bates = document.GetBates();
  144. for (int i = 0; i < 6; i++)
  145. {
  146. bates.SetText(i, batesInfo.TextData[i].text);
  147. bates.SetFontName(i, batesInfo.TextData[i].fontName);
  148. bates.SetFontSize(i, batesInfo.TextData[i].fontSize);
  149. bates.SetTextColor(i, batesInfo.TextData[i].Color);
  150. }
  151. bates.SetPageOffset(batesInfo.StarPagetNumber + 1);
  152. if (IsNewDoc) { bates.SetPages("0"); }
  153. else
  154. {
  155. bates.SetPages(batesInfo.PageRange);
  156. }
  157. bates.SetMargin(batesInfo.margin);
  158. bates.Update();
  159. }
  160. }
  161. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  162. {
  163. }
  164. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  165. {
  166. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  167. {
  168. List<int> PageIndexLists = new List<int>();
  169. foreach (DrawPageData drawPageData in e.DrawPages)
  170. {
  171. if (batesInfo != null)
  172. {
  173. char[] enumerationSeparator = new char[] { ',' };
  174. char[] rangeSeparator = new char[] { '-' };
  175. if (CommonHelper.GetPagesInRange(ref PageIndexLists, batesInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
  176. { //TODO
  177. if (PageIndexLists.Contains(drawPageData.PageIndex - 1))
  178. {
  179. WriteableBitmap backgroundBitmap = GetBates(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  180. e.DrawContext.DrawImage(backgroundBitmap, drawPageData.PageBound);
  181. }
  182. }
  183. }
  184. }
  185. }
  186. CurrentPageIndex = PDFViewer.CurrentIndex + 1;
  187. }
  188. private WriteableBitmap GetBates(CPDFDocument oldDoc, double zoom, int pageIndex)
  189. {
  190. Size pageSize = oldDoc.GetPageSize(pageIndex);
  191. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  192. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  193. CreateBates(newDoc, true);
  194. CPDFPage newPage = newDoc.PageAtIndex(0);
  195. double scale = 96.0 / 72.0;
  196. zoom = zoom * 1.5;
  197. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  198. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  199. newPage.RenderPageBitmapWithMatrix(1 / (float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  200. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  201. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  202. newDoc.Release();
  203. return WirteBitmap;
  204. }
  205. public bool IsNavigationTarget(NavigationContext navigationContext)
  206. {
  207. return true;
  208. }
  209. public void OnNavigatedFrom(NavigationContext navigationContext)
  210. {
  211. }
  212. public void OnNavigatedTo(NavigationContext navigationContext)
  213. {
  214. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  215. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  216. if (pdfViewer != null)
  217. {
  218. PDFViewer = new CPDFViewer();
  219. PDFViewer.InitDocument(pdfViewer.Document);
  220. Document = PDFViewer.Document;
  221. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  222. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  223. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  224. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  225. PDFViewer.Load();
  226. PDFViewer.ChangeViewMode(ViewMode.Single);
  227. PDFViewer.SetMouseMode(MouseModes.Default);
  228. PageRangeNumber = PDFViewer.Document.PageCount;
  229. PDFViewer.Zoom(0.5);
  230. }
  231. }
  232. }
  233. }