CPDFCreateStampDialog.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. using ComPDFKit.PDFAnnotation;
  2. using Compdfkit_Tools.Data;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
  17. {
  18. public partial class CPDFCreateStampDialog : Window
  19. {
  20. public CPDFStampData cPDFStampData;
  21. private string SaveToPath;
  22. private string StampTextDate;
  23. private static int Dpi;
  24. private string CustomStampPath;
  25. private int StampWidth;
  26. private int StampHeight;
  27. private static float ScaleDpi { get { return (96F / Dpi); } }
  28. private C_TEXTSTAMP_SHAPE Shape;
  29. private C_TEXTSTAMP_COLOR Color;
  30. bool PageLoaded = false;
  31. public ObservableCollection<string> ShapeBoxList { get; set; }
  32. public CPDFCreateStampDialog()
  33. {
  34. InitializeComponent();
  35. }
  36. public void SetCreateHeaderIndex(int index)
  37. {
  38. CreateHeader.SelectedIndex = index;
  39. if (index == 0)
  40. {
  41. SaveBtn.IsEnabled = true;
  42. }
  43. else
  44. {
  45. SaveBtn.IsEnabled = false;
  46. }
  47. }
  48. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  49. {
  50. if (PageLoaded)
  51. {
  52. UpTextPreview();
  53. if (string.IsNullOrEmpty((sender as TextBox).Text))
  54. {
  55. SaveBtn.IsEnabled = false;
  56. }
  57. else
  58. {
  59. SaveBtn.IsEnabled = true;
  60. }
  61. }
  62. }
  63. private void Save_Click(object sender, RoutedEventArgs e)
  64. {
  65. StampType Type = StampType.UNKNOWN_STAMP;
  66. switch (CreateHeader.SelectedIndex)
  67. {
  68. case 0:
  69. Type = StampType.TEXT_STAMP;
  70. break;
  71. case 1:
  72. Type = StampType.IMAGE_STAMP;
  73. break;
  74. default:
  75. break;
  76. }
  77. CretaeStampData(Type);
  78. this.Close();
  79. }
  80. private void SaveToImage(string FilePath)
  81. {
  82. string path = CustomStampPath;
  83. string name = Guid.NewGuid().ToString();
  84. if (!string.IsNullOrEmpty(CustomStampPath))
  85. {
  86. try
  87. {
  88. BitmapImage image = new BitmapImage(new Uri(FilePath));
  89. double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
  90. scale = Math.Min(scale, 1);
  91. string ext = Path.GetExtension(FilePath);
  92. if (ext.ToUpper() == ".PNG")
  93. {
  94. BitmapEncoder encoder = new PngBitmapEncoder();
  95. var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  96. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  97. path = System.IO.Path.Combine(path, name);
  98. using (FileStream stream = new FileStream(path, FileMode.Create))
  99. {
  100. encoder.Save(stream);
  101. }
  102. if (!string.IsNullOrEmpty(SaveToPath))
  103. {
  104. DirectoryInfo CreatedFilePathFolder = new DirectoryInfo(SaveToPath);
  105. if (CreatedFilePathFolder.Exists)
  106. {
  107. Directory.Delete(SaveToPath, true);
  108. }
  109. }
  110. SaveToPath = path;
  111. ImageImage.Source = targetBitmap;
  112. StampWidth = targetBitmap.PixelWidth;
  113. StampHeight = targetBitmap.PixelHeight;
  114. AddImagebackground.Visibility = Visibility.Collapsed;
  115. SaveBtn.IsEnabled = true;
  116. }
  117. else
  118. {
  119. BitmapEncoder encoder = new JpegBitmapEncoder();
  120. TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  121. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  122. path = System.IO.Path.Combine(path, name);
  123. using (FileStream stream = new FileStream(path, FileMode.Create))
  124. {
  125. encoder.Save(stream);
  126. }
  127. if (!string.IsNullOrEmpty(SaveToPath))
  128. {
  129. DirectoryInfo CreatedFilePathFolder = new DirectoryInfo(SaveToPath);
  130. if (CreatedFilePathFolder.Exists)
  131. {
  132. Directory.Delete(SaveToPath, true);
  133. }
  134. }
  135. SaveToPath = path;
  136. ImageImage.Source = targetBitmap;
  137. StampWidth = targetBitmap.PixelWidth;
  138. StampHeight = targetBitmap.PixelHeight;
  139. AddImagebackground.Visibility = Visibility.Collapsed;
  140. SaveBtn.IsEnabled = true;
  141. }
  142. }
  143. catch
  144. {
  145. }
  146. }
  147. else
  148. {
  149. SaveToPath = "";
  150. }
  151. }
  152. private void UpTextPreview()
  153. {
  154. if (!PageLoaded)
  155. {
  156. return;
  157. }
  158. string date = "";
  159. string dateType = "";
  160. if ((bool)Date.IsChecked)
  161. {
  162. dateType = "yyyy-MM-dd";
  163. }
  164. if ((bool)Time.IsChecked)
  165. {
  166. dateType = dateType + " HH:mm:ss";
  167. }
  168. if (!String.IsNullOrEmpty(dateType))
  169. {
  170. date = DateTime.Now.ToString(dateType);
  171. }
  172. var bytes = CPDFStampAnnotation.GetTempTextStampImage(StampText.Text, date,
  173. Shape, Color, out int stampWidth, out int stampHeight, out int width, out int height);
  174. if (bytes.Length > 0)
  175. {
  176. PixelFormat fmt = PixelFormats.Bgra32;
  177. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
  178. TextImage.Source = bps;
  179. StampTextDate = date;
  180. StampWidth = stampWidth;
  181. StampHeight = stampHeight;
  182. //Type = StampType.TEXT_STAMP;
  183. }
  184. else
  185. {
  186. TextImage.Source = null;
  187. }
  188. }
  189. private void ShapeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  190. {
  191. if (PageLoaded)
  192. {
  193. switch (ShapeBox.SelectedIndex)
  194. {
  195. case 0:
  196. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  197. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_NONE;
  198. break;
  199. case 1:
  200. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  201. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  202. break;
  203. case 2:
  204. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  205. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  206. break;
  207. case 3:
  208. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  209. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  210. break;
  211. case 4:
  212. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  213. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  214. break;
  215. case 5:
  216. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  217. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  218. break;
  219. case 6:
  220. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  221. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  222. break;
  223. case 7:
  224. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  225. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  226. break;
  227. case 8:
  228. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  229. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  230. break;
  231. case 9:
  232. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  233. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  234. break;
  235. default:
  236. break;
  237. }
  238. UpTextPreview();
  239. }
  240. }
  241. private void CretaeStampData(StampType Type)
  242. {
  243. switch (Type)
  244. {
  245. case StampType.UNKNOWN_STAMP:
  246. break;
  247. case StampType.IMAGE_STAMP:
  248. CreateCPDFStampData();
  249. cPDFStampData.Type = Type;
  250. break;
  251. case StampType.TEXT_STAMP:
  252. SaveImageToPath();
  253. CreateCPDFStampData();
  254. cPDFStampData.Type = Type;
  255. break;
  256. default:
  257. break;
  258. }
  259. }
  260. private void SaveImageToPath()
  261. {
  262. string path = CustomStampPath;
  263. string name = Guid.NewGuid().ToString();
  264. if (!string.IsNullOrEmpty(path) && TextImage.Source != null)
  265. {
  266. BitmapEncoder encoder = new PngBitmapEncoder();
  267. encoder.Frames.Add(BitmapFrame.Create((BitmapSource)TextImage.Source));
  268. path = System.IO.Path.Combine(path, name);
  269. using (FileStream stream = new FileStream(path, FileMode.Create))
  270. {
  271. encoder.Save(stream);
  272. }
  273. SaveToPath = path;
  274. }
  275. else
  276. {
  277. SaveToPath = "";
  278. }
  279. }
  280. private void CreateCPDFStampData()
  281. {
  282. CPDFStampData stamp = new CPDFStampData();
  283. stamp.Opacity = 1;
  284. stamp.SourcePath = SaveToPath;
  285. stamp.StampText = StampText.Text;
  286. stamp.MaxWidth = (int)(double)((StampWidth / 72D * Dpi) * ScaleDpi);
  287. stamp.MaxHeight = (int)(double)((StampHeight / 72D * Dpi) * ScaleDpi);
  288. stamp.StampTextDate = StampTextDate;
  289. stamp.TextColor = (TextStampColor)(int)Color;
  290. stamp.TextSharp = (TextStampSharp)(int)Shape;
  291. stamp.IsCheckedDate = (bool)Date.IsChecked;
  292. stamp.IsCheckedTime = (bool)Time.IsChecked;
  293. stamp.AnnotationType = CPDFAnnotationType.Stamp;
  294. cPDFStampData = stamp;
  295. }
  296. private void Window_Loaded(object sender, RoutedEventArgs e)
  297. {
  298. ShapeBoxList = new ObservableCollection<string>
  299. {
  300. Helper.LanguageHelper.PropertyPanelManager.GetString("Style_General"),
  301. Helper.LanguageHelper.PropertyPanelManager.GetString("Rec_Green"),
  302. Helper.LanguageHelper.PropertyPanelManager.GetString("Rec_Blue"),
  303. Helper.LanguageHelper.PropertyPanelManager.GetString("Rec_Red"),
  304. Helper.LanguageHelper.PropertyPanelManager.GetString("Left_Green"),
  305. Helper.LanguageHelper.PropertyPanelManager.GetString("Left_Blue"),
  306. Helper.LanguageHelper.PropertyPanelManager.GetString("Left_Red"),
  307. Helper.LanguageHelper.PropertyPanelManager.GetString("Right_Green"),
  308. Helper.LanguageHelper.PropertyPanelManager.GetString("Right_Blue"),
  309. Helper.LanguageHelper.PropertyPanelManager.GetString("Right_Red")
  310. };
  311. var flags = BindingFlags.NonPublic | BindingFlags.Static;
  312. var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
  313. Dpi = (int)dpiProperty.GetValue(null, null);
  314. CustomStampPath = System.IO.Path.Combine(Environment.CurrentDirectory, "ComPDFKit");
  315. CustomStampPath = System.IO.Path.Combine(CustomStampPath, "CustomStamp");
  316. System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(CustomStampPath);
  317. Binding ShapeBoxbinding = new Binding();
  318. ShapeBoxbinding.Source = this;
  319. ShapeBoxbinding.Path = new System.Windows.PropertyPath("ShapeBoxList");
  320. ShapeBox.SetBinding(ListBox.ItemsSourceProperty, ShapeBoxbinding);
  321. PageLoaded = true;
  322. UpTextPreview();
  323. }
  324. private void OpenImage_Click(object sender, RoutedEventArgs e)
  325. {
  326. OpenFileDialog openFile = new OpenFileDialog();
  327. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  328. "(*.gif)|*.gif|" +
  329. "(*.jpeg)|*.jpeg|" +
  330. "(*.jpg)|*.jpg|" +
  331. "(*.png)|*.png|" +
  332. "(*.tiff)|*.tiff";
  333. if (openFile.ShowDialog() == false)
  334. {
  335. return;
  336. }
  337. SaveToImage(openFile.FileName);
  338. }
  339. private void Cancel_Click(object sender, RoutedEventArgs e)
  340. {
  341. this.Close();
  342. }
  343. private void Clear_Click(object sender, RoutedEventArgs e)
  344. {
  345. ShapeBox.SelectedIndex = 0;
  346. cPDFStampData = null;
  347. ImageImage.Source = null;
  348. TextImage.Source = null;
  349. Date.IsChecked = false;
  350. Time.IsChecked = false;
  351. StampText.Text = "Stamp Text";
  352. AddImagebackground.Visibility = Visibility.Visible;
  353. if (CreateHeader.SelectedIndex == 0)
  354. {
  355. SaveBtn.IsEnabled = true;
  356. }
  357. else
  358. {
  359. SaveBtn.IsEnabled = false;
  360. }
  361. UpTextPreview();
  362. }
  363. private void Date_Checked(object sender, RoutedEventArgs e)
  364. {
  365. if (string.IsNullOrEmpty(StampText.Text))
  366. {
  367. SaveBtn.IsEnabled = false;
  368. }
  369. else
  370. {
  371. UpTextPreview();
  372. SaveBtn.IsEnabled = true;
  373. }
  374. }
  375. private void Date_Unchecked(object sender, RoutedEventArgs e)
  376. {
  377. if (string.IsNullOrEmpty(StampText.Text))
  378. {
  379. SaveBtn.IsEnabled = false;
  380. }
  381. else
  382. {
  383. UpTextPreview();
  384. SaveBtn.IsEnabled = true;
  385. }
  386. }
  387. private void Time_Checked(object sender, RoutedEventArgs e)
  388. {
  389. if (string.IsNullOrEmpty(StampText.Text))
  390. {
  391. SaveBtn.IsEnabled = false;
  392. }
  393. else
  394. {
  395. UpTextPreview();
  396. SaveBtn.IsEnabled = true;
  397. }
  398. }
  399. private void Time_Unchecked(object sender, RoutedEventArgs e)
  400. {
  401. if (string.IsNullOrEmpty(StampText.Text))
  402. {
  403. SaveBtn.IsEnabled = false;
  404. }
  405. else
  406. {
  407. UpTextPreview();
  408. SaveBtn.IsEnabled = true;
  409. }
  410. }
  411. private void Grid_Drop(object sender, DragEventArgs e)
  412. {
  413. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  414. foreach (string f in file)
  415. {
  416. string FileType = System.IO.Path.GetExtension(f).Trim().ToLower();
  417. string type = "*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff";
  418. if (!string.IsNullOrEmpty(FileType))
  419. {
  420. string imagetype = "*" + FileType;
  421. string[] x = type.ToLower().Split(';');
  422. List<string> list = x.ToList();
  423. int imageindex = list.IndexOf(imagetype);
  424. if (imageindex > 0)
  425. {
  426. SaveToImage(f);
  427. }
  428. }
  429. }
  430. }
  431. private void ImageImage_SourceUpdated(object sender, DataTransferEventArgs e)
  432. {
  433. if (PageLoaded)
  434. {
  435. if (ImageImage.Source == null)
  436. {
  437. SaveBtn.IsEnabled = false;
  438. }
  439. else
  440. {
  441. SaveBtn.IsEnabled = true;
  442. }
  443. }
  444. }
  445. private void CreateHeader_SelectionChanged(object sender, SelectionChangedEventArgs e)
  446. {
  447. TabControl tabControl = sender as TabControl;
  448. if (PageLoaded && tabControl != null)
  449. {
  450. switch (tabControl.SelectedIndex)
  451. {
  452. case 0:
  453. if (string.IsNullOrEmpty(StampText.Text))
  454. {
  455. SaveBtn.IsEnabled = false;
  456. }
  457. else
  458. {
  459. SaveBtn.IsEnabled = true;
  460. }
  461. break;
  462. case 1:
  463. if (ImageImage.Source == null)
  464. {
  465. SaveBtn.IsEnabled = false;
  466. }
  467. else
  468. {
  469. SaveBtn.IsEnabled = true;
  470. }
  471. break;
  472. default:
  473. break;
  474. }
  475. }
  476. }
  477. }
  478. }