StampAnnotPropertyViewModel.cs 17 KB

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