CPDFCreateStampDialog.xaml.cs 18 KB

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