PDFImageEditControl.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.PDFPage.Edit;
  5. using ComPDFKit.Tool;
  6. using ComPDFKit.Tool.UndoManger;
  7. using ComPDFKit.Viewer.Helper;
  8. using ComPDFKitViewer;
  9. using Microsoft.Win32;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.Drawing;
  14. using System.Globalization;
  15. using System.IO;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Controls.Primitives;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using ComPDFKit.Tool.DrawTool;
  23. using ComPDFKitViewer.Helper;
  24. using ComPDFKit.Tool.Help;
  25. namespace Compdfkit_Tools.Edit
  26. {
  27. public partial class PDFImageEditControl : UserControl
  28. {
  29. #region property
  30. public CPDFViewerTool ToolView { get; set; }
  31. public ImageEditParam EditEvent { get; set; }
  32. //public List<PDFEditEvent> EditMultiEvents { get; set; }
  33. #endregion
  34. public PDFImageEditControl()
  35. {
  36. InitializeComponent();
  37. Loaded += PDFImageEditControl_Loaded;
  38. Unloaded += PDFImageEditControl_Unloaded;
  39. }
  40. #region Load unload custom control
  41. private void PDFImageEditControl_Loaded(object sender, RoutedEventArgs e)
  42. {
  43. RotateUI.RotationChanged -= RotateUI_RotationChanged;
  44. FlipUI.FlipChanged -= FlipUI_FlipChanged;
  45. RotateUI.RotationChanged += RotateUI_RotationChanged;
  46. FlipUI.FlipChanged += FlipUI_FlipChanged;
  47. ToolView.SelectedDataChanged -= ToolView_SelectedDataChanged;
  48. ToolView.SelectedDataChanged += ToolView_SelectedDataChanged;
  49. }
  50. private void PDFImageEditControl_Unloaded(object sender, RoutedEventArgs e)
  51. {
  52. RotateUI.RotationChanged -= RotateUI_RotationChanged;
  53. FlipUI.FlipChanged -= FlipUI_FlipChanged;
  54. }
  55. #endregion
  56. #region Property changed
  57. private void ToolView_SelectedDataChanged(object sender, SelectedAnnotData e)
  58. {
  59. if (ToolView.GetIsCropMode())
  60. {
  61. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  62. SelectedRect selectedRect = ToolView.GetSelectedRectForEditAreaObject(imageArea);
  63. if (selectedRect != null)
  64. {
  65. Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
  66. double currentZoom = ToolView.GetCPDFViewer().CurrentRenderFrame.ZoomFactor;
  67. Rect rect = selectedRect.GetRect();
  68. Rect maxRect = selectedRect.GetMaxRect();
  69. Rect pdfRect = new Rect((rect.X - maxRect.X) / currentZoom, (rect.Y - maxRect.Y) / currentZoom, rect.Width / currentZoom, rect.Height / currentZoom);
  70. pdfRect = DpiHelper.StandardRectToPDFRect(pdfRect);
  71. CRect newCRect = new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top);
  72. imageArea.CutWithRect(newCRect);
  73. SetImageThumb();
  74. ToolView.UpdateRender(oldRect, imageArea);
  75. editPage.EndEdit();
  76. }
  77. }
  78. }
  79. #endregion
  80. #region Init PDFViewer
  81. public void InitWithPDFViewer(CPDFViewerTool newPDFView)
  82. {
  83. ToolView = newPDFView;
  84. }
  85. #endregion
  86. #region public method
  87. public void SetRotationText(float rotation)
  88. {
  89. RotationTxb.Text = rotation.ToString(CultureInfo.CurrentCulture);
  90. }
  91. #endregion
  92. #region Image Edit
  93. public void SetImageThumb()
  94. {
  95. if (EditEvent != null)
  96. {
  97. try
  98. {
  99. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  100. string path = Path.GetTempPath();
  101. string uuid = Guid.NewGuid().ToString("N");
  102. string imagePath = Path.Combine(path, uuid + ".tmp");
  103. imageArea.ExtractImage(imagePath);
  104. Bitmap bitmapImage = new Bitmap(imagePath);
  105. MemoryStream memoryStream = new MemoryStream();
  106. bitmapImage.Save(memoryStream, bitmapImage.RawFormat);
  107. BitmapImage imageShow = new BitmapImage();
  108. imageShow.BeginInit();
  109. imageShow.StreamSource = memoryStream;
  110. imageShow.EndInit();
  111. ImageThumbUI.Source = imageShow;
  112. }
  113. catch (Exception ex)
  114. {
  115. }
  116. }
  117. }
  118. public void SetImageTransparency(double transparency)
  119. {
  120. ImasgeOpacitySlider.Value = transparency / 255D;
  121. OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Round(ImasgeOpacitySlider.Value * 100)));
  122. }
  123. public void SetPDFImageEditData(ImageEditParam newEvent)
  124. {
  125. if (newEvent.EditIndex < 0)
  126. {
  127. EditEvent = null;
  128. }
  129. else
  130. {
  131. EditEvent = newEvent;
  132. }
  133. if (newEvent != null && newEvent.EditType == CPDFEditType.EditImage)
  134. {
  135. SetImageTransparency(newEvent.Transparency);
  136. }
  137. if (RotationTxb != null && newEvent != null && newEvent.EditType == CPDFEditType.EditImage)
  138. {
  139. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  140. RotationTxb.Text = imageArea.GetRotation().ToString();
  141. //RotationTxb.Text = newEvent.Rotate.ToString(CultureInfo.CurrentCulture);
  142. }
  143. EditEvent = newEvent;
  144. SetImageThumb();
  145. }
  146. #endregion
  147. //public void SetPDFImageMultiEditData(List<PDFEditEvent> editEvents)
  148. //{
  149. // EditEvent = null;
  150. // EditMultiEvents = null;
  151. // if (ImageThumbBorder != null)
  152. // {
  153. // ImageThumbBorder.Visibility = Visibility.Collapsed;
  154. // }
  155. // if(ImageReplaceBtn != null)
  156. // {
  157. // ImageReplaceBtn.Visibility = Visibility.Collapsed;
  158. // }
  159. // if(ImageClipBtn!=null)
  160. // {
  161. // ImageClipBtn.Visibility = Visibility.Collapsed;
  162. // }
  163. // if (ImageThumbUI != null)
  164. // {
  165. // ImageThumbUI.Source = null;
  166. // }
  167. // if (editEvents != null && editEvents.Count > 0)
  168. // {
  169. // PDFEditEvent editEvent = editEvents[0];
  170. // if (editEvent != null && editEvent.EditType == CPDFEditType.EditImage)
  171. // {
  172. // SetImageTransparency(editEvent.Transparency);
  173. // }
  174. // }
  175. // EditMultiEvents = editEvents;
  176. //}
  177. #region Property changed
  178. private void FlipUI_FlipChanged(object sender, bool e)
  179. {
  180. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  181. if (imageArea != null)
  182. {
  183. Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
  184. bool result = false;
  185. if (e)
  186. {
  187. result = imageArea.VerticalMirror();
  188. }
  189. else
  190. {
  191. result = imageArea.HorizontalMirror();
  192. }
  193. if (result)
  194. {
  195. PDFEditHistory editHistory = new PDFEditHistory();
  196. editHistory.EditPage = editPage;
  197. if (pdfPage != null)
  198. {
  199. editHistory.PageIndex = pdfPage.PageIndex;
  200. }
  201. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  202. ToolView.UpdateRender(oldRect, imageArea);
  203. editPage.EndEdit();
  204. }
  205. SetImageThumb();
  206. }
  207. //if (EditMultiEvents != null)
  208. //{
  209. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  210. // {
  211. // if (e)
  212. // {
  213. // editEvent.VerticalMirror = true;
  214. // }
  215. // else
  216. // {
  217. // editEvent.HorizontalMirror = true;
  218. // }
  219. // }
  220. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  221. //}
  222. }
  223. private void RotateUI_RotationChanged(object sender, double e)
  224. {
  225. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  226. if (imageArea != null)
  227. {
  228. Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
  229. if (imageArea.Rotate((int)e))
  230. {
  231. PDFEditHistory editHistory = new PDFEditHistory();
  232. editHistory.EditPage = editPage;
  233. if (pdfPage != null)
  234. {
  235. editHistory.PageIndex = pdfPage.PageIndex;
  236. }
  237. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  238. //SetRotationText(EditEvent.CurrentRotated);
  239. SetImageThumb();
  240. ToolView.UpdateRender(oldRect, imageArea);
  241. editPage.EndEdit();
  242. RotationTxb.Text = imageArea.GetRotation().ToString();
  243. }
  244. }
  245. //if (EditMultiEvents != null)
  246. //{
  247. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  248. // {
  249. // editEvent.Rotate = (int)e;
  250. // }
  251. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  252. //}
  253. }
  254. private void Slider_DragStarted(object sender, DragStartedEventArgs e)
  255. {
  256. Slider slider = sender as Slider;
  257. if (slider != null)
  258. {
  259. slider.Tag = "false";
  260. }
  261. }
  262. private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
  263. {
  264. Slider slider = sender as Slider;
  265. if (slider != null)
  266. {
  267. slider.Tag = "true";
  268. }
  269. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  270. if (imageArea != null)
  271. {
  272. Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
  273. if (imageArea.SetImageTransparency((byte)(ImasgeOpacitySlider.Value * 255)))
  274. {
  275. PDFEditHistory editHistory = new PDFEditHistory();
  276. editHistory.EditPage = editPage;
  277. if (pdfPage != null)
  278. {
  279. editHistory.PageIndex = pdfPage.PageIndex;
  280. }
  281. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  282. SetImageThumb();
  283. ToolView.UpdateRender(oldRect, imageArea);
  284. editPage.EndEdit();
  285. }
  286. }
  287. //if (EditMultiEvents != null)
  288. //{
  289. // foreach (PDFEditEvent editEvent in EditMultiEvents)
  290. // {
  291. // editEvent.Transparency = (int)(ImageOpacitySlider.Value * 255);
  292. // }
  293. // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
  294. //}
  295. }
  296. private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  297. {
  298. Slider slider = sender as Slider;
  299. if (OpacityTextBox != null)
  300. {
  301. OpacityTextBox.Text = string.Format("{0}%", (int)(ImasgeOpacitySlider.Value * 100));
  302. }
  303. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  304. {
  305. return;
  306. }
  307. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  308. if (imageArea != null)
  309. {
  310. Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
  311. if (imageArea.SetImageTransparency((byte)(ImasgeOpacitySlider.Value * 255)))
  312. {
  313. PDFEditHistory editHistory = new PDFEditHistory();
  314. editHistory.EditPage = editPage;
  315. if (pdfPage != null)
  316. {
  317. editHistory.PageIndex = pdfPage.PageIndex;
  318. }
  319. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  320. SetImageThumb();
  321. ToolView.UpdateRender(oldRect, imageArea);
  322. editPage.EndEdit();
  323. }
  324. }
  325. }
  326. private void ImageReplaceBtn_Click(object sender, RoutedEventArgs e)
  327. {
  328. //if(EditMultiEvents!=null && EditMultiEvents.Count>1)
  329. //{
  330. // return;
  331. //}
  332. if (EditEvent != null)
  333. {
  334. OpenFileDialog openFileDialog = new OpenFileDialog();
  335. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  336. if (openFileDialog.ShowDialog() == true)
  337. {
  338. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  339. if (imageArea != null)
  340. {
  341. int imageWidth = 0;
  342. int imageHeight = 0;
  343. byte[] imageData = null;
  344. PDFHelp.ImagePathToByte(openFileDialog.FileName, ref imageData, ref imageWidth, ref imageHeight);
  345. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  346. {
  347. Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
  348. CRect imageRect = imageArea.GetClipRect();
  349. if (imageArea.ReplaceImageArea(imageRect, imageData, imageWidth, imageHeight))
  350. {
  351. PDFEditHistory editHistory = new PDFEditHistory();
  352. editHistory.EditPage = editPage;
  353. if (pdfPage != null)
  354. {
  355. editHistory.PageIndex = pdfPage.PageIndex;
  356. }
  357. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  358. SetImageThumb();
  359. ToolView.UpdateRender(oldRect, imageArea);
  360. editPage.EndEdit();
  361. }
  362. }
  363. }
  364. }
  365. }
  366. }
  367. private void ImageExportBtn_Click(object sender, RoutedEventArgs e)
  368. {
  369. if (ToolView != null)
  370. {
  371. // Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
  372. Dictionary<int, List<Bitmap>> imageDict = new Dictionary<int, List<Bitmap>>();
  373. if (imageDict != null && imageDict.Count > 0)
  374. {
  375. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  376. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  377. {
  378. string choosePath = folderBrowser.SelectedPath;
  379. string openPath = choosePath;
  380. try
  381. {
  382. foreach (int pageIndex in imageDict.Keys)
  383. {
  384. List<Bitmap> imageList = imageDict[pageIndex];
  385. foreach (Bitmap image in imageList)
  386. {
  387. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  388. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  389. openPath = savePath;
  390. }
  391. }
  392. Process.Start("explorer", "/select,\"" + openPath + "\"");
  393. }
  394. catch (Exception ex)
  395. {
  396. }
  397. }
  398. }
  399. }
  400. }
  401. private void ImageClipBtn_Click(object sender, RoutedEventArgs e)
  402. {
  403. ToolView.SetCropMode(!ToolView.GetIsCropMode());
  404. }
  405. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  406. {
  407. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  408. if (selectItem != null && selectItem.Content != null)
  409. {
  410. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  411. {
  412. OpacityTextBox.Text = selectItem.Content.ToString();
  413. ImasgeOpacitySlider.Value = newOpacity / 100.0;
  414. }
  415. }
  416. }
  417. private void GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage)
  418. {
  419. imageArea = null;
  420. editPage = null;
  421. pdfPage = null;
  422. if (ToolView == null || EditEvent == null)
  423. {
  424. return;
  425. }
  426. try
  427. {
  428. CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
  429. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  430. pdfPage = pdfDoc.PageAtIndex(EditEvent.PageIndex);
  431. editPage = pdfPage.GetEditPage();
  432. List<CPDFEditArea> editAreas = editPage.GetEditAreaList();
  433. if (editAreas != null && editAreas.Count > EditEvent.EditIndex)
  434. {
  435. imageArea = editAreas[EditEvent.EditIndex] as CPDFEditImageArea;
  436. }
  437. }
  438. catch (Exception ex)
  439. {
  440. }
  441. }
  442. private void SetAbsRotation(double absRotation)
  443. {
  444. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  445. if (imageArea != null)
  446. {
  447. int rotation = (int)absRotation - imageArea.GetRotation();
  448. Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame());
  449. if (imageArea.Rotate(rotation))
  450. {
  451. PDFEditHistory editHistory = new PDFEditHistory();
  452. editHistory.EditPage = editPage;
  453. if (pdfPage != null)
  454. {
  455. editHistory.PageIndex = pdfPage.PageIndex;
  456. }
  457. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  458. SetImageThumb();
  459. ToolView.UpdateRender(oldRect, imageArea);
  460. editPage.EndEdit();
  461. }
  462. }
  463. }
  464. private void RotationTxb_LostFocus(object sender, RoutedEventArgs e)
  465. {
  466. if (!double.TryParse(RotationTxb.Text, out double rotation))
  467. {
  468. return;
  469. }
  470. SetAbsRotation(rotation);
  471. }
  472. private void RotationTxb_PreviewKeyDown(object sender, KeyEventArgs e)
  473. {
  474. if (e.Key == Key.Enter)
  475. {
  476. RotationTxb_LostFocus(null, null);
  477. }
  478. }
  479. #endregion
  480. }
  481. }