MainWindow.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using ComPDFKit.PDFPage;
  2. using compdfkit_tools.Edit;
  3. using compdfkit_tools.PDFControl;
  4. using ComPDFKitViewer;
  5. using ComPDFKitViewer.PdfViewer;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Runtime.CompilerServices;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Controls.Primitives;
  19. using System.Windows.Data;
  20. using System.Windows.Documents;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. using System.Windows.Navigation;
  25. using System.Windows.Shapes;
  26. using System.Xml.Linq;
  27. namespace edit_ctrl_demo
  28. {
  29. /// <summary>
  30. /// MainWindow.xaml 的交互逻辑
  31. /// </summary>
  32. public partial class MainWindow : Window,INotifyPropertyChanged
  33. {
  34. public bool CanUndo
  35. {
  36. get
  37. {
  38. if (PDFView != null)
  39. {
  40. return PDFView.UndoManager.CanUndo;
  41. }
  42. return false;
  43. }
  44. }
  45. public bool CanRedo
  46. {
  47. get
  48. {
  49. if (PDFView != null)
  50. {
  51. return PDFView.UndoManager.CanRedo;
  52. }
  53. return false;
  54. }
  55. }
  56. private PDFEditEvent lastPDFEditEvent = null;
  57. public MainWindow()
  58. {
  59. InitializeComponent();
  60. Loaded += MainWindow_Loaded;
  61. DataContext = this;
  62. }
  63. public event PropertyChangedEventHandler PropertyChanged;
  64. protected void OnPropertyChanged([CallerMemberName] string name = null)
  65. {
  66. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  67. }
  68. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  69. {
  70. LoadDefaultDocument();
  71. TitleBarTool.OpenFileEvent += TitleBarTool_OpenFileEvent;
  72. PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  73. PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  74. PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  75. }
  76. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  77. {
  78. if (e == null)
  79. {
  80. return;
  81. }
  82. if(e.EditType==CPDFEditType.EditText)
  83. {
  84. e.Handle = true;
  85. PDFEditTextContextMenu(sender,e);
  86. }
  87. if(e.EditType==CPDFEditType.EditImage)
  88. {
  89. e.Handle = true;
  90. PDFEditImageContextMenu(sender, e);
  91. }
  92. }
  93. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  94. {
  95. OnPropertyChanged(e.PropertyName);
  96. }
  97. /// <summary>
  98. /// 文字或图片块选中事件
  99. /// </summary>
  100. private void PDFView_PDFEditActiveHandler(object sender, PDFEditEvent e)
  101. {
  102. lastPDFEditEvent = null;
  103. if (e == null)
  104. {
  105. PDFEditContainer.Child= null;
  106. return;
  107. }
  108. if(Mouse.RightButton==MouseButtonState.Pressed)
  109. {
  110. lastPDFEditEvent = e;
  111. return;
  112. }
  113. if (e.EditType == CPDFEditType.EditText)
  114. {
  115. PDFTextEditControl textEditControl = new PDFTextEditControl();
  116. textEditControl.SetPDFTextEditData(e);
  117. PDFEditContainer.Child = textEditControl;
  118. return;
  119. }
  120. if (e.EditType == CPDFEditType.EditImage)
  121. {
  122. PDFImageEditControl imageEditControl = new PDFImageEditControl();
  123. imageEditControl.InitWithPDFViewer(PDFView);
  124. imageEditControl.SetPDFImageEditData(e);
  125. PDFEditContainer.Child = imageEditControl;
  126. return;
  127. }
  128. }
  129. private void TitleBarTool_OpenFileEvent(object sender, string filePath)
  130. {
  131. ClearPDFEditState();
  132. PDFView?.CloseDocument();
  133. PDFView?.InitDocument(filePath);
  134. PDFView?.Load();
  135. PDFView?.SetMouseMode(MouseModes.PanTool);
  136. }
  137. private void LoadDefaultDocument()
  138. {
  139. string defaultFilePath = "..\\..\\..\\..\\developer_guide_windows.pdf";
  140. PDFView?.InitDocument(defaultFilePath);
  141. PDFView?.Load();
  142. }
  143. /// <summary>
  144. /// 文字编辑模式
  145. /// </summary>
  146. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  147. {
  148. ToggleButton senderBtn=sender as ToggleButton;
  149. if(senderBtn != null)
  150. {
  151. PDFImageEditButton.IsChecked = false;
  152. if (senderBtn.IsChecked == true)
  153. {
  154. PDFView?.SetPDFEditType(CPDFEditType.EditText);
  155. PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  156. PDFView?.SetMouseMode(MouseModes.PDFEdit);
  157. PDFView?.ReloadDocument();
  158. }
  159. else
  160. {
  161. PDFView?.SetPDFEditType(CPDFEditType.None);
  162. PDFView?.SetMouseMode(MouseModes.PanTool);
  163. PDFView?.ReloadDocument();
  164. }
  165. }
  166. }
  167. /// <summary>
  168. /// 图片编辑模式
  169. /// </summary>
  170. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  171. {
  172. ToggleButton senderBtn = sender as ToggleButton;
  173. if (senderBtn != null)
  174. {
  175. PDFTextEditButton.IsChecked= false;
  176. if (senderBtn.IsChecked == true)
  177. {
  178. PDFView?.SetPDFEditType(CPDFEditType.EditImage);
  179. PDFView?.SetMouseMode(MouseModes.PDFEdit);
  180. PDFView?.ReloadDocument();
  181. }
  182. else
  183. {
  184. PDFView?.SetPDFEditType(CPDFEditType.None);
  185. PDFView?.SetMouseMode(MouseModes.PanTool);
  186. PDFView?.ReloadDocument();
  187. }
  188. }
  189. }
  190. private void ClearPDFEditState()
  191. {
  192. PDFTextEditButton.IsChecked = false;
  193. PDFImageEditButton.IsChecked = false;
  194. PDFEditContainer.Child = null;
  195. }
  196. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  197. {
  198. PDFView?.UndoManager?.Undo();
  199. }
  200. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  201. {
  202. PDFView?.UndoManager?.Redo();
  203. }
  204. private void PDFEditTextContextMenu(object sender,PDFEditCommand editCommand)
  205. {
  206. editCommand.PopupMenu = new ContextMenu();
  207. if (lastPDFEditEvent != null)
  208. {
  209. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "复制", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  210. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "剪切", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  211. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "删除", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  212. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  213. MenuItem propertyMenu = new MenuItem();
  214. propertyMenu.Header = "属性";
  215. propertyMenu.Click += (o, p) =>
  216. {
  217. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditText)
  218. {
  219. PDFTextEditControl textEditControl = new PDFTextEditControl();
  220. textEditControl.SetPDFTextEditData(lastPDFEditEvent);
  221. PDFEditContainer.Child = textEditControl;
  222. }
  223. };
  224. editCommand.PopupMenu.Items.Add(propertyMenu);
  225. }
  226. else
  227. {
  228. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  229. if(editCommand.TextAreaCopied)
  230. {
  231. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴样式", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  232. }
  233. }
  234. }
  235. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  236. {
  237. editCommand.PopupMenu = new ContextMenu();
  238. if(lastPDFEditEvent!=null)
  239. {
  240. MenuItem rotateLeftMenu = new MenuItem();
  241. rotateLeftMenu.Header = "左旋转";
  242. rotateLeftMenu.Click += (o, p) =>
  243. {
  244. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  245. {
  246. lastPDFEditEvent.Rotate = -90;
  247. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  248. }
  249. };
  250. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  251. MenuItem rotateRightMenu = new MenuItem();
  252. rotateRightMenu.Header = "右旋转";
  253. rotateRightMenu.Click += (o, p) =>
  254. {
  255. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  256. {
  257. lastPDFEditEvent.Rotate = 90;
  258. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  259. }
  260. };
  261. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  262. MenuItem replaceMenu = new MenuItem();
  263. replaceMenu.Header = "替换";
  264. replaceMenu.Click += (o, p) =>
  265. {
  266. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  267. {
  268. OpenFileDialog openFileDialog = new OpenFileDialog();
  269. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  270. if (openFileDialog.ShowDialog() == true)
  271. {
  272. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  273. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  274. PDFView?.ClearSelectPDFEdit();
  275. }
  276. }
  277. };
  278. editCommand.PopupMenu.Items.Add(replaceMenu);
  279. MenuItem exportMenu = new MenuItem();
  280. exportMenu.Header = "导出";
  281. exportMenu.Click += (o, p) =>
  282. {
  283. if (PDFView != null)
  284. {
  285. Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
  286. if (imageDict != null && imageDict.Count > 0)
  287. {
  288. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  289. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  290. {
  291. string choosePath = folderBrowser.SelectedPath;
  292. string openPath = choosePath;
  293. try
  294. {
  295. foreach (int pageIndex in imageDict.Keys)
  296. {
  297. List<Bitmap> imageList = imageDict[pageIndex];
  298. foreach (Bitmap image in imageList)
  299. {
  300. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  301. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  302. openPath = savePath;
  303. }
  304. }
  305. Process.Start("explorer", "/select,\"" + openPath + "\"");
  306. }
  307. catch (Exception ex)
  308. {
  309. }
  310. }
  311. }
  312. }
  313. };
  314. editCommand.PopupMenu.Items.Add(exportMenu);
  315. MenuItem opacityMenu = new MenuItem();
  316. opacityMenu.Header = "透明度";
  317. editCommand.PopupMenu.Items.Add(opacityMenu);
  318. AppendOpacityMenu(opacityMenu);
  319. MenuItem horizonMirror = new MenuItem();
  320. horizonMirror.Header = "水平镜像";
  321. horizonMirror.Click += (o, p) =>
  322. {
  323. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  324. {
  325. lastPDFEditEvent.HorizontalMirror = true;
  326. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  327. }
  328. };
  329. editCommand.PopupMenu.Items.Add(horizonMirror);
  330. MenuItem verticalMirror = new MenuItem();
  331. verticalMirror.Header = "垂直镜像";
  332. verticalMirror.Click += (o, p) =>
  333. {
  334. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  335. {
  336. lastPDFEditEvent.VerticalMirror = true;
  337. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  338. }
  339. };
  340. editCommand.PopupMenu.Items.Add(verticalMirror);
  341. MenuItem cropMenu = new MenuItem();
  342. cropMenu.Header = "裁剪";
  343. cropMenu.Click += (o, p) =>
  344. {
  345. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  346. {
  347. lastPDFEditEvent.ClipImage = true;
  348. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  349. }
  350. };
  351. editCommand.PopupMenu.Items.Add(cropMenu);
  352. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "复制", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  353. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "剪切", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  354. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "删除", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  355. }
  356. else
  357. {
  358. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  359. }
  360. }
  361. private void AppendOpacityMenu(MenuItem parentMenu)
  362. {
  363. List<int> opacityList = new List<int>()
  364. {
  365. 25,50,75,100
  366. };
  367. foreach(int opacity in opacityList)
  368. {
  369. MenuItem opacityMenu = new MenuItem();
  370. opacityMenu.Header = string.Format("{0}%", opacity);
  371. opacityMenu.Click += (o, p) =>
  372. {
  373. if(lastPDFEditEvent != null && lastPDFEditEvent.EditType==CPDFEditType.EditImage)
  374. {
  375. lastPDFEditEvent.Transparency = (int)(opacity * 255 / 100);
  376. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  377. }
  378. };
  379. parentMenu.Items.Add(opacityMenu);
  380. }
  381. }
  382. }
  383. }