BackgroundDocumentContentViewModel.cs 11 KB

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