CPDFCreateStampDialog.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. SaveBtn.IsEnabled = true;
  156. }
  157. }
  158. else
  159. {
  160. SaveToPath = "";
  161. }
  162. }
  163. private void UpTextPreview()
  164. {
  165. if (!PageLoaded)
  166. {
  167. return;
  168. }
  169. string date = "";
  170. string dateType = "";
  171. if ((bool)Date.IsChecked)
  172. {
  173. dateType = "yyyy-MM-dd";
  174. }
  175. if ((bool)Time.IsChecked)
  176. {
  177. dateType = dateType + " HH:mm:ss";
  178. }
  179. if (!String.IsNullOrEmpty(dateType))
  180. {
  181. date = DateTime.Now.ToString(dateType);
  182. }
  183. var bytes = CPDFStampAnnotation.GetTempTextStampImage(StampText.Text, date,
  184. Shape, Color, out int stampWidth, out int stampHeight, out int width, out int height);
  185. if (bytes.Length > 0)
  186. {
  187. PixelFormat fmt = PixelFormats.Bgra32;
  188. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
  189. TextImage.Source = bps;
  190. StampTextDate = date;
  191. StampWidth = stampWidth;
  192. StampHeight = stampHeight;
  193. //Type = StampType.TEXT_STAMP;
  194. }
  195. else
  196. {
  197. TextImage.Source = null;
  198. }
  199. }
  200. private void ShapeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  201. {
  202. if (PageLoaded)
  203. {
  204. switch (ShapeBox.SelectedIndex)
  205. {
  206. case 0:
  207. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  208. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_NONE;
  209. break;
  210. case 1:
  211. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  212. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  213. break;
  214. case 2:
  215. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  216. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  217. break;
  218. case 3:
  219. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  220. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  221. break;
  222. case 4:
  223. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  224. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  225. break;
  226. case 5:
  227. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  228. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  229. break;
  230. case 6:
  231. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  232. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  233. break;
  234. case 7:
  235. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  236. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  237. break;
  238. case 8:
  239. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  240. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  241. break;
  242. case 9:
  243. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  244. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  245. break;
  246. default:
  247. break;
  248. }
  249. UpTextPreview();
  250. }
  251. }
  252. private void CretaeStampData(StampType Type)
  253. {
  254. switch (Type)
  255. {
  256. case StampType.UNKNOWN_STAMP:
  257. break;
  258. case StampType.IMAGE_STAMP:
  259. CreateCPDFStampData();
  260. cPDFStampData.Type = Type;
  261. break;
  262. case StampType.TEXT_STAMP:
  263. SaveImageToPath();
  264. CreateCPDFStampData();
  265. cPDFStampData.Type = Type;
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. private void SaveImageToPath()
  272. {
  273. string path = CustomStampPath;
  274. string name = Guid.NewGuid().ToString();
  275. if (!string.IsNullOrEmpty(path) && TextImage.Source != null)
  276. {
  277. BitmapEncoder encoder = new PngBitmapEncoder();
  278. encoder.Frames.Add(BitmapFrame.Create((BitmapSource)TextImage.Source));
  279. path = System.IO.Path.Combine(path, name);
  280. using (FileStream stream = new FileStream(path, FileMode.Create))
  281. {
  282. encoder.Save(stream);
  283. }
  284. SaveToPath = path;
  285. }
  286. else
  287. {
  288. SaveToPath = "";
  289. }
  290. }
  291. private void CreateCPDFStampData()
  292. {
  293. CPDFStampData stamp = new CPDFStampData();
  294. stamp.Opacity = 1;
  295. stamp.SourcePath = SaveToPath;
  296. stamp.StampText = StampText.Text;
  297. stamp.MaxWidth = (int)(double)((StampWidth / 72D * Dpi) * ScaleDpi);
  298. stamp.MaxHeight = (int)(double)((StampHeight / 72D * Dpi) * ScaleDpi);
  299. stamp.StampTextDate = StampTextDate;
  300. stamp.TextColor = (TextStampColor)(int)Color;
  301. stamp.TextSharp = (TextStampSharp)(int)Shape;
  302. stamp.IsCheckedDate = (bool)Date.IsChecked;
  303. stamp.IsCheckedTime = (bool)Time.IsChecked;
  304. stamp.AnnotationType = CPDFAnnotationType.Stamp;
  305. cPDFStampData = stamp;
  306. }
  307. private void Window_Loaded(object sender, RoutedEventArgs e)
  308. {
  309. ShapeBoxList = new ObservableCollection<string>
  310. {
  311. "NormalText",
  312. "Rectangle(Green)", "Rectangle(Blue)", "Rectangle(Red)",
  313. "Left Arrow Stamp(Green)", "Left Arrow Stamp(Blue)", "Left Arrow Stamp(Red)",
  314. "Right Arrow Stamp(Green)","Right Arrow Stamp(Blue)","Right Arrow Stamp(Red)"
  315. };
  316. var flags = BindingFlags.NonPublic | BindingFlags.Static;
  317. var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
  318. Dpi = (int)dpiProperty.GetValue(null, null);
  319. CustomStampPath = System.IO.Path.Combine(Environment.CurrentDirectory, "ComPDFKit");
  320. CustomStampPath = System.IO.Path.Combine(CustomStampPath, "CustomStamp");
  321. System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(CustomStampPath);
  322. Binding ShapeBoxbinding = new Binding();
  323. ShapeBoxbinding.Source = this;
  324. ShapeBoxbinding.Path = new System.Windows.PropertyPath("ShapeBoxList");
  325. ShapeBox.SetBinding(ListBox.ItemsSourceProperty, ShapeBoxbinding);
  326. PageLoaded = true;
  327. UpTextPreview();
  328. }
  329. private void OpenImage_Click(object sender, RoutedEventArgs e)
  330. {
  331. OpenFileDialog openFile = new OpenFileDialog();
  332. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  333. "(*.gif)|*.gif|" +
  334. "(*.jpeg)|*.jpeg|" +
  335. "(*.jpg)|*.jpg|" +
  336. "(*.png)|*.png|" +
  337. "(*.tiff)|*.tiff";
  338. if (openFile.ShowDialog() == false)
  339. {
  340. return;
  341. }
  342. SaveToImage(openFile.FileName);
  343. SaveBtn.IsEnabled = true;
  344. }
  345. private void Cancel_Click(object sender, RoutedEventArgs e)
  346. {
  347. this.Close();
  348. }
  349. private void Clear_Click(object sender, RoutedEventArgs e)
  350. {
  351. ShapeBox.SelectedIndex = 0;
  352. cPDFStampData = null;
  353. ImageImage.Source = null;
  354. TextImage.Source = null;
  355. Date.IsChecked = false;
  356. Time.IsChecked = false;
  357. StampText.Text = "Stamp Text";
  358. AddImageBtn.Visibility = Visibility.Visible;
  359. SaveBtn.IsEnabled = false;
  360. UpTextPreview();
  361. }
  362. private void Date_Checked(object sender, RoutedEventArgs e)
  363. {
  364. if (string.IsNullOrEmpty(StampText.Text))
  365. {
  366. SaveBtn.IsEnabled = false;
  367. }
  368. else
  369. {
  370. UpTextPreview();
  371. SaveBtn.IsEnabled = true;
  372. }
  373. }
  374. private void Date_Unchecked(object sender, RoutedEventArgs e)
  375. {
  376. if (string.IsNullOrEmpty(StampText.Text))
  377. {
  378. SaveBtn.IsEnabled = false;
  379. }
  380. else
  381. {
  382. UpTextPreview();
  383. SaveBtn.IsEnabled = true;
  384. }
  385. }
  386. private void Time_Checked(object sender, RoutedEventArgs e)
  387. {
  388. if (string.IsNullOrEmpty(StampText.Text))
  389. {
  390. SaveBtn.IsEnabled = false;
  391. }
  392. else
  393. {
  394. UpTextPreview();
  395. SaveBtn.IsEnabled = true;
  396. }
  397. }
  398. private void Time_Unchecked(object sender, RoutedEventArgs e)
  399. {
  400. if (string.IsNullOrEmpty(StampText.Text))
  401. {
  402. SaveBtn.IsEnabled = false;
  403. }
  404. else
  405. {
  406. UpTextPreview();
  407. SaveBtn.IsEnabled = true;
  408. }
  409. }
  410. private void Grid_Drop(object sender, DragEventArgs e)
  411. {
  412. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  413. foreach (string f in file)
  414. {
  415. string FileType = System.IO.Path.GetExtension(f).Trim().ToLower();
  416. string type = "*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff";
  417. if (!string.IsNullOrEmpty(FileType))
  418. {
  419. string imagetype = "*" + FileType;
  420. string[] x = type.ToLower().Split(';');
  421. List<string> list = x.ToList();
  422. int imageindex = list.IndexOf(imagetype);
  423. if (imageindex > 0)
  424. {
  425. SaveToImage(f);
  426. }
  427. }
  428. }
  429. }
  430. private void ImageImage_SourceUpdated(object sender, DataTransferEventArgs e)
  431. {
  432. if (PageLoaded)
  433. {
  434. if (ImageImage.Source == null)
  435. {
  436. SaveBtn.IsEnabled = false;
  437. }
  438. else
  439. {
  440. SaveBtn.IsEnabled = true;
  441. }
  442. }
  443. }
  444. private void CreateHeader_SelectionChanged(object sender, SelectionChangedEventArgs e)
  445. {
  446. TabControl tabControl = sender as TabControl;
  447. if (PageLoaded && tabControl != null)
  448. {
  449. switch (tabControl.SelectedIndex)
  450. {
  451. case 0:
  452. if (string.IsNullOrEmpty(StampText.Text))
  453. {
  454. SaveBtn.IsEnabled = false;
  455. }
  456. else
  457. {
  458. SaveBtn.IsEnabled = true;
  459. }
  460. break;
  461. case 1:
  462. if (ImageImage.Source == null)
  463. {
  464. SaveBtn.IsEnabled = false;
  465. }
  466. else
  467. {
  468. SaveBtn.IsEnabled = true;
  469. }
  470. break;
  471. default:
  472. break;
  473. }
  474. }
  475. }
  476. }
  477. }