HeaderFooterDocumentContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.HeaderFooter;
  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 System.Diagnostics;
  22. using System.Drawing.Printing;
  23. using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify;
  24. using ComPDFKit.PDFWatermark;
  25. using PDF_Master.Model.EditTools.Watermark;
  26. using Microsoft.Office.Core;
  27. using PDF_Master.Model.EditTools.Bates;
  28. namespace PDF_Master.ViewModels.EditTools.HeaderFooter
  29. {
  30. public class HeaderFooterDocumentContentViewModel : BindableBase, INavigationAware
  31. {
  32. public IEventAggregator eventAggregator;
  33. public IRegionManager regionManager;
  34. public CPDFViewer PDFViewer;
  35. public CPDFDocument Document;
  36. private ViewContentViewModel viewContentViewModel;
  37. public EnumDelete EnumDelete = EnumDelete.StatusCreate;
  38. public CPDFViewer pdfViewer;
  39. private CPDFHeaderFooter headerFooter;
  40. private HeaderFooterInfo headerFooterInfo;
  41. public bool firstin = true;
  42. public string ViewerRegionName { get; set; }
  43. public string unicode = null;
  44. public string Unicode = null;
  45. private int _pageSize;
  46. public int PageRangeNumber
  47. {
  48. get { return _pageSize; }
  49. set { SetProperty(ref _pageSize, value); }
  50. }
  51. private int _currentPageIndex;
  52. public int CurrentPageIndex
  53. {
  54. get { return _currentPageIndex; }
  55. set
  56. {
  57. SetProperty(ref _currentPageIndex, value);
  58. }
  59. }
  60. private Visibility _inputIndexBoxVisible = Visibility.Collapsed;
  61. public Visibility InputIndexBoxVisible
  62. {
  63. get
  64. {
  65. return _inputIndexBoxVisible;
  66. }
  67. set
  68. {
  69. SetProperty(ref _inputIndexBoxVisible, value);
  70. }
  71. }
  72. public DelegateCommand<object> ShowInputIndexBoxCommand { set; get; }
  73. public DelegateCommand<object> GoToPageCommand { set; get; }
  74. public HeaderFooterDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
  75. {
  76. this.eventAggregator = eventAggregator;
  77. this.regionManager = regionManager;
  78. ShowInputIndexBoxCommand = new DelegateCommand<object>(ShowInputIndexBox);
  79. GoToPageCommand = new DelegateCommand<object>(GoToPage);
  80. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  81. ViewerRegionName = RegionNames.HeaderFooterViewerRegionName;
  82. eventAggregator.GetEvent<SetHeaderFooterEvent>().Subscribe(SetHeaderFooter, e => e.Unicode == Unicode);
  83. eventAggregator.GetEvent<ConfirmEditToolsHeaderFooterEvent>().Subscribe(ConfirmEditToolsHeaderFooter, e => e == Unicode);
  84. eventAggregator.GetEvent<DeleteHeaderFooterEvent>().Subscribe(DeleteHeaderFooter, e => e.Unicode == Unicode);
  85. }
  86. public void ShowInputIndexBox(object obj)
  87. {
  88. if (obj != null)
  89. {
  90. InputIndexBoxVisible = Visibility.Visible;
  91. }
  92. }
  93. public void GoToPage(object obj)
  94. {
  95. if (obj != null)
  96. {
  97. var args = obj as TextBox;
  98. if (args.Text == null)
  99. {
  100. return;
  101. }
  102. if (int.Parse(args.Text) > 0 && int.Parse(args.Text) <= PDFViewer.Document.PageCount && Regex.Match(args.Text, "^\\d+$").Success)
  103. {
  104. PDFViewer.GoToPage(int.Parse(args.Text) - 1);
  105. InputIndexBoxVisible = Visibility.Collapsed;
  106. }
  107. }
  108. }
  109. public void ConfirmEditToolsHeaderFooter(string unicode)
  110. {
  111. if (EnumDelete == EnumDelete.StatusDeleteAll)
  112. {
  113. PDFViewer.Document.GetHeaderFooter().Clear();
  114. }
  115. else
  116. {
  117. if (headerFooterInfo != null)
  118. {
  119. CreateHeaderFooter(viewContentViewModel.PDFViewer.Document);
  120. }
  121. }
  122. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  123. }
  124. public void DeleteHeaderFooter(EnumDeleteUnicode enumDeleteunicode)
  125. {
  126. if (enumDeleteunicode.Status == EnumDelete.StatusDeleteAll)
  127. {
  128. EnumDelete enumDelete = enumDeleteunicode.Status;
  129. EnumDelete = enumDelete;
  130. PDFViewer.Document.ReleasePages();
  131. PDFViewer.ReloadDocument();
  132. }
  133. }
  134. public void SetHeaderFooter(HeaderFooterInfoUnicode headerFooterInfoUnicode)
  135. {
  136. EnumDelete = EnumDelete.StatusCreate;
  137. HeaderFooterInfo headerFooterInfo = headerFooterInfoUnicode.Status;
  138. EditToolsHelper.GetPageRange(headerFooterInfo.PageRangeIndex, PDFViewer.Document, ref headerFooterInfo.PageRange, headerFooterInfo.PageRange);
  139. this.headerFooterInfo = headerFooterInfo;
  140. //CreateHeaderFooter(PDFViewer.Document);
  141. PDFViewer.InvalidChildVisual(false);
  142. }
  143. public void CreateHeaderFooter(CPDFDocument document, bool IsNewDoc = false)
  144. {
  145. if (headerFooterInfo != null)
  146. {
  147. //document.(headerFooterInfo.TextData, headerFooterInfo.margin, headerFooterInfo.PageRange, headerFooterInfo.StarPagetNumber + 1);
  148. headerFooter = document.GetHeaderFooter();
  149. for (int i = 0; i < 6; i++)
  150. {
  151. headerFooter.SetText(i, HomePageEditHelper.ReverseDate(headerFooterInfo.TextData[i].text, document));
  152. headerFooter.SetFontName(i, headerFooterInfo.TextData[i].fontName);
  153. headerFooter.SetFontSize(i, headerFooterInfo.TextData[i].fontSize);
  154. headerFooter.SetTextColor(i, headerFooterInfo.TextData[i].Color);
  155. }
  156. if (IsNewDoc)
  157. {
  158. headerFooter.SetPageOffset(1);
  159. headerFooter.SetPages("0");
  160. }
  161. else
  162. {
  163. headerFooter.SetPages(headerFooterInfo.PageRange);
  164. headerFooter.SetPageOffset(headerFooterInfo.StarPagetNumber + 1);
  165. }
  166. headerFooter.SetMargin(headerFooterInfo.margin);
  167. headerFooter.Update();
  168. }
  169. }
  170. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  171. {
  172. }
  173. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  174. {
  175. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  176. {
  177. List<int> PageIndexLists = new List<int>();
  178. foreach (DrawPageData drawPageData in e.DrawPages)
  179. {
  180. if (headerFooterInfo != null)
  181. {
  182. char[] enumerationSeparator = new char[] { ',' };
  183. char[] rangeSeparator = new char[] { '-' };
  184. if (CommonHelper.GetPagesInRange(ref PageIndexLists, headerFooterInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
  185. { //TODO
  186. if (PageIndexLists.Contains(drawPageData.PageIndex-1))
  187. {
  188. WriteableBitmap headerFooterBitmap = GetHeaderFooter(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  189. e.DrawContext.DrawImage(headerFooterBitmap, drawPageData.PageBound);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. CurrentPageIndex = PDFViewer.CurrentIndex + 1;
  196. }
  197. private WriteableBitmap GetHeaderFooter(CPDFDocument oldDoc, double zoom, int pageIndex)
  198. {
  199. Size pageSize = oldDoc.GetPageSize(pageIndex);
  200. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  201. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  202. CreateHeaderFooter(newDoc, true);
  203. CPDFPage newPage = newDoc.PageAtIndex(0);
  204. double scale = 96.0 / 72.0;
  205. zoom = zoom * 1.5;
  206. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  207. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  208. newPage.RenderPageBitmapWithMatrix(1 / (float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  209. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  210. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  211. newDoc.Release();
  212. return WirteBitmap;
  213. }
  214. public bool IsNavigationTarget(NavigationContext navigationContext)
  215. {
  216. return true;
  217. }
  218. public void OnNavigatedFrom(NavigationContext navigationContext)
  219. {
  220. }
  221. public void OnNavigatedTo(NavigationContext navigationContext)
  222. {
  223. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  224. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  225. if (pdfViewer != null)
  226. {
  227. if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
  228. {
  229. PDFViewer = new CPDFViewer();
  230. PDFViewer.InitDocument(pdfViewer.Document);
  231. Document = PDFViewer.Document;
  232. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  233. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  234. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  235. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  236. PDFViewer.Load();
  237. PDFViewer.ChangeViewMode(ViewMode.Single);
  238. PDFViewer.SetMouseMode(MouseModes.PanTool);
  239. PageRangeNumber = PDFViewer.Document.PageCount;
  240. PDFViewer.Zoom(0.5);
  241. }
  242. }
  243. }
  244. }
  245. }