BackgroundDocumentContentViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFWatermark;
  4. using ComPDFKitViewer;
  5. using ComPDFKitViewer.PdfViewer;
  6. using PDF_Master.EventAggregators;
  7. using PDF_Master.Helper;
  8. using PDF_Master.Model;
  9. using PDF_Master.Model.EditTools.Background;
  10. using PDF_Master.Model.EditTools.Bates;
  11. using PDF_Master.Model.EditTools.Watermark;
  12. using Prism.Commands;
  13. using Prism.Events;
  14. using Prism.Mvvm;
  15. using Prism.Regions;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.Drawing;
  20. using System.Linq;
  21. using System.Text.RegularExpressions;
  22. using System.Threading.Tasks;
  23. using System.Windows;
  24. using System.Windows.Controls;
  25. using System.Windows.Media;
  26. using System.Windows.Media.Imaging;
  27. using Size = System.Windows.Size;
  28. namespace PDF_Master.ViewModels.EditTools.Background
  29. {
  30. public class BackgroundDocumentContentViewModel : 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 CPDFBackground background;
  40. private BackgroundInfo backgroundInfo;
  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 BackgroundDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
  75. {
  76. this.regionManager = regionManager;
  77. this.eventAggregator = eventAggregator;
  78. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  79. ShowInputIndexBoxCommand = new DelegateCommand<object>(ShowInputIndexBox);
  80. GoToPageCommand = new DelegateCommand<object>(GoToPage);
  81. eventAggregator.GetEvent<ConfirmEditToolsBackgroundEvent>().Subscribe(ConfirmEditToolsBackground,e=> e== Unicode);
  82. eventAggregator.GetEvent<SetBackgroundEvent>().Subscribe(SetBackground, e => e.Unicode == Unicode);
  83. eventAggregator.GetEvent<DeleteBackgroundEvent>().Subscribe(DeleteBackground, e => e.Unicode == Unicode);
  84. ViewerRegionName = RegionNames.BackgroundViewerRegionName;
  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. try
  103. {
  104. if (int.Parse(args.Text) > 0 && int.Parse(args.Text) <= PDFViewer.Document.PageCount && Regex.Match(args.Text, "^\\d+$").Success)
  105. {
  106. PDFViewer.GoToPage(int.Parse(args.Text) - 1);
  107. InputIndexBoxVisible = Visibility.Collapsed;
  108. }
  109. }
  110. catch
  111. {
  112. PDFViewer.GoToPage(1);
  113. InputIndexBoxVisible = Visibility.Collapsed;
  114. }
  115. }
  116. }
  117. public void ConfirmEditToolsBackground(string unicode)
  118. {
  119. if (EnumDelete == EnumDelete.StatusDeleteAll)
  120. {
  121. PDFViewer.Document.GetBackground().Clear();
  122. }
  123. if (backgroundInfo != null)
  124. {
  125. CreateBackground(viewContentViewModel.PDFViewer.Document);
  126. }
  127. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  128. }
  129. public void DeleteBackground(EnumDeleteUnicode enumDeleteunicode)
  130. {
  131. if(enumDeleteunicode.Status == EnumDelete.StatusDeleteAll)
  132. {
  133. EnumDelete enumDelete = enumDeleteunicode.Status;
  134. EnumDelete = enumDelete;
  135. PDFViewer.Document.GetBackground().Clear();
  136. PDFViewer.Document.ReleasePages();
  137. PDFViewer.ReloadDocument();
  138. }
  139. }
  140. public void SetBackground(BackgroundInfoUnicode backgroundInfounicode)
  141. {
  142. EnumDelete = EnumDelete.StatusCreate;
  143. PDFViewer.Document.GetBackground().Clear();
  144. PDFViewer.Document.ReleasePages();
  145. PDFViewer.ReloadDocument();
  146. BackgroundInfo backgroundInfo = backgroundInfounicode.Status;
  147. EditToolsHelper.GetPageRange(backgroundInfo.PageRangeIndex, PDFViewer.Document, ref backgroundInfo.PageRange, backgroundInfo.PageRange);
  148. this.backgroundInfo = backgroundInfo;
  149. PDFViewer.InvalidChildVisual(false);
  150. }
  151. public void CreateBackground(CPDFDocument document, bool IsNewDoc = false)
  152. {
  153. if (backgroundInfo != null)
  154. {
  155. background = document.GetBackground();
  156. if (backgroundInfo.BackgroundType == C_Background_Type.BG_TYPE_COLOR)
  157. {
  158. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  159. background.SetColor(backgroundInfo.Color);
  160. }
  161. else
  162. {
  163. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  164. background.SetImage(backgroundInfo.ImageArray, backgroundInfo.ImageWidth, backgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  165. }
  166. if (!backgroundInfo.IsRelativeScale)
  167. {
  168. background.SetScale(1);
  169. }
  170. else
  171. {
  172. background.SetScale((float)(backgroundInfo.Scale / 100));
  173. }
  174. background.SetRotation((float)((backgroundInfo.Rotation / 180) * Math.PI ));
  175. background.SetOpacity((byte)(((float)backgroundInfo.Opacity / 100) * 255));
  176. background.SetVertalign(backgroundInfo.Vertalign);
  177. background.SetHorizalign(backgroundInfo.Horizalign);
  178. background.SetXOffset(backgroundInfo.HorizOffset);
  179. background.SetYOffset(backgroundInfo.VertOffset);
  180. background.SetAllowsPrint(true);
  181. background.SetAllowsView(true);
  182. if (IsNewDoc) { background.SetPages("0"); }
  183. else
  184. {
  185. background.SetPages(backgroundInfo.PageRange);
  186. }
  187. background.Update();
  188. }
  189. }
  190. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  191. {
  192. }
  193. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  194. {
  195. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  196. {
  197. List<int> PageIndexLists = new List<int>();
  198. foreach (DrawPageData drawPageData in e.DrawPages)
  199. {
  200. if (backgroundInfo != null)
  201. {
  202. char[] enumerationSeparator = new char[] { ',' };
  203. char[] rangeSeparator = new char[] { '-' };
  204. if (CommonHelper.GetPagesInRange(ref PageIndexLists, backgroundInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
  205. { //TODO
  206. if (PageIndexLists.Contains(drawPageData.PageIndex - 1))
  207. {
  208. WriteableBitmap backgroundBitmap = GetBackground(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  209. e.DrawContext.DrawImage(backgroundBitmap, drawPageData.PageBound);
  210. }
  211. }
  212. }
  213. }
  214. }
  215. CurrentPageIndex = PDFViewer.CurrentIndex + 1;
  216. }
  217. private WriteableBitmap GetBackground(CPDFDocument oldDoc, double zoom, int pageIndex)
  218. {
  219. Size pageSize = oldDoc.GetPageSize(pageIndex);
  220. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  221. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  222. CreateBackground(newDoc, true);
  223. CPDFPage newPage = newDoc.PageAtIndex(0);
  224. double scale = 96.0 / 72.0;
  225. zoom = zoom * 1.5;
  226. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  227. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  228. newPage.RenderPageBitmapWithMatrix(1/(float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  229. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  230. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  231. newDoc.Release();
  232. return WirteBitmap;
  233. }
  234. public bool IsNavigationTarget(NavigationContext navigationContext)
  235. {
  236. return true;
  237. }
  238. public void OnNavigatedFrom(NavigationContext navigationContext)
  239. {
  240. }
  241. public void OnNavigatedTo(NavigationContext navigationContext)
  242. {
  243. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  244. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  245. if (pdfViewer != null)
  246. {
  247. if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
  248. {
  249. PDFViewer = new CPDFViewer();
  250. PDFViewer.InitDocument(pdfViewer.Document);
  251. Document = PDFViewer.Document;
  252. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  253. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  254. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  255. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  256. PDFViewer.Load();
  257. PDFViewer.ChangeViewMode(ViewMode.Single);
  258. PDFViewer.SetMouseMode(MouseModes.Default);
  259. PageRangeNumber = PDFViewer.Document.PageCount;
  260. PDFViewer.Zoom(0.5);
  261. }
  262. }
  263. }
  264. }
  265. }