PDFImageEditControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 ComPDFKitViewer;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Drawing;
  13. using System.IO;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Media;
  18. namespace Compdfkit_Tools.Edit
  19. {
  20. public partial class PDFImageEditControl : UserControl
  21. {
  22. public CPDFViewerTool ToolView { get; set; }
  23. public ImageEditParam EditEvent { get; set; }
  24. public PDFImageEditControl()
  25. {
  26. InitializeComponent();
  27. Loaded += PDFImageEditControl_Loaded;
  28. Unloaded += PDFImageEditControl_Unloaded;
  29. }
  30. private void PDFImageEditControl_Unloaded(object sender, RoutedEventArgs e)
  31. {
  32. RotateUI.RotationChanged -= RotateUI_RotationChanged;
  33. FlipUI.FlipChanged -= FlipUI_FlipChanged;
  34. }
  35. public void InitWithPDFViewer(CPDFViewerTool newPDFView)
  36. {
  37. ToolView = newPDFView;
  38. }
  39. public void SetPDFImageEditData(ImageEditParam newEvent)
  40. {
  41. EditEvent = null;
  42. if (newEvent != null && newEvent.EditType == CPDFEditType.EditImage)
  43. {
  44. SetImageTransparency(newEvent.Transparency);
  45. }
  46. EditEvent = newEvent;
  47. SetImageThumb();
  48. }
  49. private void PDFImageEditControl_Loaded(object sender, RoutedEventArgs e)
  50. {
  51. RotateUI.RotationChanged += RotateUI_RotationChanged;
  52. FlipUI.FlipChanged += FlipUI_FlipChanged;
  53. }
  54. private void FlipUI_FlipChanged(object sender, bool e)
  55. {
  56. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  57. if (imageArea != null)
  58. {
  59. bool result = false;
  60. if (e)
  61. {
  62. result = imageArea.VerticalMirror();
  63. }
  64. else
  65. {
  66. result = imageArea.HorizontalMirror();
  67. }
  68. if (result)
  69. {
  70. PDFEditHistory editHistory = new PDFEditHistory();
  71. editHistory.EditPage = editPage;
  72. if (pdfPage != null)
  73. {
  74. editHistory.PageIndex = pdfPage.PageIndex;
  75. }
  76. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  77. }
  78. SetImageThumb();
  79. }
  80. }
  81. private void RotateUI_RotationChanged(object sender, double e)
  82. {
  83. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  84. if (imageArea != null)
  85. {
  86. if (imageArea.Rotate((float)e))
  87. {
  88. PDFEditHistory editHistory = new PDFEditHistory();
  89. editHistory.EditPage = editPage;
  90. if (pdfPage != null)
  91. {
  92. editHistory.PageIndex = pdfPage.PageIndex;
  93. }
  94. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  95. SetImageThumb();
  96. }
  97. }
  98. }
  99. private void Slider_DragStarted(object sender, DragStartedEventArgs e)
  100. {
  101. Slider slider = sender as Slider;
  102. if (slider != null)
  103. {
  104. slider.Tag = "false";
  105. }
  106. }
  107. private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
  108. {
  109. Slider slider = sender as Slider;
  110. if (slider != null)
  111. {
  112. slider.Tag = "true";
  113. }
  114. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  115. if (imageArea != null)
  116. {
  117. if (imageArea.SetImageTransparency((byte)(ImasgeOpacitySlider.Value * 255)))
  118. {
  119. PDFEditHistory editHistory = new PDFEditHistory();
  120. editHistory.EditPage = editPage;
  121. if (pdfPage != null)
  122. {
  123. editHistory.PageIndex = pdfPage.PageIndex;
  124. }
  125. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  126. SetImageThumb();
  127. }
  128. }
  129. }
  130. private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  131. {
  132. Slider slider = sender as Slider;
  133. if(OpacityTextBox != null)
  134. {
  135. OpacityTextBox.Text = string.Format("{0}%", (int)(ImasgeOpacitySlider.Value * 100));
  136. }
  137. if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
  138. {
  139. return;
  140. }
  141. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  142. if (imageArea != null)
  143. {
  144. if (imageArea.SetImageTransparency((byte)(ImasgeOpacitySlider.Value * 255)))
  145. {
  146. PDFEditHistory editHistory = new PDFEditHistory();
  147. editHistory.EditPage = editPage;
  148. if (pdfPage != null)
  149. {
  150. editHistory.PageIndex = pdfPage.PageIndex;
  151. }
  152. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  153. SetImageThumb();
  154. }
  155. }
  156. }
  157. private void ImageReplaceBtn_Click(object sender, RoutedEventArgs e)
  158. {
  159. if (EditEvent != null)
  160. {
  161. OpenFileDialog openFileDialog = new OpenFileDialog();
  162. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  163. if (openFileDialog.ShowDialog() == true)
  164. {
  165. GetImageArea(out CPDFEditImageArea imageArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
  166. if (imageArea != null)
  167. {
  168. CRect imageRect= imageArea.GetClipRect();
  169. if (imageArea.ReplaceImageArea(imageRect,openFileDialog.FileName,string.Empty))
  170. {
  171. PDFEditHistory editHistory = new PDFEditHistory();
  172. editHistory.EditPage = editPage;
  173. if (pdfPage != null)
  174. {
  175. editHistory.PageIndex = pdfPage.PageIndex;
  176. }
  177. ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
  178. SetImageThumb();
  179. }
  180. }
  181. }
  182. }
  183. }
  184. private void ImageExportBtn_Click(object sender, RoutedEventArgs e)
  185. {
  186. if (ToolView != null)
  187. {
  188. //需要选中图片对象
  189. // Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
  190. Dictionary<int, List<Bitmap>> imageDict=new Dictionary<int, List<Bitmap>>();
  191. if (imageDict != null && imageDict.Count > 0)
  192. {
  193. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  194. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  195. {
  196. string choosePath = folderBrowser.SelectedPath;
  197. string openPath = choosePath;
  198. try
  199. {
  200. foreach (int pageIndex in imageDict.Keys)
  201. {
  202. List<Bitmap> imageList = imageDict[pageIndex];
  203. foreach (Bitmap image in imageList)
  204. {
  205. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  206. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  207. openPath = savePath;
  208. }
  209. }
  210. Process.Start("explorer", "/select,\"" + openPath + "\"");
  211. }
  212. catch (Exception ex)
  213. {
  214. }
  215. }
  216. }
  217. }
  218. }
  219. private void ImageClipBtn_Click(object sender, RoutedEventArgs e)
  220. {
  221. }
  222. private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  223. {
  224. ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
  225. if (selectItem != null && selectItem.Content != null)
  226. {
  227. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
  228. {
  229. OpacityTextBox.Text = selectItem.Content.ToString();
  230. ImasgeOpacitySlider.Value = newOpacity / 100.0;
  231. }
  232. }
  233. }
  234. public void SetImageThumb()
  235. {
  236. //if (PDFView != null && EditEvent!=null)
  237. //{
  238. // try
  239. // {
  240. // Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
  241. // foreach (int pageIndex in imageDict.Keys)
  242. // {
  243. // List<Bitmap> imageList = imageDict[pageIndex];
  244. // if (imageList.Count > 0)
  245. // {
  246. // Bitmap bitmapImage = imageList[0];
  247. // MemoryStream memoryStream = new MemoryStream();
  248. // bitmapImage.Save(memoryStream, bitmapImage.RawFormat);
  249. // BitmapImage imageShow = new BitmapImage();
  250. // imageShow.BeginInit();
  251. // imageShow.StreamSource = memoryStream;
  252. // imageShow.EndInit();
  253. // ImageThumbUI.Source = imageShow;
  254. // break;
  255. // }
  256. // }
  257. // }
  258. // catch (Exception ex)
  259. // {
  260. // }
  261. //}
  262. }
  263. public void SetImageTransparency(double transparency)
  264. {
  265. ImasgeOpacitySlider.Value = transparency / 255D;
  266. OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Round(ImasgeOpacitySlider.Value * 100)));
  267. }
  268. private void GetImageArea(out CPDFEditImageArea imageArea,out CPDFPage pdfPage, out CPDFEditPage editPage)
  269. {
  270. imageArea = null;
  271. editPage = null;
  272. pdfPage=null;
  273. if (ToolView == null || EditEvent == null)
  274. {
  275. return;
  276. }
  277. try
  278. {
  279. CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
  280. CPDFDocument pdfDoc = pdfViewer.GetDocument();
  281. pdfPage = pdfDoc.PageAtIndex(EditEvent.PageIndex);
  282. editPage = pdfPage.GetEditPage();
  283. List<CPDFEditArea> editAreas = editPage.GetEditAreaList();
  284. if (editAreas != null && editAreas.Count > EditEvent.EditIndex)
  285. {
  286. imageArea = editAreas[EditEvent.EditIndex] as CPDFEditImageArea;
  287. }
  288. }
  289. catch (Exception ex)
  290. {
  291. }
  292. }
  293. }
  294. }