StampAnnotPropertyViewModel.cs 18 KB

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