CPDFCreateStampDialog.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool.Help;
  3. using Compdfkit_Tools.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_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. 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 = 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. 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 = 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 = 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. "NormalText",
  301. "Rectangle(Green)", "Rectangle(Blue)", "Rectangle(Red)",
  302. "Left Arrow Stamp(Green)", "Left Arrow Stamp(Blue)", "Left Arrow Stamp(Red)",
  303. "Right Arrow Stamp(Green)","Right Arrow Stamp(Blue)","Right Arrow Stamp(Red)"
  304. };
  305. var flags = BindingFlags.NonPublic | BindingFlags.Static;
  306. var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
  307. Dpi = (int)dpiProperty.GetValue(null, null);
  308. CustomStampPath = System.IO.Path.Combine(Environment.CurrentDirectory, "ComPDFKit");
  309. CustomStampPath = System.IO.Path.Combine(CustomStampPath, "CustomStamp");
  310. System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(CustomStampPath);
  311. Binding ShapeBoxbinding = new Binding();
  312. ShapeBoxbinding.Source = this;
  313. ShapeBoxbinding.Path = new System.Windows.PropertyPath("ShapeBoxList");
  314. ShapeBox.SetBinding(ListBox.ItemsSourceProperty, ShapeBoxbinding);
  315. PageLoaded = true;
  316. UpTextPreview();
  317. }
  318. private void OpenImage_Click(object sender, RoutedEventArgs e)
  319. {
  320. OpenFileDialog openFile = new OpenFileDialog();
  321. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  322. "(*.gif)|*.gif|" +
  323. "(*.jpeg)|*.jpeg|" +
  324. "(*.jpg)|*.jpg|" +
  325. "(*.png)|*.png|" +
  326. "(*.tiff)|*.tiff";
  327. if (openFile.ShowDialog() == false)
  328. {
  329. return;
  330. }
  331. SaveToImage(openFile.FileName);
  332. }
  333. private void Cancel_Click(object sender, RoutedEventArgs e)
  334. {
  335. this.Close();
  336. }
  337. private void Clear_Click(object sender, RoutedEventArgs e)
  338. {
  339. ShapeBox.SelectedIndex = 0;
  340. cPDFStampData = null;
  341. ImageImage.Source = null;
  342. TextImage.Source = null;
  343. Date.IsChecked = false;
  344. Time.IsChecked = false;
  345. StampText.Text = "Stamp Text";
  346. AddImagebackground.Visibility = Visibility.Visible;
  347. if (CreateHeader.SelectedIndex == 0)
  348. {
  349. SaveBtn.IsEnabled = true;
  350. }
  351. else
  352. {
  353. SaveBtn.IsEnabled = false;
  354. }
  355. UpTextPreview();
  356. }
  357. private void Date_Checked(object sender, RoutedEventArgs e)
  358. {
  359. if (string.IsNullOrEmpty(StampText.Text))
  360. {
  361. SaveBtn.IsEnabled = false;
  362. }
  363. else
  364. {
  365. UpTextPreview();
  366. SaveBtn.IsEnabled = true;
  367. }
  368. }
  369. private void Date_Unchecked(object sender, RoutedEventArgs e)
  370. {
  371. if (string.IsNullOrEmpty(StampText.Text))
  372. {
  373. SaveBtn.IsEnabled = false;
  374. }
  375. else
  376. {
  377. UpTextPreview();
  378. SaveBtn.IsEnabled = true;
  379. }
  380. }
  381. private void Time_Checked(object sender, RoutedEventArgs e)
  382. {
  383. if (string.IsNullOrEmpty(StampText.Text))
  384. {
  385. SaveBtn.IsEnabled = false;
  386. }
  387. else
  388. {
  389. UpTextPreview();
  390. SaveBtn.IsEnabled = true;
  391. }
  392. }
  393. private void Time_Unchecked(object sender, RoutedEventArgs e)
  394. {
  395. if (string.IsNullOrEmpty(StampText.Text))
  396. {
  397. SaveBtn.IsEnabled = false;
  398. }
  399. else
  400. {
  401. UpTextPreview();
  402. SaveBtn.IsEnabled = true;
  403. }
  404. }
  405. private void Grid_Drop(object sender, DragEventArgs e)
  406. {
  407. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  408. foreach (string f in file)
  409. {
  410. string FileType = System.IO.Path.GetExtension(f).Trim().ToLower();
  411. string type = "*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff";
  412. if (!string.IsNullOrEmpty(FileType))
  413. {
  414. string imagetype = "*" + FileType;
  415. string[] x = type.ToLower().Split(';');
  416. List<string> list = x.ToList();
  417. int imageindex = list.IndexOf(imagetype);
  418. if (imageindex > 0)
  419. {
  420. SaveToImage(f);
  421. }
  422. }
  423. }
  424. }
  425. private void ImageImage_SourceUpdated(object sender, DataTransferEventArgs e)
  426. {
  427. if (PageLoaded)
  428. {
  429. if (ImageImage.Source == null)
  430. {
  431. SaveBtn.IsEnabled = false;
  432. }
  433. else
  434. {
  435. SaveBtn.IsEnabled = true;
  436. }
  437. }
  438. }
  439. private void CreateHeader_SelectionChanged(object sender, SelectionChangedEventArgs e)
  440. {
  441. TabControl tabControl = sender as TabControl;
  442. if (PageLoaded && tabControl != null)
  443. {
  444. switch (tabControl.SelectedIndex)
  445. {
  446. case 0:
  447. if (string.IsNullOrEmpty(StampText.Text))
  448. {
  449. SaveBtn.IsEnabled = false;
  450. }
  451. else
  452. {
  453. SaveBtn.IsEnabled = true;
  454. }
  455. break;
  456. case 1:
  457. if (ImageImage.Source == null)
  458. {
  459. SaveBtn.IsEnabled = false;
  460. }
  461. else
  462. {
  463. SaveBtn.IsEnabled = true;
  464. }
  465. break;
  466. default:
  467. break;
  468. }
  469. }
  470. }
  471. }
  472. }