PDFImageEditControl.xaml.cs 25 KB

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