BackgroundDocumentContentViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. CPDFViewer pdfViewer;
  37. private CPDFBackground background;
  38. private BackgroundInfo backgroundInfo;
  39. public bool firstin = true;
  40. public string ViewerRegionName { get; set; }
  41. public string unicode = null;
  42. public string Unicode = null;
  43. private int _pageSize;
  44. public int PageRangeNumber
  45. {
  46. get { return _pageSize; }
  47. set { SetProperty(ref _pageSize, value); }
  48. }
  49. private int _currentPageIndex;
  50. public int CurrentPageIndex
  51. {
  52. get { return _currentPageIndex; }
  53. set
  54. {
  55. SetProperty(ref _currentPageIndex, value);
  56. }
  57. }
  58. private Visibility _inputIndexBoxVisible = Visibility.Collapsed;
  59. public Visibility InputIndexBoxVisible
  60. {
  61. get
  62. {
  63. return _inputIndexBoxVisible;
  64. }
  65. set
  66. {
  67. SetProperty(ref _inputIndexBoxVisible, value);
  68. }
  69. }
  70. public DelegateCommand<object> ShowInputIndexBoxCommand { set; get; }
  71. public DelegateCommand<object> GoToPageCommand { set; get; }
  72. public BackgroundDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
  73. {
  74. this.regionManager = regionManager;
  75. this.eventAggregator = eventAggregator;
  76. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  77. ShowInputIndexBoxCommand = new DelegateCommand<object>(ShowInputIndexBox);
  78. GoToPageCommand = new DelegateCommand<object>(GoToPage);
  79. eventAggregator.GetEvent<ConfirmEditToolsBackgroundEvent>().Subscribe(ConfirmEditToolsBackground);
  80. eventAggregator.GetEvent<SetBackgroundEvent>().Subscribe(SetBackground, e => e.Unicode == Unicode);
  81. ViewerRegionName = RegionNames.BackgroundViewerRegionName;
  82. }
  83. public void ShowInputIndexBox(object obj)
  84. {
  85. if (obj != null)
  86. {
  87. InputIndexBoxVisible = Visibility.Visible;
  88. }
  89. }
  90. public void GoToPage(object obj)
  91. {
  92. if (obj != null)
  93. {
  94. var args = obj as TextBox;
  95. if (args.Text == null)
  96. {
  97. return;
  98. }
  99. if (int.Parse(args.Text) > 0 && int.Parse(args.Text) <= PDFViewer.Document.PageCount && Regex.Match(args.Text, "^\\d+$").Success)
  100. {
  101. PDFViewer.GoToPage(int.Parse(args.Text)-1);
  102. InputIndexBoxVisible = Visibility.Collapsed;
  103. }
  104. }
  105. }
  106. public void ConfirmEditToolsBackground()
  107. {
  108. if (backgroundInfo != null)
  109. {
  110. CreateBackground(viewContentViewModel.PDFViewer.Document);
  111. }
  112. }
  113. public void SetBackground(BackgroundInfoUnicode backgroundInfounicode)
  114. {
  115. BackgroundInfo backgroundInfo = backgroundInfounicode.Status;
  116. this.backgroundInfo = backgroundInfo;
  117. CreateBackground(PDFViewer.Document);
  118. PDFViewer.InvalidChildVisual(false);
  119. PDFViewer.Document.ReleasePages();
  120. PDFViewer.ReloadDocument();
  121. }
  122. public void CreateBackground(CPDFDocument document, bool IsNewDoc = false)
  123. {
  124. if (backgroundInfo != null)
  125. {
  126. background = document.GetBackground();
  127. if (backgroundInfo.BackgroundType == C_Background_Type.BG_TYPE_COLOR)
  128. {
  129. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  130. background.SetColor(backgroundInfo.Color);
  131. }
  132. else
  133. {
  134. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  135. background.SetImage(backgroundInfo.ImageArray, backgroundInfo.ImageWidth, backgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  136. }
  137. background.SetScale(1);
  138. background.SetRotation(backgroundInfo.Rotation);
  139. background.SetOpacity(backgroundInfo.Opacity);
  140. background.SetVertalign(backgroundInfo.Vertalign);
  141. background.SetHorizalign(backgroundInfo.Horizalign);
  142. background.SetXOffset(backgroundInfo.HorizOffset);
  143. background.SetYOffset(backgroundInfo.VertOffset);
  144. background.SetAllowsPrint(true);
  145. background.SetAllowsView(true);
  146. if (IsNewDoc) { background.SetPages("0"); }
  147. else
  148. {
  149. background.SetPages(backgroundInfo.PageRange);
  150. }
  151. background.Update();
  152. }
  153. }
  154. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  155. {
  156. }
  157. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  158. {
  159. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  160. {
  161. List<int> PageIndexLists = new List<int>();
  162. foreach (DrawPageData drawPageData in e.DrawPages)
  163. {
  164. if (backgroundInfo != null)
  165. {
  166. char[] enumerationSeparator = new char[] { ',' };
  167. char[] rangeSeparator = new char[] { '-' };
  168. if (CommonHelper.GetPagesInRange(ref PageIndexLists, backgroundInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
  169. { //TODO
  170. if (PageIndexLists.Contains(drawPageData.PageIndex - 1))
  171. {
  172. WriteableBitmap backgroundBitmap = GetBackground(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  173. e.DrawContext.DrawImage(backgroundBitmap, drawPageData.PageBound);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. CurrentPageIndex = PDFViewer.CurrentIndex + 1;
  180. }
  181. private WriteableBitmap GetBackground(CPDFDocument oldDoc, double zoom, int pageIndex)
  182. {
  183. Size pageSize = oldDoc.GetPageSize(pageIndex);
  184. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  185. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  186. CreateBackground(newDoc, true);
  187. CPDFPage newPage = newDoc.PageAtIndex(0);
  188. double scale = 96.0 / 72.0;
  189. zoom = zoom * scale;
  190. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  191. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  192. newPage.RenderPageBitmapWithMatrix((float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  193. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  194. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  195. return WirteBitmap;
  196. }
  197. public bool IsNavigationTarget(NavigationContext navigationContext)
  198. {
  199. return true;
  200. }
  201. public void OnNavigatedFrom(NavigationContext navigationContext)
  202. {
  203. }
  204. public void OnNavigatedTo(NavigationContext navigationContext)
  205. {
  206. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  207. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  208. navigationContext.Parameters.TryGetValue<string>("UniCode", out unicode);
  209. if (pdfViewer != null)
  210. {
  211. if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
  212. {
  213. PDFViewer = new CPDFViewer();
  214. PDFViewer.InitDocument(pdfViewer.Document);
  215. Document = PDFViewer.Document;
  216. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  217. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  218. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  219. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  220. PDFViewer.Load();
  221. PDFViewer.ChangeViewMode(ViewMode.Single);
  222. PDFViewer.SetMouseMode(MouseModes.Default);
  223. PageRangeNumber = PDFViewer.Document.PageCount;
  224. }
  225. }
  226. }
  227. }
  228. }