WatermarkDocumentContentViewModel.cs 14 KB

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