BackgroundDocumentContentViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. using Size = System.Windows.Size;
  25. namespace PDF_Office.ViewModels.EditTools.Background
  26. {
  27. public class BackgroundDocumentContentViewModel : BindableBase, INavigationAware
  28. {
  29. public IEventAggregator eventAggregator;
  30. public IRegionManager regionManager;
  31. public CPDFViewer PDFViewer;
  32. public CPDFDocument Document;
  33. private ViewContentViewModel viewContentViewModel;
  34. CPDFViewer pdfViewer;
  35. private CPDFBackground background;
  36. private BackgroundInfo backgroundInfo;
  37. public bool firstin = true;
  38. public string ViewerRegionName { get; set; }
  39. public string unicode = null;
  40. public string Unicode = null;
  41. public BackgroundDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
  42. {
  43. this.regionManager = regionManager;
  44. this.eventAggregator = eventAggregator;
  45. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  46. eventAggregator.GetEvent<ConfirmEditToolsBackgroundEvent>().Subscribe(ConfirmEditToolsBackground);
  47. eventAggregator.GetEvent<SetBackgroundEvent>().Subscribe(SetBackground, e => e.Unicode == Unicode);
  48. ViewerRegionName = RegionNames.BackgroundViewRegionName;
  49. }
  50. public void ConfirmEditToolsBackground()
  51. {
  52. if (backgroundInfo != null)
  53. {
  54. CreateBackground(viewContentViewModel.PDFViewer.Document);
  55. }
  56. }
  57. public void SetBackground(BackgroundInfoUnicode backgroundInfounicode)
  58. {
  59. BackgroundInfo backgroundInfo=backgroundInfounicode.Status;
  60. this.backgroundInfo = backgroundInfo;
  61. CreateBackground(PDFViewer.Document);
  62. PDFViewer.InvalidChildVisual(false);
  63. PDFViewer.Document.ReleasePages();
  64. PDFViewer.ReloadDocument();
  65. }
  66. public void CreateBackground(CPDFDocument document, bool IsNewDoc = false)
  67. {
  68. if (backgroundInfo != null)
  69. {
  70. background = document.GetBackground();
  71. if (backgroundInfo.BackgroundType == C_Background_Type.BG_TYPE_COLOR)
  72. {
  73. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  74. background.SetColor(backgroundInfo.Color);
  75. }
  76. else
  77. {
  78. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  79. background.SetImage(backgroundInfo.ImageArray, backgroundInfo.ImageWidth, backgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  80. }
  81. background.SetScale(1);
  82. background.SetRotation(backgroundInfo.Rotation);
  83. background.SetOpacity(backgroundInfo.Opacity);
  84. background.SetVertalign(backgroundInfo.Vertalign);
  85. background.SetHorizalign(backgroundInfo.Horizalign);
  86. background.SetXOffset(backgroundInfo.HorizOffset);
  87. background.SetYOffset(backgroundInfo.VertOffset);
  88. background.SetAllowsPrint(true);
  89. background.SetAllowsView(true);
  90. if (IsNewDoc) { background.SetPages("0"); }
  91. else
  92. {
  93. background.SetPages(backgroundInfo.PageRange);
  94. }
  95. background.Update();
  96. }
  97. }
  98. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  99. {
  100. }
  101. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  102. {
  103. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  104. {
  105. List<int> PageIndexLists = new List<int>();
  106. foreach (DrawPageData drawPageData in e.DrawPages)
  107. {
  108. if (backgroundInfo != null)
  109. {
  110. char[] enumerationSeparator = new char[] { ',' };
  111. char[] rangeSeparator = new char[] { '-' };
  112. if (CommonHelper.GetPagesInRange(ref PageIndexLists, backgroundInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
  113. { //TODO
  114. if (PageIndexLists.Contains(drawPageData.PageIndex - 1))
  115. {
  116. WriteableBitmap backgroundBitmap = GetBackground(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  117. e.DrawContext.DrawImage(backgroundBitmap, drawPageData.PageBound);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. private WriteableBitmap GetBackground(CPDFDocument oldDoc, double zoom, int pageIndex)
  125. {
  126. Size pageSize = oldDoc.GetPageSize(pageIndex);
  127. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  128. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  129. CreateBackground(newDoc, true);
  130. CPDFPage newPage = newDoc.PageAtIndex(0);
  131. double scale = 96.0 / 72.0;
  132. zoom = zoom * scale;
  133. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  134. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  135. newPage.RenderPageBitmapWithMatrix((float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  136. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  137. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  138. return WirteBitmap;
  139. }
  140. public bool IsNavigationTarget(NavigationContext navigationContext)
  141. {
  142. return true;
  143. }
  144. public void OnNavigatedFrom(NavigationContext navigationContext)
  145. {
  146. }
  147. public void OnNavigatedTo(NavigationContext navigationContext)
  148. {
  149. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  150. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  151. navigationContext.Parameters.TryGetValue<string>("UniCode", out unicode);
  152. if (pdfViewer != null)
  153. {
  154. if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
  155. {
  156. PDFViewer = new CPDFViewer();
  157. PDFViewer.InitDocument(pdfViewer.Document);
  158. Document = PDFViewer.Document;
  159. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  160. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  161. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  162. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  163. PDFViewer.Load();
  164. PDFViewer.ChangeViewMode(ViewMode.Single);
  165. PDFViewer.SetMouseMode(MouseModes.Default);
  166. }
  167. }
  168. }
  169. }
  170. }