PDFImageEditControl.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. using ComPDFKitViewer.PdfViewer;
  2. using ComPDFKitViewer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using ComPDFKit.PDFPage;
  9. using System.Windows.Controls.Primitives;
  10. using Microsoft.Win32;
  11. using System.Diagnostics;
  12. using System.Drawing;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Windows.Input;
  16. namespace Compdfkit_Tools.Edit
  17. {
  18. public partial class PDFImageEditControl : UserControl
  19. {
  20. public CPDFViewer PDFView { get; private set; }
  21. public PDFEditEvent EditEvent { get; set; }
  22. public List<PDFEditEvent> EditMultiEvents { get; set; }
  23. public PDFImageEditControl()
  24. {
  25. InitializeComponent();
  26. Loaded += PDFImageEditControl_Loaded;
  27. Unloaded += PDFImageEditControl_Unloaded;
  28. }
  29. private void PDFImageEditControl_Unloaded(object sender, RoutedEventArgs e)
  30. {
  31. RotateUI.RotationChanged -= RotateUI_RotationChanged;
  32. FlipUI.FlipChanged -= FlipUI_FlipChanged;
  33. }
  34. public void InitWithPDFViewer(CPDFViewer newPDFView)
  35. {
  36. PDFView = newPDFView;
  37. }
  38. public void SetRotationText(float rotation)
  39. {
  40. RotationTxb.Text = rotation.ToString(CultureInfo.CurrentCulture);
  41. }
  42. public void SetPDFImageEditData(PDFEditEvent newEvent)
  43. {
  44. EditEvent = null;
  45. EditMultiEvents = null;
  46. if(ImageThumbBorder!= null)
  47. {
  48. ImageThumbBorder.Visibility = Visibility.Visible;
  49. }
  50. if (ImageReplaceBtn != null)
  51. {
  52. ImageReplaceBtn.Visibility = Visibility.Visible;
  53. }
  54. if (ImageClipBtn != null)
  55. {
  56. ImageClipBtn.Visibility = Visibility.Visible;
  57. }
  58. if (newEvent != null && newEvent.EditType == CPDFEditType.EditImage)
  59. {
  60. SetImageTransparency(newEvent.Transparency);
  61. }
  62. if(RotationTxb!=null && newEvent!=null && newEvent.EditType == CPDFEditType.EditImage)
  63. {
  64. RotationTxb.Text = newEvent.CurrentRotated.ToString(CultureInfo.CurrentCulture);
  65. }
  66. EditEvent = newEvent;
  67. SetImageThumb();
  68. }
  69. public void SetPDFImageMultiEditData(List<PDFEditEvent> editEvents)
  70. {
  71. EditEvent = null;
  72. EditMultiEvents = null;
  73. if (ImageThumbBorder != null)
  74. {
  75. ImageThumbBorder.Visibility = Visibility.Collapsed;
  76. }
  77. if(ImageReplaceBtn != null)
  78. {
  79. ImageReplaceBtn.Visibility = Visibility.Collapsed;
  80. }
  81. if(ImageClipBtn!=null)
  82. {
  83. ImageClipBtn.Visibility = Visibility.Collapsed;
  84. }
  85. if (ImageThumbUI != null)
  86. {
  87. ImageThumbUI.Source = null;
  88. }
  89. if(RotationTxb!=null)
  90. {
  91. RotationTxb.Text = "";
  92. }
  93. if (editEvents != null && editEvents.Count > 0)
  94. {
  95. PDFEditEvent editEvent = editEvents[0];
  96. if (editEvent != null && editEvent.EditType == CPDFEditType.EditImage)
  97. {
  98. SetImageTransparency(editEvent.Transparency);
  99. }
  100. }
  101. EditMultiEvents = editEvents;
  102. }
  103. private void PDFImageEditControl_Loaded(object sender, RoutedEventArgs e)
  104. {
  105. RotateUI.RotationChanged += RotateUI_RotationChanged;
  106. FlipUI.FlipChanged += FlipUI_FlipChanged;
  107. }
  108. private void FlipUI_FlipChanged(object sender, bool e)
  109. {
  110. if (EditEvent != null)
  111. {
  112. if(e)
  113. {
  114. EditEvent.VerticalMirror = true;
  115. }
  116. else
  117. {
  118. EditEvent.HorizontalMirror = true;
  119. }
  120. EditEvent.UpdatePDFEditByEventArgs();
  121. SetImageThumb();
  122. }
  123. if (EditMultiEvents != null)
  124. {
  125. foreach (PDFEditEvent editEvent in EditMultiEvents)
  126. {
  127. if (e)
  128. {
  129. editEvent.VerticalMirror = true;
  130. }
  131. else
  132. {
  133. editEvent.HorizontalMirror = true;
  134. }
  135. }
  136. PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  137. }
  138. }
  139. private void RotateUI_RotationChanged(object sender, double e)
  140. {
  141. if (EditEvent != null)
  142. {
  143. EditEvent.Rotate = (int)e;
  144. EditEvent.UpdatePDFEditByEventArgs();
  145. SetRotationText(EditEvent.CurrentRotated);
  146. SetImageThumb();
  147. }
  148. if (EditMultiEvents != null)
  149. {
  150. foreach (PDFEditEvent editEvent in EditMultiEvents)
  151. {
  152. editEvent.Rotate = (int)e;
  153. }
  154. PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  155. }
  156. }
  157. private void Slider_DragStarted(object sender, DragStartedEventArgs e)
  158. {
  159. Slider slider = sender as Slider;
  160. if (slider != null)
  161. {
  162. slider.Tag = "false";
  163. }
  164. }
  165. private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
  166. {
  167. Slider slider = sender as Slider;
  168. if (slider != null)
  169. {
  170. slider.Tag = "true";
  171. }
  172. if (EditEvent != null)
  173. {
  174. EditEvent.Transparency = (int)(ImasgeOpacitySlider.Value * 255);
  175. EditEvent.UpdatePDFEditByEventArgs();
  176. SetImageThumb();
  177. }
  178. if (EditMultiEvents != null)
  179. {
  180. foreach (PDFEditEvent editEvent in EditMultiEvents)
  181. {
  182. editEvent.Transparency = (int)(ImasgeOpacitySlider.Value * 255);
  183. }
  184. PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  185. }
  186. }
  187. private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  188. {
  189. Slider slider = sender as Slider;
  190. if(OpacityTextBox != null)
  191. {
  192. OpacityTextBox.Text = string.Format("{0}%", (int)(ImasgeOpacitySlider.Value * 100));
  193. }
  194. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  195. {
  196. return;
  197. }
  198. if (EditEvent != null)
  199. {
  200. EditEvent.Transparency = (int)(ImasgeOpacitySlider.Value * 255);
  201. EditEvent.UpdatePDFEditByEventArgs();
  202. SetImageThumb();
  203. }
  204. if (EditMultiEvents != null)
  205. {
  206. foreach (PDFEditEvent editEvent in EditMultiEvents)
  207. {
  208. editEvent.Transparency = (int)(ImasgeOpacitySlider.Value * 255);
  209. }
  210. PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  211. }
  212. }
  213. private void ImageReplaceBtn_Click(object sender, RoutedEventArgs e)
  214. {
  215. if(EditMultiEvents!=null && EditMultiEvents.Count>1)
  216. {
  217. return;
  218. }
  219. if (EditEvent != null)
  220. {
  221. OpenFileDialog openFileDialog = new OpenFileDialog();
  222. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  223. if (openFileDialog.ShowDialog() == true)
  224. {
  225. EditEvent.ReplaceImagePath = openFileDialog.FileName;
  226. EditEvent.UpdatePDFEditByEventArgs();
  227. // EditEvent = null;
  228. SetImageThumb();
  229. }
  230. }
  231. }
  232. private void ImageExportBtn_Click(object sender, RoutedEventArgs e)
  233. {
  234. if (PDFView != null)
  235. {
  236. Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
  237. if (imageDict != null && imageDict.Count > 0)
  238. {
  239. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  240. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  241. {
  242. string choosePath = folderBrowser.SelectedPath;
  243. string openPath = choosePath;
  244. try
  245. {
  246. foreach (int pageIndex in imageDict.Keys)
  247. {
  248. List<Bitmap> imageList = imageDict[pageIndex];
  249. foreach (Bitmap image in imageList)
  250. {
  251. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  252. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  253. openPath = savePath;
  254. }
  255. }
  256. Process.Start("explorer", "/select,\"" + openPath + "\"");
  257. }
  258. catch (Exception ex)
  259. {
  260. }
  261. }
  262. }
  263. }
  264. }
  265. private void ImageClipBtn_Click(object sender, RoutedEventArgs e)
  266. {
  267. if(EditMultiEvents!=null && EditMultiEvents.Count>1)
  268. {
  269. return;
  270. }
  271. if (EditEvent != null)
  272. {
  273. EditEvent.ClipImage = true;
  274. EditEvent.UpdatePDFEditByEventArgs();
  275. }
  276. }
  277. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  278. {
  279. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  280. if (selectItem != null && selectItem.Content != null)
  281. {
  282. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  283. {
  284. OpacityTextBox.Text = selectItem.Content.ToString();
  285. ImasgeOpacitySlider.Value = newOpacity / 100.0;
  286. }
  287. }
  288. }
  289. public void SetImageThumb()
  290. {
  291. if (PDFView != null && EditEvent!=null)
  292. {
  293. try
  294. {
  295. Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
  296. foreach (int pageIndex in imageDict.Keys)
  297. {
  298. List<Bitmap> imageList = imageDict[pageIndex];
  299. if (imageList.Count > 0)
  300. {
  301. Bitmap bitmapImage = imageList[0];
  302. MemoryStream memoryStream = new MemoryStream();
  303. bitmapImage.Save(memoryStream, bitmapImage.RawFormat);
  304. BitmapImage imageShow = new BitmapImage();
  305. imageShow.BeginInit();
  306. imageShow.StreamSource = memoryStream;
  307. imageShow.EndInit();
  308. ImageThumbUI.Source = imageShow;
  309. break;
  310. }
  311. }
  312. }
  313. catch (Exception ex)
  314. {
  315. }
  316. }
  317. }
  318. public void SetImageTransparency(double transparency)
  319. {
  320. ImasgeOpacitySlider.Value = transparency / 255D;
  321. OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Round(ImasgeOpacitySlider.Value * 100)));
  322. }
  323. private void SetAbsRotation(double absRotation, PDFEditEvent editEvent)
  324. {
  325. absRotation -= editEvent.CurrentRotated;
  326. if (absRotation != 0)
  327. {
  328. editEvent.Rotate = (int)absRotation;
  329. editEvent.UpdatePDFEditByEventArgs();
  330. SetImageThumb();
  331. }
  332. }
  333. private void SetAbsRotation(double absRotation, List<PDFEditEvent> editEvents)
  334. {
  335. foreach (var editEvent in editEvents)
  336. {
  337. SetAbsRotation(absRotation, editEvent);
  338. }
  339. }
  340. private void RotationTxb_LostFocus(object sender, RoutedEventArgs e)
  341. {
  342. if(!double.TryParse(RotationTxb.Text, out double rotation))
  343. {
  344. return;
  345. }
  346. if(EditEvent!=null)
  347. {
  348. SetAbsRotation(rotation, EditEvent);
  349. }
  350. else if(EditMultiEvents != null && EditMultiEvents.Count>0)
  351. {
  352. SetAbsRotation(rotation, EditMultiEvents);
  353. }
  354. }
  355. private void RotationTxb_PreviewKeyDown(object sender, KeyEventArgs e)
  356. {
  357. if (e.Key == Key.Enter)
  358. {
  359. RotationTxb_LostFocus(null, null);
  360. }
  361. }
  362. }
  363. }