BackgroundDocumentContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.Bates;
  11. using PDF_Office.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_Office.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);
  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. if (int.Parse(args.Text) > 0 && int.Parse(args.Text) <= PDFViewer.Document.PageCount && Regex.Match(args.Text, "^\\d+$").Success)
  103. {
  104. PDFViewer.GoToPage(int.Parse(args.Text)-1);
  105. InputIndexBoxVisible = Visibility.Collapsed;
  106. }
  107. }
  108. }
  109. public void ConfirmEditToolsBackground()
  110. {
  111. if (EnumDelete == EnumDelete.StatusDeleteAll)
  112. {
  113. PDFViewer.Document.GetBackground().Clear();
  114. }
  115. if (backgroundInfo != null)
  116. {
  117. CreateBackground(viewContentViewModel.PDFViewer.Document);
  118. }
  119. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  120. }
  121. public void DeleteBackground(EnumDeleteUnicode enumDeleteunicode)
  122. {
  123. if(enumDeleteunicode.Status == EnumDelete.StatusDeleteAll)
  124. {
  125. EnumDelete enumDelete = enumDeleteunicode.Status;
  126. EnumDelete = enumDelete;
  127. PDFViewer.Document.GetBackground().Clear();
  128. PDFViewer.Document.ReleasePages();
  129. PDFViewer.ReloadDocument();
  130. }
  131. }
  132. public void SetBackground(BackgroundInfoUnicode backgroundInfounicode)
  133. {
  134. EnumDelete = EnumDelete.StatusCreate;
  135. BackgroundInfo backgroundInfo = backgroundInfounicode.Status;
  136. EditToolsHelper.GetPageRange(backgroundInfo.PageRangeIndex, PDFViewer.Document, ref backgroundInfo.PageRange, backgroundInfo.PageRange);
  137. this.backgroundInfo = backgroundInfo;
  138. PDFViewer.InvalidChildVisual(false);
  139. }
  140. public void CreateBackground(CPDFDocument document, bool IsNewDoc = false)
  141. {
  142. if (backgroundInfo != null)
  143. {
  144. background = document.GetBackground();
  145. if (backgroundInfo.BackgroundType == C_Background_Type.BG_TYPE_COLOR)
  146. {
  147. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  148. background.SetColor(backgroundInfo.Color);
  149. }
  150. else
  151. {
  152. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  153. background.SetImage(backgroundInfo.ImageArray, backgroundInfo.ImageWidth, backgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  154. }
  155. if (!backgroundInfo.IsRelativeScale)
  156. {
  157. background.SetScale(1);
  158. }
  159. else
  160. {
  161. background.SetScale((float)(backgroundInfo.Scale / 100));
  162. }
  163. background.SetRotation((float)((backgroundInfo.Rotation / 180) * Math.PI ));
  164. background.SetOpacity((byte)(((float)backgroundInfo.Opacity / 100) * 255));
  165. background.SetVertalign(backgroundInfo.Vertalign);
  166. background.SetHorizalign(backgroundInfo.Horizalign);
  167. background.SetXOffset(backgroundInfo.HorizOffset);
  168. background.SetYOffset(backgroundInfo.VertOffset);
  169. background.SetAllowsPrint(true);
  170. background.SetAllowsView(true);
  171. if (IsNewDoc) { background.SetPages("0"); }
  172. else
  173. {
  174. background.SetPages(backgroundInfo.PageRange);
  175. }
  176. background.Update();
  177. }
  178. }
  179. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  180. {
  181. }
  182. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  183. {
  184. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  185. {
  186. List<int> PageIndexLists = new List<int>();
  187. foreach (DrawPageData drawPageData in e.DrawPages)
  188. {
  189. if (backgroundInfo != null)
  190. {
  191. char[] enumerationSeparator = new char[] { ',' };
  192. char[] rangeSeparator = new char[] { '-' };
  193. if (CommonHelper.GetPagesInRange(ref PageIndexLists, backgroundInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
  194. { //TODO
  195. if (PageIndexLists.Contains(drawPageData.PageIndex - 1))
  196. {
  197. WriteableBitmap backgroundBitmap = GetBackground(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  198. e.DrawContext.DrawImage(backgroundBitmap, drawPageData.PageBound);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. CurrentPageIndex = PDFViewer.CurrentIndex + 1;
  205. }
  206. private WriteableBitmap GetBackground(CPDFDocument oldDoc, double zoom, int pageIndex)
  207. {
  208. Size pageSize = oldDoc.GetPageSize(pageIndex);
  209. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  210. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  211. CreateBackground(newDoc, true);
  212. CPDFPage newPage = newDoc.PageAtIndex(0);
  213. double scale = 96.0 / 72.0;
  214. zoom = zoom * 1.5;
  215. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  216. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  217. newPage.RenderPageBitmapWithMatrix(1/(float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  218. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  219. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  220. return WirteBitmap;
  221. }
  222. public bool IsNavigationTarget(NavigationContext navigationContext)
  223. {
  224. return true;
  225. }
  226. public void OnNavigatedFrom(NavigationContext navigationContext)
  227. {
  228. }
  229. public void OnNavigatedTo(NavigationContext navigationContext)
  230. {
  231. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  232. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  233. if (pdfViewer != null)
  234. {
  235. if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
  236. {
  237. PDFViewer = new CPDFViewer();
  238. PDFViewer.InitDocument(pdfViewer.Document);
  239. Document = PDFViewer.Document;
  240. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  241. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  242. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  243. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  244. PDFViewer.Load();
  245. PDFViewer.ChangeViewMode(ViewMode.Single);
  246. PDFViewer.SetMouseMode(MouseModes.Default);
  247. PageRangeNumber = PDFViewer.Document.PageCount;
  248. PDFViewer.Zoom(0.5);
  249. }
  250. }
  251. }
  252. }
  253. }