CPDFCreateStampDialog.xaml.cs 18 KB

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