StampAnnotPropertyViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ComPDFKitViewer.PdfViewer;
  6. using PDF_Office.Model;
  7. using PDF_Office.Model.AnnotPanel;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using static Dropbox.Api.Files.FileCategory;
  22. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  23. {
  24. internal class StampAnnotPropertyViewModel : BindableBase, INavigationAware
  25. {
  26. #region 标准图章相关默认配置(如果需要修改,要留意按顺序一一对应的,如果要支持多语,Path则需要替换成SDK获取图片的方法,并修改对应参数与实现代码)
  27. List<string> Path = new List<string>
  28. {
  29. "pack://application:,,,/PDF Office;component/Resources/StampIcons/Approved.png",
  30. "pack://application:,,,/PDF Office;component/Resources/StampIcons/NotApproved.png",
  31. "pack://application:,,,/PDF Office;component/Resources/StampIcons/Completed.png",
  32. "pack://application:,,,/PDF Office;component/Resources/StampIcons/Final.png",
  33. "pack://application:,,,/PDF Office;component/Resources/StampIcons/Draft.png",
  34. "pack://application:,,,/PDF Office;component/Resources/StampIcons/Confidential.png",
  35. "pack://application:,,,/PDF Office;component/Resources/StampIcons/ForPublicRelease.png",
  36. "pack://application:,,,/PDF Office;component/Resources/StampIcons/NotForPublicRelease.png",
  37. "pack://application:,,,/PDF Office;component/Resources/StampIcons/ForComment.png",
  38. "pack://application:,,,/PDF Office;component/Resources/StampIcons/Void.png",
  39. "pack://application:,,,/PDF Office;component/Resources/StampIcons/PreliminaryResults.png",
  40. "pack://application:,,,/PDF Office;component/Resources/StampIcons/InformationOnly.png",
  41. "pack://application:,,,/PDF Office;component/Resources/StampIcons/Accepted.png",
  42. "pack://application:,,,/PDF Office;component/Resources/StampIcons/Rejected.png",
  43. "pack://application:,,,/PDF Office;component/Resources/StampIcons/Witness.png",
  44. "pack://application:,,,/PDF Office;component/Resources/StampIcons/InitialHere.png",
  45. "pack://application:,,,/PDF Office;component/Resources/StampIcons/SignHere.png",
  46. "pack://application:,,,/PDF Office;component/Resources/StampIcons/revised.png",
  47. "pack://application:,,,/PDF Office;component/Resources/StampIcons/PrivateMark1.png",
  48. "pack://application:,,,/PDF Office;component/Resources/StampIcons/PrivateMark2.png",
  49. "pack://application:,,,/PDF Office;component/Resources/StampIcons/PrivateMark3.png",
  50. };
  51. List<string> StampText = new List<string>
  52. {
  53. "Approved","NotApproved","Completed","Final","Draft","Confidential","ForPublicRelease","NotForPublicRelease",
  54. "ForComment","Void","PreliminaryResults","InformationOnly","Accepted","Rejected","Witness","InitialHere","SignHere",
  55. "revised","PrivateMark#1","PrivateMark#2","PrivateMark#3"
  56. };
  57. List<int> MaxWidth = new List<int>
  58. {
  59. 218,292,234,130,150,280,386,461,282,121,405,366,30,30,133,133,133,173,30,30,30
  60. };
  61. List<int> MaxHeight = new List<int>
  62. {
  63. 66,66,66,66,66,66,66,66,66,66,66,66,30,30,39,39,39,66,30,30,30
  64. };
  65. #endregion
  66. #region 动态图章相关配置(需要支持多语则修改DynamicStampText的内容)
  67. List<string> DynamicStampText = new List<string>
  68. {
  69. "REVISED","REVIEWED","Completed","RECEIVED","APPROVED","CONFIDENTIAL"
  70. };
  71. List<C_TEXTSTAMP_COLOR> DynamicColor = new List<C_TEXTSTAMP_COLOR>
  72. {
  73. C_TEXTSTAMP_COLOR.TEXTSTAMP_RED, C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN,
  74. C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN,C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN,
  75. C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN,C_TEXTSTAMP_COLOR.TEXTSTAMP_RED,
  76. };
  77. #endregion
  78. private string author = "12312";
  79. public string Author
  80. {
  81. get { return author; }
  82. set
  83. {
  84. SetProperty(ref author, value);
  85. }
  86. }
  87. private bool isSetAuthor = false;
  88. public bool IsSetAuthor
  89. {
  90. get { return isSetAuthor; }
  91. set
  92. {
  93. SetProperty(ref isSetAuthor, value);
  94. }
  95. }
  96. private int tabControlSelectedIndex = 0;
  97. public int TabControlSelectedIndex
  98. {
  99. get { return tabControlSelectedIndex; }
  100. set
  101. {
  102. SetProperty(ref tabControlSelectedIndex, value);
  103. if (tabControlSelectedIndex == 0)
  104. {
  105. UnStandard = false;
  106. }
  107. else
  108. {
  109. UnStandard = true;
  110. }
  111. }
  112. }
  113. private bool unStandard;
  114. /// <summary>
  115. /// 判断当前是否为非标准图章,控制UI展示
  116. /// </summary>
  117. public bool UnStandard
  118. {
  119. get { return unStandard; }
  120. set
  121. {
  122. SetProperty(ref unStandard, value);
  123. }
  124. }
  125. /// <summary>
  126. /// 日期格式
  127. /// </summary>
  128. private string dateType = "F";
  129. private IDialogService dialogs;
  130. public DelegateCommand ShowDialogCommand { get; set; }
  131. private CPDFViewer PDFViewer;
  132. public ObservableCollection<Stamp> StandardStampList { get; set; }
  133. public ObservableCollection<Stamp> DynamicStampList { get; set; }
  134. public ObservableCollection<Stamp> CustomStampList { get; set; }
  135. public StampAnnotPropertyViewModel(IDialogService dialogService)
  136. {
  137. dialogs = dialogService;
  138. StandardStampList = new ObservableCollection<Stamp>();
  139. DynamicStampList = new ObservableCollection<Stamp>();
  140. CustomStampList = new ObservableCollection<Stamp>();
  141. ShowDialogCommand = new DelegateCommand(ShowDialog);
  142. InitStandardStamp();
  143. UpDataDynamicStampList();
  144. //CPDFDocument doc = CPDFDocument.CreateDocument();
  145. //bool tt= doc.InsertPage(0, 300, 500, Path[0]);
  146. //doc.WriteToFilePath("D:\\fafdas.pdf");
  147. //doc.Release();
  148. }
  149. /// <summary>
  150. /// 初始化标准图章相关内容
  151. /// </summary>
  152. private void InitStandardStamp()
  153. {
  154. for (int i = 0; i < Path.Count; i++)
  155. {
  156. Stamp standardStamp = new Stamp();
  157. standardStamp.Author = "";
  158. standardStamp.Opacity = 1;
  159. standardStamp.SourcePath = Path[i];
  160. standardStamp.StampText = StampText[i];
  161. standardStamp.MaxWidth = MaxWidth[i];
  162. standardStamp.MaxHeight = MaxHeight[i];
  163. standardStamp.Type = StampType.STANDARD_STAMP;
  164. StandardStampList.Add(standardStamp);
  165. }
  166. }
  167. public bool IsNavigationTarget(NavigationContext navigationContext)
  168. {
  169. return true;
  170. }
  171. public void OnNavigatedFrom(NavigationContext navigationContext)
  172. {
  173. return;
  174. }
  175. public void OnNavigatedTo(NavigationContext navigationContext)
  176. {
  177. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  178. if (PDFViewer == null)
  179. {
  180. return;
  181. }
  182. }
  183. /// <summary>
  184. /// 在PDFView上创建图章的统一方法
  185. /// </summary>
  186. /// <param name="stamp"></param>
  187. public void SetStamp(Stamp stamp)
  188. {
  189. StampAnnotArgs Args = new StampAnnotArgs();
  190. Args.StampText = stamp.StampText;
  191. Args.Author = stamp.Author;
  192. Args.Opacity = stamp.Opacity;
  193. Args.MaxWidth = stamp.MaxWidth;
  194. Args.MaxHeight = stamp.MaxHeight;
  195. Args.StampTextDate = stamp.StampTextDate;
  196. Args.TextColor = stamp.TextColor;
  197. if (!string.IsNullOrEmpty(stamp.SourcePath))
  198. {
  199. BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
  200. Args.ImageArray = new byte[image.PixelWidth * image.PixelHeight * 4];
  201. image.CopyPixels(Args.ImageArray, image.PixelWidth * 4, 0);
  202. Args.ImageHeight = image.PixelHeight;
  203. Args.ImageWidth = image.PixelWidth;
  204. }
  205. else
  206. {
  207. Args.ImageArray = new byte[stamp.ImageSource.PixelWidth * stamp.ImageSource.PixelHeight * 4];
  208. stamp.ImageSource.CopyPixels(Args.ImageArray, stamp.ImageSource.PixelWidth * 4, 0);
  209. Args.ImageHeight = stamp.ImageSource.PixelHeight;
  210. Args.ImageWidth = stamp.ImageSource.PixelWidth;
  211. }
  212. Args.Type = stamp.Type;
  213. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  214. PDFViewer.SetToolParam(Args);
  215. }
  216. /// <summary>
  217. /// 更新动态图章对象相关属性
  218. /// </summary>
  219. public void UpDataDynamicStampList()
  220. {
  221. CPDFDocument doc = CPDFDocument.CreateDocument();
  222. bool tt = doc.InsertPage(0, 300, 500, null);
  223. CPDFPage docPage = doc.PageAtIndex(0);
  224. CPDFStampAnnotation stampCore = docPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  225. var x = stampCore.GetTextAttribute();
  226. DynamicStampList.Clear();
  227. for (int i = 0; i < DynamicStampText.Count; i++)
  228. {
  229. string date = "";
  230. //区分机密(CONFIDENTIAL)和其他动态图章的区别
  231. if (i < DynamicStampText.Count - 1)
  232. {
  233. date = System.DateTime.Now.ToString(dateType);
  234. }
  235. Stamp standardStamp = new Stamp();
  236. if (IsSetAuthor && !string.IsNullOrEmpty(Author))
  237. {
  238. standardStamp.Author = Author;
  239. if (i < DynamicStampText.Count - 1)
  240. {
  241. standardStamp.StampTextDate = "By " + Author + " at " + date;
  242. }
  243. else
  244. {
  245. standardStamp.StampTextDate = Author;
  246. }
  247. }
  248. else
  249. {
  250. standardStamp.StampTextDate = date;
  251. standardStamp.Author = "";
  252. }
  253. var bytes = CPDFStampAnnotation.GetTempTextStampImage(DynamicStampText[i], standardStamp.StampTextDate,
  254. C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT, DynamicColor[i], out int stampWidth, out int stampHeight, out int width, out int height);
  255. PixelFormat fmt = PixelFormats.Bgra32;
  256. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
  257. standardStamp.Opacity = 1;
  258. standardStamp.SourcePath = "";
  259. standardStamp.StampText = DynamicStampText[i];
  260. standardStamp.MaxWidth = stampWidth;
  261. standardStamp.MaxHeight = stampHeight;
  262. standardStamp.Type = StampType.TEXT_STAMP;
  263. standardStamp.ImageSource = bps;
  264. switch (DynamicColor[i])
  265. {
  266. case C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE:
  267. break;
  268. case C_TEXTSTAMP_COLOR.TEXTSTAMP_RED:
  269. standardStamp.TextColor = TextStampColor.TEXTSTAMP_RED;
  270. break;
  271. case C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN:
  272. standardStamp.TextColor = TextStampColor.TEXTSTAMP_GREEN;
  273. break;
  274. case C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE:
  275. break;
  276. default:
  277. break;
  278. }
  279. DynamicStampList.Add(standardStamp);
  280. }
  281. }
  282. private void ShowDialog()
  283. {
  284. switch (TabControlSelectedIndex)
  285. {
  286. case 1:
  287. ShowDynamicPropertyDialog();
  288. break;
  289. case 2:
  290. ShowCustomCreateDialog();
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. private void ShowDynamicPropertyDialog()
  297. {
  298. bool result = true;
  299. dialogs.ShowDialog(DialogNames.DynamicPropertyDialog, null, e =>
  300. {
  301. if (e.Result != ButtonResult.OK)
  302. {
  303. result = false;
  304. }
  305. DynamicPropertyDialogViewModel DynamicVM = e.Parameters.GetValue<DynamicPropertyDialogViewModel>(ParameterNames.DataModel);
  306. if (DynamicVM != null)
  307. {
  308. Author = DynamicVM.Author;
  309. IsSetAuthor = DynamicVM.IsChecked;
  310. //需要加多语判断
  311. dateType = DynamicVM.SelectedIndex == 0 ? "F" : DynamicVM.DateFormatList[DynamicVM.SelectedIndex] + " HH:mm:ss";
  312. UpDataDynamicStampList();
  313. }
  314. });
  315. if (!result)
  316. {
  317. return;
  318. }
  319. }
  320. private void ShowCustomCreateDialog()
  321. {
  322. bool result = true;
  323. DialogParameters value = new DialogParameters();
  324. value.Add(ParameterNames.PDFViewer, PDFViewer);
  325. dialogs.ShowDialog(DialogNames.CustomCreateDialog, value, e =>
  326. {
  327. if (e.Result != ButtonResult.OK)
  328. {
  329. result = false;
  330. }
  331. CustomCreateDialogViewModel CustomVM = e.Parameters.GetValue<CustomCreateDialogViewModel>(ParameterNames.DataModel);
  332. if (CustomVM != null)
  333. {
  334. }
  335. });
  336. if (!result)
  337. {
  338. return;
  339. }
  340. }
  341. }
  342. }