MainWindow.xaml.cs 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFPage.Edit;
  4. using Compdfkit_Tools.Edit;
  5. using Compdfkit_Tools.Helper;
  6. using Compdfkit_Tools.PDFControl;
  7. using ComPDFKitViewer;
  8. using ComPDFKitViewer.AnnotEvent;
  9. using ComPDFKitViewer.PdfViewer;
  10. using Microsoft.Win32;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Diagnostics;
  15. using System.Drawing;
  16. using System.IO;
  17. using System.Runtime.CompilerServices;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Controls.Primitives;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. using Point = System.Windows.Point;
  25. namespace ContentEditor
  26. {
  27. public partial class MainWindow : Window, INotifyPropertyChanged
  28. {
  29. #region Property
  30. public bool CanUndo
  31. {
  32. get
  33. {
  34. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  35. {
  36. return pdfViewControl.PDFView.UndoManager.CanUndo;
  37. }
  38. return false;
  39. }
  40. }
  41. public bool CanRedo
  42. {
  43. get
  44. {
  45. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  46. {
  47. return pdfViewControl.PDFView.UndoManager.CanRedo;
  48. }
  49. return false;
  50. }
  51. }
  52. public bool CanSave
  53. {
  54. get
  55. {
  56. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  57. {
  58. return pdfViewControl.PDFView.UndoManager.CanSave;
  59. }
  60. return false;
  61. }
  62. }
  63. /// <summary>
  64. /// The last PDF edit object
  65. /// </summary>
  66. private PDFEditEvent lastPDFEditEvent = null;
  67. private PDFViewControl passwordViewer;
  68. private PDFViewControl pdfViewControl;
  69. private PDFImageEditControl imageEditControl = new PDFImageEditControl();
  70. private PDFTextEditControl textEditControl = new PDFTextEditControl();
  71. private Border empytPanel = new Border();
  72. private UIElement prevPanel = null;
  73. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  74. public event PropertyChangedEventHandler PropertyChanged;
  75. private PDFEditEvent pdfTextCreateParam;
  76. private KeyEventHandler KeyDownHandler;
  77. #endregion
  78. public MainWindow()
  79. {
  80. InitializeComponent();
  81. Loaded += MainWindow_Loaded;
  82. DataContext = this;
  83. }
  84. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  85. {
  86. TextBlock emptyBlock = new TextBlock();
  87. emptyBlock.HorizontalAlignment = HorizontalAlignment.Center;
  88. emptyBlock.VerticalAlignment = VerticalAlignment.Center;
  89. empytPanel.Child = emptyBlock;
  90. empytPanel.Width = 260;
  91. BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search);
  92. LoadDefaultDocument();
  93. }
  94. #region Load document
  95. private void LoadDocument()
  96. {
  97. if (pdfViewControl.PDFView.Document == null)
  98. {
  99. return;
  100. }
  101. pdfViewControl.PDFView?.Load();
  102. PDFGrid.Child = pdfViewControl;
  103. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  104. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  105. PasswordUI.Closed -= PasswordUI_Closed;
  106. PasswordUI.Canceled -= PasswordUI_Canceled;
  107. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  108. PasswordUI.Closed += PasswordUI_Closed;
  109. PasswordUI.Canceled += PasswordUI_Canceled;
  110. PasswordUI.Confirmed += PasswordUI_Confirmed;
  111. pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
  112. pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  113. pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  114. pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  115. pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
  116. pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  117. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  118. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  119. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  120. pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  121. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
  122. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
  123. FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
  124. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  125. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  126. ViewSettingBtn.IsChecked = false;
  127. PropertyContainer.Child = null;
  128. PropertyContainer.Visibility = Visibility.Collapsed;
  129. SetEditMode();
  130. if (KeyDownHandler != null)
  131. {
  132. pdfViewControl.PDFView.RemoveHandler(KeyDownEvent, KeyDownHandler);
  133. }
  134. KeyDownHandler = new KeyEventHandler(PDFView_KeyDown);
  135. pdfViewControl.PDFView.AddHandler(KeyDownEvent, KeyDownHandler, false, true);
  136. }
  137. private void LoadDefaultDocument()
  138. {
  139. string defaultFilePath = "PDF32000_2008.pdf";
  140. pdfViewControl = new PDFViewControl();
  141. pdfViewControl.PDFView.InitDocument(defaultFilePath);
  142. LoadDocument();
  143. }
  144. #endregion
  145. #region Password
  146. private void PasswordUI_Confirmed(object sender, string e)
  147. {
  148. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  149. {
  150. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  151. if (passwordViewer.PDFView.Document.IsLocked == false)
  152. {
  153. PasswordUI.SetShowError("", Visibility.Collapsed);
  154. PasswordUI.ClearPassword();
  155. PasswordUI.Visibility = Visibility.Collapsed;
  156. PopupBorder.Visibility = Visibility.Collapsed;
  157. pdfViewControl.PDFView.Document.Release();
  158. pdfViewControl = passwordViewer;
  159. LoadDocument();
  160. }
  161. else
  162. {
  163. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  164. }
  165. }
  166. }
  167. private void PasswordUI_Canceled(object sender, EventArgs e)
  168. {
  169. PopupBorder.Visibility = Visibility.Collapsed;
  170. PasswordUI.Visibility = Visibility.Collapsed;
  171. }
  172. private void PasswordUI_Closed(object sender, EventArgs e)
  173. {
  174. PopupBorder.Visibility = Visibility.Collapsed;
  175. PasswordUI.Visibility = Visibility.Collapsed;
  176. }
  177. #endregion
  178. #region Expand and collapse Panel
  179. private void ExpandLeftPanel(bool isExpand)
  180. {
  181. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  182. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  183. if (isExpand)
  184. {
  185. BodyGrid.ColumnDefinitions[0].Width = new GridLength(260);
  186. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  187. }
  188. else
  189. {
  190. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  191. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  192. }
  193. }
  194. #endregion
  195. #region Context menu
  196. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  197. {
  198. if (e != null && e.CommandType == CommandType.Context)
  199. {
  200. if (e.PressOnSelectedText)
  201. {
  202. e.Handle = true;
  203. e.PopupMenu = new ContextMenu();
  204. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  205. }
  206. else if (e.CommandTarget == TargetType.ImageSelection)
  207. {
  208. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  209. {
  210. e.Handle = true;
  211. e.PopupMenu = new ContextMenu();
  212. MenuItem imageCopyMenu = new MenuItem();
  213. imageCopyMenu = new MenuItem();
  214. imageCopyMenu.Header = "Copy Images";
  215. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  216. imageCopyMenu.CommandParameter = e;
  217. e.PopupMenu.Items.Add(imageCopyMenu);
  218. MenuItem imageExtraMenu = new MenuItem();
  219. imageExtraMenu = new MenuItem();
  220. imageExtraMenu.Header = "Extract Images";
  221. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  222. imageExtraMenu.CommandParameter = e;
  223. e.PopupMenu.Items.Add(imageExtraMenu);
  224. }
  225. }
  226. else
  227. {
  228. e.Handle = true;
  229. e.PopupMenu = new ContextMenu();
  230. MenuItem fitWidthMenu = new MenuItem();
  231. fitWidthMenu.Header = "Automatically Resize";
  232. fitWidthMenu.Click += (o, p) =>
  233. {
  234. if (pdfViewControl != null)
  235. {
  236. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  237. }
  238. };
  239. e.PopupMenu.Items.Add(fitWidthMenu);
  240. MenuItem fitSizeMenu = new MenuItem();
  241. fitSizeMenu.Header = "Actual Size";
  242. fitSizeMenu.Click += (o, p) =>
  243. {
  244. if (pdfViewControl != null)
  245. {
  246. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  247. }
  248. };
  249. e.PopupMenu.Items.Add(fitSizeMenu);
  250. MenuItem zoomInMenu = new MenuItem();
  251. zoomInMenu.Header = "Zoom In";
  252. zoomInMenu.Click += (o, p) =>
  253. {
  254. if (pdfViewControl != null)
  255. {
  256. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  257. pdfViewControl.PDFView?.Zoom(newZoom);
  258. }
  259. };
  260. e.PopupMenu.Items.Add(zoomInMenu);
  261. MenuItem zoomOutMenu = new MenuItem();
  262. zoomOutMenu.Header = "Zoom Out";
  263. zoomOutMenu.Click += (o, p) =>
  264. {
  265. if (pdfViewControl != null)
  266. {
  267. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  268. pdfViewControl.PDFView?.Zoom(newZoom);
  269. }
  270. };
  271. e.PopupMenu.Items.Add(zoomOutMenu);
  272. e.PopupMenu.Items.Add(new Separator());
  273. MenuItem singleView = new MenuItem();
  274. singleView.Header = "Single Page";
  275. singleView.Click += (o, p) =>
  276. {
  277. if (pdfViewControl != null)
  278. {
  279. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  280. }
  281. };
  282. e.PopupMenu.Items.Add(singleView);
  283. MenuItem singleContinuousView = new MenuItem();
  284. singleContinuousView.Header = "Single Page Continuous";
  285. singleContinuousView.Click += (o, p) =>
  286. {
  287. if (pdfViewControl != null)
  288. {
  289. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  290. }
  291. };
  292. e.PopupMenu.Items.Add(singleContinuousView);
  293. MenuItem doubleView = new MenuItem();
  294. doubleView.Header = "Two Pages";
  295. doubleView.Click += (o, p) =>
  296. {
  297. if (pdfViewControl != null)
  298. {
  299. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  300. }
  301. };
  302. e.PopupMenu.Items.Add(doubleView);
  303. MenuItem doubleContinuousView = new MenuItem();
  304. doubleContinuousView.Header = "Two Pages Continuous";
  305. doubleContinuousView.Click += (o, p) =>
  306. {
  307. if (pdfViewControl != null)
  308. {
  309. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  310. }
  311. };
  312. e.PopupMenu.Items.Add(doubleContinuousView);
  313. }
  314. }
  315. if (e != null && e.CommandType == CommandType.Copy)
  316. {
  317. e.DoCommand();
  318. }
  319. }
  320. private void CopyImage_Click(object sender, RoutedEventArgs e)
  321. {
  322. try
  323. {
  324. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  325. if (imageDict != null && imageDict.Count > 0)
  326. {
  327. foreach (int pageIndex in imageDict.Keys)
  328. {
  329. List<Bitmap> imageList = imageDict[pageIndex];
  330. foreach (Bitmap image in imageList)
  331. {
  332. MemoryStream ms = new MemoryStream();
  333. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  334. BitmapImage imageData = new BitmapImage();
  335. imageData.BeginInit();
  336. imageData.StreamSource = ms;
  337. imageData.CacheOption = BitmapCacheOption.OnLoad;
  338. imageData.EndInit();
  339. imageData.Freeze();
  340. Clipboard.SetImage(imageData);
  341. break;
  342. }
  343. }
  344. }
  345. }
  346. catch (Exception ex)
  347. {
  348. }
  349. }
  350. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  351. {
  352. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  353. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  354. {
  355. string choosePath = folderDialog.SelectedPath;
  356. string openPath = choosePath;
  357. try
  358. {
  359. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  360. if (imageDict != null && imageDict.Count > 0)
  361. {
  362. foreach (int pageIndex in imageDict.Keys)
  363. {
  364. List<Bitmap> imageList = imageDict[pageIndex];
  365. foreach (Bitmap image in imageList)
  366. {
  367. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  368. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  369. openPath = savePath;
  370. }
  371. }
  372. }
  373. Process.Start("explorer", "/select,\"" + openPath + "\"");
  374. }
  375. catch (Exception ex)
  376. {
  377. }
  378. }
  379. }
  380. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  381. {
  382. editCommand.PopupMenu = new ContextMenu();
  383. if (ApplicationCommands.Copy.CanExecute(null, (UIElement)sender) && lastPDFEditEvent != null)
  384. {
  385. MenuItem rotateLeftMenu = new MenuItem();
  386. rotateLeftMenu.Header = "Rotate Left";
  387. rotateLeftMenu.Click += (o, p) =>
  388. {
  389. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  390. {
  391. lastPDFEditEvent.Rotate = -90;
  392. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  393. imageEditControl?.SetImageThumb();
  394. }
  395. };
  396. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  397. MenuItem rotateRightMenu = new MenuItem();
  398. rotateRightMenu.Header = "Rotate Right";
  399. rotateRightMenu.Click += (o, p) =>
  400. {
  401. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  402. {
  403. lastPDFEditEvent.Rotate = 90;
  404. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  405. imageEditControl?.SetImageThumb();
  406. }
  407. };
  408. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  409. MenuItem replaceMenu = new MenuItem();
  410. replaceMenu.Header = "Replace";
  411. replaceMenu.Click += (o, p) =>
  412. {
  413. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  414. {
  415. OpenFileDialog openFileDialog = new OpenFileDialog();
  416. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  417. if (openFileDialog.ShowDialog() == true)
  418. {
  419. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  420. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  421. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  422. }
  423. }
  424. };
  425. editCommand.PopupMenu.Items.Add(replaceMenu);
  426. MenuItem exportMenu = new MenuItem();
  427. exportMenu.Header = "Export";
  428. exportMenu.Click += (o, p) =>
  429. {
  430. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  431. {
  432. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView.GetSelectedImages();
  433. if (imageDict != null && imageDict.Count > 0)
  434. {
  435. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  436. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  437. {
  438. string choosePath = folderBrowser.SelectedPath;
  439. string openPath = choosePath;
  440. try
  441. {
  442. foreach (int pageIndex in imageDict.Keys)
  443. {
  444. List<Bitmap> imageList = imageDict[pageIndex];
  445. foreach (Bitmap image in imageList)
  446. {
  447. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  448. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  449. openPath = savePath;
  450. }
  451. }
  452. Process.Start("explorer", "/select,\"" + openPath + "\"");
  453. }
  454. catch (Exception ex)
  455. {
  456. }
  457. }
  458. }
  459. }
  460. };
  461. editCommand.PopupMenu.Items.Add(exportMenu);
  462. MenuItem opacityMenu = new MenuItem();
  463. opacityMenu.Header = "Opacity";
  464. editCommand.PopupMenu.Items.Add(opacityMenu);
  465. AppendOpacityMenu(opacityMenu);
  466. MenuItem horizonMirror = new MenuItem();
  467. horizonMirror.Header = "Flip horizontal";
  468. horizonMirror.Click += (o, p) =>
  469. {
  470. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  471. {
  472. lastPDFEditEvent.HorizontalMirror = true;
  473. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  474. imageEditControl?.SetImageThumb();
  475. }
  476. };
  477. editCommand.PopupMenu.Items.Add(horizonMirror);
  478. MenuItem verticalMirror = new MenuItem();
  479. verticalMirror.Header = "Flip vertical";
  480. verticalMirror.Click += (o, p) =>
  481. {
  482. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  483. {
  484. lastPDFEditEvent.VerticalMirror = true;
  485. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  486. imageEditControl?.SetImageThumb();
  487. }
  488. };
  489. editCommand.PopupMenu.Items.Add(verticalMirror);
  490. MenuItem cropMenu = new MenuItem();
  491. cropMenu.Header = "Crop";
  492. cropMenu.Click += (o, p) =>
  493. {
  494. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  495. {
  496. lastPDFEditEvent.ClipImage = true;
  497. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  498. }
  499. };
  500. editCommand.PopupMenu.Items.Add(cropMenu);
  501. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  502. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  503. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  504. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  505. if (editCommand.TextAreaCopied)
  506. {
  507. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  508. }
  509. }
  510. else
  511. {
  512. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  513. }
  514. }
  515. private void AppendOpacityMenu(MenuItem parentMenu)
  516. {
  517. List<int> opacityList = new List<int>()
  518. {
  519. 25,50,75,100
  520. };
  521. foreach (int opacity in opacityList)
  522. {
  523. MenuItem opacityMenu = new MenuItem();
  524. opacityMenu.Header = string.Format("{0}%", opacity);
  525. opacityMenu.Click += (o, p) =>
  526. {
  527. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  528. {
  529. lastPDFEditEvent.Transparency = (int)(opacity * 255 / 100D);
  530. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  531. imageEditControl?.SetImageTransparency(lastPDFEditEvent.Transparency);
  532. imageEditControl?.SetImageThumb();
  533. }
  534. };
  535. parentMenu.Items.Add(opacityMenu);
  536. }
  537. }
  538. private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand)
  539. {
  540. editCommand.PopupMenu = new ContextMenu();
  541. if (lastPDFEditEvent != null)
  542. {
  543. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  544. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  545. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  546. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  547. }
  548. else
  549. {
  550. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  551. if (editCommand.TextAreaCopied)
  552. {
  553. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  554. }
  555. }
  556. }
  557. #endregion
  558. #region UI
  559. /// <summary>
  560. /// Check the zoom factor
  561. /// </summary>
  562. private double CheckZoomLevel(double zoom, bool IsGrowth)
  563. {
  564. double standardZoom = 100;
  565. if (zoom <= 0.01)
  566. {
  567. return 0.01;
  568. }
  569. if (zoom >= 10)
  570. {
  571. return 10;
  572. }
  573. zoom *= 100;
  574. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  575. {
  576. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  577. {
  578. standardZoom = zoomLevelList[i + 1];
  579. break;
  580. }
  581. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  582. {
  583. standardZoom = zoomLevelList[i];
  584. break;
  585. }
  586. }
  587. return standardZoom / 100;
  588. }
  589. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  590. {
  591. UIElement pageView = sender as UIElement;
  592. if (pageView != null)
  593. {
  594. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  595. }
  596. SetImageEditProperty();
  597. }
  598. private void ControlRightPanel()
  599. {
  600. if ((bool)ViewSettingBtn.IsChecked)
  601. {
  602. ViewSettingBtn.IsChecked = false;
  603. }
  604. if (RightPanelButton != null)
  605. {
  606. if (RightPanelButton.IsChecked == true)
  607. {
  608. if (PropertyContainer.Child is CPDFDisplaySettingsControl)
  609. {
  610. PropertyContainer.Child = prevPanel;
  611. }
  612. if (PropertyContainer.Child == null)
  613. {
  614. PropertyContainer.Child = empytPanel;
  615. }
  616. PropertyContainer.Visibility = Visibility.Visible;
  617. }
  618. else
  619. {
  620. PropertyContainer.Visibility = Visibility.Collapsed;
  621. }
  622. }
  623. }
  624. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  625. {
  626. ControlRightPanel();
  627. }
  628. private void ControlLeftPanel()
  629. {
  630. if (LeftToolPanelButton != null)
  631. {
  632. bool isExpand = LeftToolPanelButton.IsChecked == true;
  633. ExpandLeftPanel(isExpand);
  634. }
  635. }
  636. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  637. {
  638. ControlLeftPanel();
  639. }
  640. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  641. {
  642. LeftToolPanelButton.IsChecked = true;
  643. ExpandLeftPanel(true);
  644. BotaSideTool.SelectBotaTool(BOTATools.Search);
  645. }
  646. private void ShowViewSettings()
  647. {
  648. if (RightPanelButton != null)
  649. {
  650. RightPanelButton.IsChecked = false;
  651. }
  652. if (ViewSettingBtn != null)
  653. {
  654. if (ViewSettingBtn.IsChecked == true)
  655. {
  656. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  657. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  658. if (ViewComboBox.SelectedIndex == 1)
  659. {
  660. displayPanel.SetVisibilityWhenContentEdit(Visibility.Collapsed);
  661. }
  662. else
  663. {
  664. displayPanel.SetVisibilityWhenContentEdit(Visibility.Visible);
  665. }
  666. prevPanel = PropertyContainer.Child;
  667. PropertyContainer.Child = displayPanel;
  668. PropertyContainer.Visibility = Visibility.Visible;
  669. }
  670. else
  671. {
  672. PropertyContainer.Child = null;
  673. PropertyContainer.Visibility = Visibility.Collapsed;
  674. }
  675. }
  676. }
  677. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  678. {
  679. ShowViewSettings();
  680. }
  681. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  682. {
  683. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1);
  684. }
  685. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  686. {
  687. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1);
  688. }
  689. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  690. {
  691. PasswordUI.Visibility = Visibility.Collapsed;
  692. CPDFInfoControl.Visibility = Visibility.Visible;
  693. CPDFInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  694. CPDFInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  695. CPDFInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  696. PopupBorder.Visibility = Visibility.Visible;
  697. }
  698. private void CPDFInfoControl_CloseInfoEvent(object sender, EventArgs e)
  699. {
  700. PopupBorder.Visibility = Visibility.Collapsed;
  701. }
  702. private void OpenFile()
  703. {
  704. string filePath = CommonHelper.GetFilePathOrEmpty();
  705. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  706. {
  707. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  708. {
  709. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  710. if (oldFilePath.ToLower() == filePath.ToLower())
  711. {
  712. return;
  713. }
  714. }
  715. passwordViewer = new PDFViewControl();
  716. passwordViewer.PDFView.InitDocument(filePath);
  717. if (passwordViewer.PDFView.Document == null)
  718. {
  719. MessageBox.Show("Open File Failed");
  720. return;
  721. }
  722. if (passwordViewer.PDFView.Document.IsLocked)
  723. {
  724. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  725. PasswordUI.ClearPassword();
  726. PopupBorder.Visibility = Visibility.Visible;
  727. PasswordUI.Visibility = Visibility.Visible;
  728. }
  729. else
  730. {
  731. pdfViewControl.PDFView.Document.Release();
  732. pdfViewControl = passwordViewer;
  733. LoadDocument();
  734. }
  735. }
  736. }
  737. private void OpenFile_Click(object sender, RoutedEventArgs e)
  738. {
  739. OpenFile();
  740. }
  741. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  742. {
  743. SaveFile();
  744. }
  745. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  746. {
  747. ToggleButton senderBtn = sender as ToggleButton;
  748. if (senderBtn != null && pdfViewControl != null)
  749. {
  750. ClearPDFEditState(senderBtn);
  751. if (senderBtn.IsChecked == true)
  752. {
  753. PDFEditEvent createParam = new PDFEditEvent();
  754. createParam.EditType = CPDFEditType.EditText;
  755. createParam.IsBold = false;
  756. createParam.IsItalic = false;
  757. createParam.FontSize = 14;
  758. createParam.FontName = "Helvetica";
  759. createParam.FontColor = Colors.Black;
  760. createParam.TextAlign = TextAlignType.AlignLeft;
  761. createParam.Transparency = 255;
  762. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  763. {
  764. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  765. if (pdfDoc.PageCount > 0)
  766. {
  767. CPDFPage pdfPage = pdfDoc.PageAtIndex(0);
  768. CPDFEditPage editPage = pdfPage.GetEditPage();
  769. editPage.BeginEdit(CPDFEditType.EditText);
  770. createParam.SystemFontNameList.AddRange(editPage.GetFontList());
  771. editPage.EndEdit();
  772. }
  773. }
  774. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText);
  775. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  776. pdfViewControl.PDFView?.SetPDFEditParam(createParam);
  777. if (textEditControl == null)
  778. {
  779. textEditControl = new PDFTextEditControl();
  780. }
  781. textEditControl.SetPDFTextEditData(createParam);
  782. PropertyContainer.Child = textEditControl;
  783. PropertyContainer.Visibility = Visibility.Visible;
  784. if ((bool)ViewSettingBtn.IsChecked)
  785. {
  786. ViewSettingBtn.IsChecked = false;
  787. }
  788. RightPanelButton.IsChecked = true;
  789. pdfTextCreateParam = createParam;
  790. }
  791. else
  792. {
  793. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  794. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  795. RightPanelButton.IsChecked = false;
  796. if ((bool)ViewSettingBtn.IsChecked)
  797. {
  798. return;
  799. }
  800. else
  801. {
  802. if (PropertyContainer.Child != null)
  803. {
  804. PropertyContainer.Child = empytPanel;
  805. }
  806. }
  807. PropertyContainer.Visibility = Visibility.Collapsed;
  808. }
  809. }
  810. }
  811. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  812. {
  813. ToggleButton senderBtn = sender as ToggleButton;
  814. if (senderBtn != null && pdfViewControl != null)
  815. {
  816. senderBtn.IsChecked = false;
  817. OpenFileDialog openFileDialog = new OpenFileDialog();
  818. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  819. if (openFileDialog.ShowDialog() == true)
  820. {
  821. ClearPDFEditState(senderBtn);
  822. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  823. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  824. RightPanelButton.IsChecked = false;
  825. if (PropertyContainer.Child != null && !(bool)ViewSettingBtn.IsChecked)
  826. {
  827. PropertyContainer.Child = empytPanel;
  828. PropertyContainer.Visibility = Visibility.Collapsed;
  829. }
  830. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
  831. pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
  832. }
  833. }
  834. }
  835. private void ClearPDFEditState(ToggleButton ignoreBtn = null)
  836. {
  837. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  838. {
  839. PDFTextEditButton,
  840. PDFImageEditButton
  841. };
  842. foreach (ToggleButton item in clearBtnList)
  843. {
  844. if (ignoreBtn == item)
  845. {
  846. continue;
  847. }
  848. item.IsChecked = false;
  849. }
  850. }
  851. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  852. {
  853. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  854. {
  855. pdfViewControl.PDFView.UndoManager?.Undo();
  856. }
  857. }
  858. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  859. {
  860. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  861. {
  862. pdfViewControl.PDFView.UndoManager?.Redo();
  863. }
  864. }
  865. #endregion
  866. #region Property changed
  867. protected void OnPropertyChanged([CallerMemberName] string name = null)
  868. {
  869. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  870. }
  871. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  872. {
  873. OnPropertyChanged(e.PropertyName);
  874. }
  875. #endregion
  876. #region Event handle
  877. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  878. {
  879. if (e == null)
  880. {
  881. return;
  882. }
  883. if (e.EditType == CPDFEditType.EditText)
  884. {
  885. e.Handle = true;
  886. PDFEditTextContextMenu(sender, e);
  887. }
  888. if (e.EditType == CPDFEditType.EditImage)
  889. {
  890. e.Handle = true;
  891. PDFEditImageContextMenu(sender, e);
  892. }
  893. }
  894. private void PDFView_PDFEditActiveHandler(object sender, PDFEditEvent e)
  895. {
  896. PDFEditEvent tempEvent = lastPDFEditEvent;
  897. lastPDFEditEvent = e;
  898. if ((bool)ViewSettingBtn.IsChecked)
  899. {
  900. ViewSettingBtn.IsChecked = false;
  901. RightPanelButton.IsChecked = true;
  902. }
  903. if (e == null)
  904. {
  905. PropertyContainer.Child = empytPanel;
  906. if (pdfTextCreateParam != null && pdfViewControl != null && pdfViewControl.PDFView != null)
  907. {
  908. if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText)
  909. {
  910. if (textEditControl == null)
  911. {
  912. textEditControl = new PDFTextEditControl();
  913. }
  914. textEditControl.SetPDFTextEditData(pdfTextCreateParam);
  915. PropertyContainer.Child = textEditControl;
  916. }
  917. }
  918. return;
  919. }
  920. if (Mouse.RightButton == MouseButtonState.Pressed && PropertyContainer.Visibility != Visibility.Visible)
  921. {
  922. return;
  923. }
  924. if (e.EditType == CPDFEditType.EditText)
  925. {
  926. if (textEditControl == null)
  927. {
  928. textEditControl = new PDFTextEditControl();
  929. }
  930. textEditControl.SetPDFTextEditData(e);
  931. PropertyContainer.Child = textEditControl;
  932. return;
  933. }
  934. if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null)
  935. {
  936. UIElement pageView = sender as UIElement;
  937. if (pageView != null)
  938. {
  939. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  940. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp;
  941. }
  942. if (Mouse.RightButton == MouseButtonState.Pressed)
  943. {
  944. SetImageEditProperty();
  945. }
  946. return;
  947. }
  948. }
  949. #endregion
  950. #region Save file
  951. /// <summary>
  952. /// Save the file to another PDF file.
  953. /// </summary>
  954. public void SaveAsFile()
  955. {
  956. {
  957. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  958. {
  959. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  960. SaveFileDialog saveDialog = new SaveFileDialog();
  961. saveDialog.Filter = "(*.pdf)|*.pdf";
  962. saveDialog.DefaultExt = ".pdf";
  963. saveDialog.OverwritePrompt = true;
  964. if (saveDialog.ShowDialog() == true)
  965. {
  966. pdfDoc.WriteToFilePath(saveDialog.FileName);
  967. }
  968. }
  969. }
  970. }
  971. private void SaveFile()
  972. {
  973. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  974. {
  975. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  976. if (pdfDoc.WriteToLoadedPath())
  977. {
  978. return;
  979. }
  980. SaveFileDialog saveDialog = new SaveFileDialog();
  981. saveDialog.Filter = "(*.pdf)|*.pdf";
  982. saveDialog.DefaultExt = ".pdf";
  983. saveDialog.OverwritePrompt = true;
  984. if (saveDialog.ShowDialog() == true)
  985. {
  986. pdfDoc.WriteToFilePath(saveDialog.FileName);
  987. }
  988. }
  989. }
  990. #endregion
  991. #region Selected changed
  992. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  993. {
  994. if (e.Key == "Zoom")
  995. {
  996. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  997. }
  998. }
  999. private void SetImageEditProperty()
  1000. {
  1001. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  1002. {
  1003. if (imageEditControl == null)
  1004. {
  1005. imageEditControl = new PDFImageEditControl();
  1006. }
  1007. imageEditControl.InitWithPDFViewer(pdfViewControl.PDFView);
  1008. imageEditControl.SetPDFImageEditData(lastPDFEditEvent);
  1009. PropertyContainer.Child = imageEditControl;
  1010. }
  1011. }
  1012. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1013. {
  1014. SetEditMode();
  1015. }
  1016. private void SetEditMode()
  1017. {
  1018. ClearPDFEditState(null);
  1019. if (pdfViewControl != null && ViewComboBox != null)
  1020. {
  1021. if (ViewComboBox.SelectedIndex == 0)
  1022. {
  1023. PDFEditTool.Visibility = Visibility.Collapsed;
  1024. pdfViewControl.PDFView?.SetShowLink(true);
  1025. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
  1026. pdfViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  1027. pdfViewControl.PDFView?.ReloadDocument();
  1028. }
  1029. if (ViewComboBox.SelectedIndex == 1)
  1030. {
  1031. PDFEditTool.Visibility = Visibility.Visible;
  1032. pdfViewControl.PDFView?.SetShowLink(false);
  1033. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  1034. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  1035. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  1036. pdfViewControl.PDFView?.ReloadDocument();
  1037. }
  1038. RightPanelButton.IsChecked = false;
  1039. ViewSettingBtn.IsChecked = false;
  1040. PropertyContainer.Visibility = Visibility.Collapsed;
  1041. if (PropertyContainer != null)
  1042. {
  1043. PropertyContainer.Child = null;
  1044. }
  1045. }
  1046. }
  1047. public void PDFView_KeyDown(object sender, KeyEventArgs e)
  1048. {
  1049. if (pdfViewControl.PDFView.MouseMode != MouseModes.PDFEdit)
  1050. {
  1051. return;
  1052. }
  1053. if (Keyboard.Modifiers == ModifierKeys.Control)
  1054. {
  1055. if (e.Key == Key.Left)
  1056. {
  1057. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, false);
  1058. e.Handled = true;
  1059. }
  1060. if (e.Key == Key.Right)
  1061. {
  1062. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, false);
  1063. e.Handled = true;
  1064. }
  1065. if (e.Key == Key.Up)
  1066. {
  1067. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, false);
  1068. e.Handled = true;
  1069. }
  1070. if (e.Key == Key.Down)
  1071. {
  1072. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, false);
  1073. e.Handled = true;
  1074. }
  1075. }
  1076. if (Keyboard.Modifiers == ModifierKeys.Shift)
  1077. {
  1078. if (e.Key == Key.Left)
  1079. {
  1080. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreCharPlace, true);
  1081. e.Handled = true;
  1082. }
  1083. if (e.Key == Key.Right)
  1084. {
  1085. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextCharPlace, true);
  1086. e.Handled = true;
  1087. }
  1088. if (e.Key == Key.Up)
  1089. {
  1090. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeUpCharPlace, true);
  1091. e.Handled = true;
  1092. }
  1093. if (e.Key == Key.Down)
  1094. {
  1095. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeDownCharPlace, true);
  1096. e.Handled = true;
  1097. }
  1098. }
  1099. if (Keyboard.Modifiers == ModifierKeys.Alt)
  1100. {
  1101. if (e.SystemKey == Key.Up)
  1102. {
  1103. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLocationLineBegin, false);
  1104. e.Handled = true;
  1105. }
  1106. if (e.SystemKey == Key.Down)
  1107. {
  1108. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeLineEnd, false);
  1109. e.Handled = true;
  1110. }
  1111. }
  1112. if (Keyboard.Modifiers == ModifierKeys.None)
  1113. {
  1114. if (e.Key == Key.Left)
  1115. {
  1116. pdfViewControl.PDFView.MoveEditArea(new Point(-5, 0));
  1117. e.Handled = true;
  1118. }
  1119. if (e.Key == Key.Right)
  1120. {
  1121. pdfViewControl.PDFView.MoveEditArea(new Point(5, 0));
  1122. e.Handled = true;
  1123. }
  1124. if (e.Key == Key.Up)
  1125. {
  1126. pdfViewControl.PDFView.MoveEditArea(new Point(0, -5));
  1127. e.Handled = true;
  1128. }
  1129. if (e.Key == Key.Down)
  1130. {
  1131. pdfViewControl.PDFView.MoveEditArea(new Point(0, 5));
  1132. e.Handled = true;
  1133. }
  1134. }
  1135. if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
  1136. {
  1137. if (e.Key == Key.Left)
  1138. {
  1139. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, true);
  1140. e.Handled = true;
  1141. }
  1142. if (e.Key == Key.Right)
  1143. {
  1144. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, true);
  1145. e.Handled = true;
  1146. }
  1147. if (e.Key == Key.Up)
  1148. {
  1149. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, true);
  1150. e.Handled = true;
  1151. }
  1152. if (e.Key == Key.Down)
  1153. {
  1154. pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, true);
  1155. e.Handled = true;
  1156. }
  1157. }
  1158. }
  1159. #endregion
  1160. #region Close window
  1161. protected override void OnClosing(CancelEventArgs e)
  1162. {
  1163. if (pdfViewControl.PDFView.UndoManager.CanSave)
  1164. {
  1165. MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
  1166. if (result == MessageBoxResult.Yes)
  1167. {
  1168. SaveFile();
  1169. }
  1170. else if (result == MessageBoxResult.No)
  1171. {
  1172. }
  1173. else
  1174. {
  1175. e.Cancel = true;
  1176. }
  1177. }
  1178. }
  1179. #endregion
  1180. #region Shortcut
  1181. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  1182. {
  1183. OpenFile();
  1184. }
  1185. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  1186. {
  1187. if (CanSave)
  1188. {
  1189. SaveFile();
  1190. }
  1191. }
  1192. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  1193. {
  1194. SaveAsFile();
  1195. }
  1196. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  1197. {
  1198. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanUndo)
  1199. {
  1200. pdfViewControl.PDFView.UndoManager?.Undo();
  1201. }
  1202. }
  1203. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  1204. {
  1205. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanRedo)
  1206. {
  1207. pdfViewControl.PDFView.UndoManager?.Redo();
  1208. }
  1209. }
  1210. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  1211. {
  1212. LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
  1213. ControlLeftPanel();
  1214. }
  1215. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  1216. {
  1217. RightPanelButton.IsChecked = !RightPanelButton.IsChecked;
  1218. ControlRightPanel();
  1219. }
  1220. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  1221. {
  1222. ExpandLeftPanel(true);
  1223. LeftToolPanelButton.IsChecked = true;
  1224. BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
  1225. }
  1226. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  1227. {
  1228. ExpandLeftPanel(true);
  1229. LeftToolPanelButton.IsChecked = true;
  1230. BotaSideTool.SelectBotaTool(BOTATools.Outline);
  1231. }
  1232. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  1233. {
  1234. ExpandLeftPanel(true);
  1235. LeftToolPanelButton.IsChecked = true;
  1236. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  1237. }
  1238. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  1239. {
  1240. ExpandLeftPanel(true);
  1241. LeftToolPanelButton.IsChecked = true;
  1242. BotaSideTool.SelectBotaTool(BOTATools.Search);
  1243. }
  1244. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  1245. {
  1246. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  1247. pdfViewControl.PDFView?.Zoom(newZoom);
  1248. }
  1249. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  1250. {
  1251. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  1252. pdfViewControl.PDFView?.Zoom(newZoom);
  1253. }
  1254. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  1255. {
  1256. ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
  1257. ShowViewSettings();
  1258. }
  1259. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  1260. {
  1261. if (PopupBorder.Visibility != Visibility.Visible)
  1262. {
  1263. PasswordUI.Visibility = Visibility.Collapsed;
  1264. CPDFInfoControl.Visibility = Visibility.Visible;
  1265. CPDFInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  1266. CPDFInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  1267. CPDFInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  1268. PopupBorder.Visibility = Visibility.Visible;
  1269. }
  1270. else
  1271. {
  1272. CPDFInfoControl.Visibility = Visibility.Collapsed;
  1273. PopupBorder.Visibility = Visibility.Collapsed;
  1274. }
  1275. }
  1276. #endregion
  1277. }
  1278. }