HeaderFooterDocumentContentViewModel.cs 11 KB

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