WatermarkDocumentContentViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.EventAggregators;
  3. using PDF_Office.Model.EditTools.Background;
  4. using PDF_Office.Model;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using PDF_Office.Model.EditTools.Watermark;
  13. using ComPDFKit.PDFDocument;
  14. using ComPDFKit.PDFPage;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Media;
  17. using System.Threading.Tasks;
  18. using ComPDFKit.PDFWatermark;
  19. using PDF_Office.Helper;
  20. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
  21. using System.Drawing;
  22. using System.Windows;
  23. using ComPDFKitViewer;
  24. using Size = System.Windows.Size;
  25. using System.Diagnostics;
  26. using System.Windows.Controls;
  27. using ComPDFKit.Import;
  28. using PDF_Office.ViewModels.Tools;
  29. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  30. using System.Text.RegularExpressions;
  31. using TextBox = System.Windows.Controls.TextBox;
  32. namespace PDF_Office.ViewModels.EditTools.Watermark
  33. {
  34. public class WatermarkDocumentContentViewModel : BindableBase, INavigationAware
  35. {
  36. public IEventAggregator eventAggregator;
  37. public IRegionManager regionManager;
  38. public CPDFViewer PDFViewer;
  39. public CPDFDocument Document;
  40. private CPDFWatermark watermark;
  41. private ViewContentViewModel viewContentViewModel;
  42. private CPDFViewer pdfViewer;
  43. private WatermarkInfo watermarkInfo;
  44. public EnumDelete EnumDelete = EnumDelete.StatusCreate;
  45. public bool firstin = true;
  46. public string ViewerRegionName { get; set; }
  47. private int _pageRangeNumber;
  48. public int PageRangeNumber
  49. {
  50. get { return _pageRangeNumber; }
  51. set { SetProperty(ref _pageRangeNumber, value); }
  52. }
  53. private Visibility _inputIndexBoxVisible = Visibility.Collapsed;
  54. public Visibility InputIndexBoxVisible
  55. {
  56. get
  57. {
  58. return _inputIndexBoxVisible;
  59. }
  60. set
  61. {
  62. SetProperty(ref _inputIndexBoxVisible, value);
  63. }
  64. }
  65. private int _currentPageIndex;
  66. public int CurrentPageIndex
  67. {
  68. get { return _currentPageIndex; }
  69. set
  70. {
  71. SetProperty(ref _currentPageIndex, value);
  72. }
  73. }
  74. public DelegateCommand<object> ShowInputIndexBoxCommand { set; get; }
  75. public DelegateCommand<object> GoToPageCommand { set; get; }
  76. public string Unicode = null;
  77. public WatermarkDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
  78. {
  79. this.regionManager = regionManager;
  80. this.eventAggregator = eventAggregator;
  81. ShowInputIndexBoxCommand = new DelegateCommand<object>(ShowInputIndexBox);
  82. GoToPageCommand = new DelegateCommand<object>(GoToPage);
  83. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  84. eventAggregator.GetEvent<SetWatermarkEvent>().Subscribe(SetWatermark, e => e.Unicode == Unicode);
  85. ViewerRegionName = RegionNames.WatermarkViwerRegionName;
  86. eventAggregator.GetEvent<ConfirmEditToolsWatermarkEvent>().Subscribe(ConfirmEditToolsWatermark);
  87. eventAggregator.GetEvent<DeleteWatermarkEvent>().Subscribe(DeleteWatermark, e => e.Unicode == Unicode);
  88. }
  89. public void ShowInputIndexBox(object obj)
  90. {
  91. if (obj != null)
  92. {
  93. InputIndexBoxVisible = Visibility.Visible;
  94. }
  95. }
  96. public void GoToPage(object obj)
  97. {
  98. if (obj != null)
  99. {
  100. var args = obj as TextBox;
  101. if (args.Text == null || !Regex.Match(args.Text, "^\\d+$").Success)
  102. {
  103. return;
  104. }
  105. if (int.Parse(args.Text) > 0 && int.Parse(args.Text) <= PDFViewer.Document.PageCount)
  106. {
  107. PDFViewer.GoToPage(int.Parse(args.Text) - 1);
  108. InputIndexBoxVisible = Visibility.Collapsed;
  109. }
  110. }
  111. }
  112. public void ConfirmEditToolsWatermark()
  113. {
  114. if (EnumDelete == EnumDelete.StatusDeleteAll)
  115. {
  116. viewContentViewModel.PDFViewer.Document.DeleteWatermarks();
  117. }
  118. else
  119. {
  120. if (watermarkInfo != null)
  121. {
  122. if (EnumDelete == EnumDelete.StatusDeleteInitial)
  123. {
  124. viewContentViewModel.PDFViewer.Document.DeleteWatermarks();
  125. CreateWatermark(viewContentViewModel.PDFViewer.Document);
  126. }
  127. else
  128. {
  129. CreateWatermark(viewContentViewModel.PDFViewer.Document);
  130. }
  131. }
  132. }
  133. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  134. }
  135. public void DeleteWatermark(EnumDeleteUnicode enumDeleteunicode)
  136. {
  137. if (enumDeleteunicode.Status == EnumDelete.StatusDeleteAll || enumDeleteunicode.Status == EnumDelete.StatusDeleteInitial)
  138. {
  139. EnumDelete enumDelete = enumDeleteunicode.Status;
  140. EnumDelete = enumDelete;
  141. PDFViewer.Document.DeleteWatermarks();
  142. PDFViewer.Document.ReleasePages();
  143. PDFViewer.ReloadDocument();
  144. }
  145. }
  146. public void SetWatermark(WatermarkInfoUnicode watermarkInfounicode)
  147. {
  148. WatermarkInfo watermarkInfo = watermarkInfounicode.Status;
  149. this.watermarkInfo = watermarkInfo;
  150. EnumDelete = EnumDelete.StatusCreate;
  151. PDFViewer.InvalidChildVisual(false);
  152. }
  153. public void CreateWatermark(CPDFDocument document, bool IsNewDoc = false)
  154. {
  155. if (watermarkInfo != null)
  156. {
  157. if (IsNewDoc) { document.DeleteWatermarks(); }
  158. if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
  159. {
  160. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
  161. watermark.SetText(watermarkInfo.Text);
  162. watermark.SetFontName(watermarkInfo.FontName);
  163. watermark.SetFontSize(watermarkInfo.TextSize);
  164. watermark.SetTextRGBColor(watermarkInfo.TextColor);
  165. }
  166. else
  167. {
  168. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  169. if (watermarkInfo.ImageArray != null)
  170. {
  171. watermark.SetImage(watermarkInfo.ImageArray, watermarkInfo.ImageWidth, watermarkInfo.ImageHeight);
  172. }
  173. watermark.SetScale(watermarkInfo.ImageSize / 100);
  174. }
  175. watermark.SetRotation((float)((watermarkInfo.Rotation / 180) * Math.PI));
  176. watermark.SetOpacity((byte)(((float)watermarkInfo.Opacity / 100) * 255));
  177. watermark.SetFront(watermarkInfo.IsFront);
  178. watermark.SetVertalign(watermarkInfo.WatermarkVertalign);
  179. watermark.SetHorizalign(watermarkInfo.WatermarkHorizalign);
  180. watermark.SetFullScreen(watermarkInfo.Isfull);
  181. watermark.SetVertOffset(watermarkInfo.VertOffset);
  182. watermark.SetHorizOffset(watermarkInfo.HorizOffset);
  183. watermark.SetHorizontalSpacing(watermarkInfo.HorizontalSpacing);
  184. watermark.SetVerticalSpacing(watermarkInfo.VerticalSpacing);
  185. if (IsNewDoc) { watermark.SetPages("0"); }
  186. else { watermark.SetPages(watermarkInfo.PageRange); }
  187. Trace.WriteLine("IsCreateWatermark: " + watermark.CreateWatermark());
  188. Trace.WriteLine("IsUpdateWatermark: " + watermark.UpdateWatermark());
  189. }
  190. }
  191. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  192. {
  193. }
  194. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  195. {
  196. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  197. {
  198. List<int> PageIndexLists = new List<int>();
  199. foreach (DrawPageData drawPageData in e.DrawPages)
  200. {
  201. if (watermarkInfo != null)
  202. {
  203. char[] enumerationSeparator = new char[] { ',' };
  204. char[] rangeSeparator = new char[] { '-' };
  205. if (CommonHelper.GetPagesInRange(ref PageIndexLists, watermarkInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
  206. { //TODO
  207. Trace.WriteLine(CommonHelper.GetPagesInRange(ref PageIndexLists, watermarkInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true));
  208. Trace.WriteLine(drawPageData.PageIndex);
  209. foreach (var abbb in PageIndexLists)
  210. {
  211. Trace.WriteLine("smgui _______" + abbb);
  212. }
  213. if (PageIndexLists.Contains(drawPageData.PageIndex - 1))
  214. {
  215. if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT && watermarkInfo.Text != "")
  216. {
  217. WriteableBitmap waterMarkBitmap = GetWatermark(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  218. e.DrawContext.DrawImage(waterMarkBitmap, drawPageData.PageBound);
  219. }
  220. if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_IMG)
  221. {
  222. WriteableBitmap waterMarkBitmap = GetWatermark(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  223. e.DrawContext.DrawImage(waterMarkBitmap, drawPageData.PageBound);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. CurrentPageIndex = PDFViewer.CurrentIndex + 1;
  231. }
  232. private WriteableBitmap GetWatermark(CPDFDocument oldDoc, double zoom, int pageIndex)
  233. {
  234. Size pageSize = oldDoc.GetPageSize(pageIndex);
  235. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  236. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  237. CreateWatermark(newDoc, true);
  238. CPDFPage newPage = newDoc.PageAtIndex(0);
  239. double scale = 96.0 / 72.0;
  240. zoom = zoom * 1.5;
  241. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  242. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  243. newPage.RenderPageBitmapWithMatrix(1 / (float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  244. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  245. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  246. return WirteBitmap;
  247. }
  248. public bool IsNavigationTarget(NavigationContext navigationContext)
  249. {
  250. return true;
  251. }
  252. public void OnNavigatedFrom(NavigationContext navigationContext)
  253. {
  254. }
  255. public void OnNavigatedTo(NavigationContext navigationContext)
  256. {
  257. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  258. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  259. if (pdfViewer != null && viewContentViewModel != null)
  260. {
  261. if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
  262. {
  263. PDFViewer = new CPDFViewer();
  264. PDFViewer.InitDocument(pdfViewer.Document.FilePath);
  265. Document = PDFViewer.Document;
  266. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  267. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  268. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  269. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  270. PDFViewer.Load();
  271. PDFViewer.ChangeViewMode(ViewMode.Single);
  272. PDFViewer.SetMouseMode(MouseModes.Default);
  273. eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Publish(new CPDFViewerUnicode
  274. {
  275. Unicode = Unicode,
  276. Status = PDFViewer
  277. });
  278. PageRangeNumber = PDFViewer.Document.PageCount;
  279. PDFViewer.Zoom(0.5);
  280. }
  281. }
  282. }
  283. }
  284. }