CPDFCreateStampDialog.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool.Help;
  3. using ComPDFKit.Controls.Data;
  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.Controls.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. private 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. C_STAMP_TYPE Type = C_STAMP_TYPE.UNKNOWN_STAMP;
  66. switch (CreateHeader.SelectedIndex)
  67. {
  68. case 0:
  69. Type = C_STAMP_TYPE.TEXT_STAMP;
  70. break;
  71. case 1:
  72. Type = C_STAMP_TYPE.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 = 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 = 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. byte[] bytes = PDFDateHelp.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(C_STAMP_TYPE Type)
  242. {
  243. switch (Type)
  244. {
  245. case C_STAMP_TYPE.UNKNOWN_STAMP:
  246. break;
  247. case C_STAMP_TYPE.IMAGE_STAMP:
  248. CreateCPDFStampData();
  249. cPDFStampData.Type = Type;
  250. break;
  251. case C_STAMP_TYPE.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 = 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 = Color;
  290. stamp.TextSharp = 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 = Path.Combine(Environment.CurrentDirectory, "ComPDFKit");
  315. CustomStampPath = Path.Combine(CustomStampPath, "CustomStamp");
  316. if (!Directory.Exists(CustomStampPath))
  317. {
  318. Directory.CreateDirectory(CustomStampPath);
  319. }
  320. Binding ShapeBoxbinding = new Binding();
  321. ShapeBoxbinding.Source = this;
  322. ShapeBoxbinding.Path = new PropertyPath("ShapeBoxList");
  323. ShapeBox.SetBinding(ItemsControl.ItemsSourceProperty, ShapeBoxbinding);
  324. PageLoaded = true;
  325. UpTextPreview();
  326. }
  327. private void OpenImage_Click(object sender, RoutedEventArgs e)
  328. {
  329. OpenFileDialog openFile = new OpenFileDialog();
  330. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  331. "(*.gif)|*.gif|" +
  332. "(*.jpeg)|*.jpeg|" +
  333. "(*.jpg)|*.jpg|" +
  334. "(*.png)|*.png|" +
  335. "(*.tiff)|*.tiff";
  336. if (openFile.ShowDialog() == false)
  337. {
  338. return;
  339. }
  340. SaveToImage(openFile.FileName);
  341. }
  342. private void Cancel_Click(object sender, RoutedEventArgs e)
  343. {
  344. Close();
  345. }
  346. private void Clear_Click(object sender, RoutedEventArgs e)
  347. {
  348. ShapeBox.SelectedIndex = 0;
  349. cPDFStampData = null;
  350. ImageImage.Source = null;
  351. TextImage.Source = null;
  352. Date.IsChecked = false;
  353. Time.IsChecked = false;
  354. StampText.Text = "Stamp Text";
  355. AddImagebackground.Visibility = Visibility.Visible;
  356. if (CreateHeader.SelectedIndex == 0)
  357. {
  358. SaveBtn.IsEnabled = true;
  359. }
  360. else
  361. {
  362. SaveBtn.IsEnabled = false;
  363. }
  364. UpTextPreview();
  365. }
  366. private void Date_Checked(object sender, RoutedEventArgs e)
  367. {
  368. if (string.IsNullOrEmpty(StampText.Text))
  369. {
  370. SaveBtn.IsEnabled = false;
  371. }
  372. else
  373. {
  374. UpTextPreview();
  375. SaveBtn.IsEnabled = true;
  376. }
  377. }
  378. private void Date_Unchecked(object sender, RoutedEventArgs e)
  379. {
  380. if (string.IsNullOrEmpty(StampText.Text))
  381. {
  382. SaveBtn.IsEnabled = false;
  383. }
  384. else
  385. {
  386. UpTextPreview();
  387. SaveBtn.IsEnabled = true;
  388. }
  389. }
  390. private void Time_Checked(object sender, RoutedEventArgs e)
  391. {
  392. if (string.IsNullOrEmpty(StampText.Text))
  393. {
  394. SaveBtn.IsEnabled = false;
  395. }
  396. else
  397. {
  398. UpTextPreview();
  399. SaveBtn.IsEnabled = true;
  400. }
  401. }
  402. private void Time_Unchecked(object sender, RoutedEventArgs e)
  403. {
  404. if (string.IsNullOrEmpty(StampText.Text))
  405. {
  406. SaveBtn.IsEnabled = false;
  407. }
  408. else
  409. {
  410. UpTextPreview();
  411. SaveBtn.IsEnabled = true;
  412. }
  413. }
  414. private void Grid_Drop(object sender, DragEventArgs e)
  415. {
  416. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  417. foreach (string f in file)
  418. {
  419. string FileType = Path.GetExtension(f).Trim().ToLower();
  420. string type = "*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff";
  421. if (!string.IsNullOrEmpty(FileType))
  422. {
  423. string imagetype = "*" + FileType;
  424. string[] x = type.ToLower().Split(';');
  425. List<string> list = x.ToList();
  426. int imageindex = list.IndexOf(imagetype);
  427. if (imageindex > 0)
  428. {
  429. SaveToImage(f);
  430. }
  431. }
  432. }
  433. }
  434. private void ImageImage_SourceUpdated(object sender, DataTransferEventArgs e)
  435. {
  436. if (PageLoaded)
  437. {
  438. if (ImageImage.Source == null)
  439. {
  440. SaveBtn.IsEnabled = false;
  441. }
  442. else
  443. {
  444. SaveBtn.IsEnabled = true;
  445. }
  446. }
  447. }
  448. private void CreateHeader_SelectionChanged(object sender, SelectionChangedEventArgs e)
  449. {
  450. TabControl tabControl = sender as TabControl;
  451. if (PageLoaded && tabControl != null)
  452. {
  453. switch (tabControl.SelectedIndex)
  454. {
  455. case 0:
  456. if (string.IsNullOrEmpty(StampText.Text))
  457. {
  458. SaveBtn.IsEnabled = false;
  459. }
  460. else
  461. {
  462. SaveBtn.IsEnabled = true;
  463. }
  464. break;
  465. case 1:
  466. if (ImageImage.Source == null)
  467. {
  468. SaveBtn.IsEnabled = false;
  469. }
  470. else
  471. {
  472. SaveBtn.IsEnabled = true;
  473. }
  474. break;
  475. default:
  476. break;
  477. }
  478. }
  479. }
  480. }
  481. }