MainWindow.xaml.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.Tool;
  5. using Compdfkit_Tools.Data;
  6. using Compdfkit_Tools.Helper;
  7. using Compdfkit_Tools.PDFControl;
  8. using ComPDFKitViewer;
  9. using ComPDFKitViewer.AnnotEvent;
  10. using ComPDFKitViewer.PdfViewer;
  11. using Microsoft.Win32;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.Diagnostics;
  16. using System.Drawing;
  17. using System.IO;
  18. using System.Runtime.CompilerServices;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Controls.Primitives;
  22. using System.Windows.Input;
  23. using System.Windows.Media.Imaging;
  24. namespace Annotations
  25. {
  26. public partial class MainWindow : Window, INotifyPropertyChanged
  27. {
  28. #region Property
  29. private PDFViewControl passwordViewer;
  30. private PDFViewControl pdfViewControl;
  31. private CPDFAnnotationControl pdfAnnotationControl = null;
  32. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  33. public bool CanSave
  34. {
  35. get
  36. {
  37. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  38. {
  39. return pdfViewControl.PDFView.UndoManager.CanSave;
  40. }
  41. return false;
  42. }
  43. }
  44. public bool CanUndo
  45. {
  46. get
  47. {
  48. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  49. {
  50. return pdfViewControl.PDFView.UndoManager.CanUndo;
  51. }
  52. return false;
  53. }
  54. }
  55. public bool CanRedo
  56. {
  57. get
  58. {
  59. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  60. {
  61. return pdfViewControl.PDFView.UndoManager.CanRedo;
  62. }
  63. return false;
  64. }
  65. }
  66. public event PropertyChangedEventHandler PropertyChanged;
  67. public ICommand CloseTabCommand;
  68. #endregion
  69. public MainWindow()
  70. {
  71. InitializeComponent();
  72. DataContext = this;
  73. }
  74. #region Load document
  75. CPDFViewerTool cPDFViewerTool;
  76. private void LoadDefaultDocument()
  77. {
  78. string defaultFilePath = "PDF32000_2008.pdf";
  79. pdfViewControl = new PDFViewControl();
  80. pdfViewControl.PDFView.InitDocument(defaultFilePath);
  81. cPDFViewerTool = new CPDFViewerTool();
  82. LoadDocument();
  83. cPDFViewerTool.GetCPDFViewer().InitDoc( pdfViewControl.PDFView.Document);
  84. ComPDFKitViewer.ViewModes mode = ComPDFKitViewer.ViewModes.SingleContinuous;
  85. cPDFViewerTool.GetCPDFViewer().SetFitMode(ComPDFKitViewer.FitModes.FitHeight);
  86. cPDFViewerTool.GetCPDFViewer().MouseWheelZoomHandler += MainWindow_MouseWheelZoomHandler;
  87. cPDFViewerTool.GetCPDFViewer().SetViewMode(mode);
  88. cPDFViewerTool.GetCPDFViewer().UpDataRenderFrame();
  89. }
  90. double zoom = 1;
  91. private void MainWindow_MouseWheelZoomHandler(object sender, MouseWheelZoomArgs e)
  92. {
  93. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  94. {
  95. e.IsZoom = true;
  96. cPDFViewerTool.GetCPDFViewer().SetFitMode(ComPDFKitViewer.FitModes.FitZoom);
  97. if (e.WheelBehavior == 1)
  98. {
  99. zoom += 0.1;
  100. }
  101. else
  102. {
  103. zoom -= 0.1;
  104. if (zoom < 0.1)
  105. {
  106. zoom = 0.1;
  107. }
  108. }
  109. cPDFViewerTool.GetCPDFViewer().SetZoom(zoom);
  110. cPDFViewerTool.GetCPDFViewer().UpDataRenderFrame();
  111. }
  112. }
  113. private void LoadDocument()
  114. {
  115. if (pdfViewControl.PDFView.Document == null)
  116. {
  117. return;
  118. }
  119. pdfViewControl.PDFView?.Load();
  120. pdfViewControl.PDFView?.SetShowLink(true);
  121. PDFGrid.Child = cPDFViewerTool;
  122. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  123. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  124. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  125. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  126. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  127. pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  128. pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  129. pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  130. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  131. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  132. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  133. PasswordUI.Closed -= PasswordUI_Closed;
  134. PasswordUI.Canceled -= PasswordUI_Canceled;
  135. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  136. PasswordUI.Closed += PasswordUI_Closed;
  137. PasswordUI.Canceled += PasswordUI_Canceled;
  138. PasswordUI.Confirmed += PasswordUI_Confirmed;
  139. pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  140. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
  141. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
  142. ViewSettingBtn.IsChecked = false;
  143. PropertyContainer.Child = null;
  144. PropertyContainer.Visibility = Visibility.Collapsed;
  145. InitialPDFViewControl(pdfViewControl);
  146. FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
  147. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  148. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  149. }
  150. #endregion
  151. #region Load Unload custom control
  152. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  153. {
  154. pdfAnnotationControl = new CPDFAnnotationControl();
  155. BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
  156. LoadDefaultDocument();
  157. }
  158. private void MainWindow_Closed(object sender, EventArgs e)
  159. {
  160. cPDFViewerTool?.GetCPDFViewer().Dispose();
  161. }
  162. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  163. {
  164. CPDFAnnotationType[] annotationProperties = { CPDFAnnotationType.Highlight, CPDFAnnotationType.Underline, CPDFAnnotationType.Strikeout, CPDFAnnotationType.Squiggly, CPDFAnnotationType.Freehand, CPDFAnnotationType.FreeText, CPDFAnnotationType.Note, CPDFAnnotationType.Circle, CPDFAnnotationType.Square, CPDFAnnotationType.Arrow, CPDFAnnotationType.Line, CPDFAnnotationType.Image, CPDFAnnotationType.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link, CPDFAnnotationType.Audio };
  165. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  166. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  167. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  168. }
  169. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  170. {
  171. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  172. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  173. }
  174. #endregion
  175. #region Annotation
  176. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  177. {
  178. pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView, cPDFViewerTool);
  179. pdfAnnotationControl.AnnotationCancel();
  180. AnnotationBarControl.ClearAllToolState();
  181. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  182. pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  183. }
  184. private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
  185. {
  186. AnnotationBarControl.ClearAllToolState();
  187. }
  188. #endregion
  189. #region Load Document
  190. #endregion
  191. #region Password
  192. private void PasswordUI_Confirmed(object sender, string e)
  193. {
  194. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  195. {
  196. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  197. if (passwordViewer.PDFView.Document.IsLocked == false)
  198. {
  199. PasswordUI.SetShowError("", Visibility.Collapsed);
  200. PasswordUI.ClearPassword();
  201. PasswordUI.Visibility = Visibility.Collapsed;
  202. PopupBorder.Visibility = Visibility.Collapsed;
  203. pdfViewControl = passwordViewer;
  204. LoadDocument();
  205. }
  206. else
  207. {
  208. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  209. }
  210. }
  211. }
  212. private void PasswordUI_Canceled(object sender, EventArgs e)
  213. {
  214. PopupBorder.Visibility = Visibility.Collapsed;
  215. PasswordUI.Visibility = Visibility.Collapsed;
  216. }
  217. private void PasswordUI_Closed(object sender, EventArgs e)
  218. {
  219. PopupBorder.Visibility = Visibility.Collapsed;
  220. PasswordUI.Visibility = Visibility.Collapsed;
  221. }
  222. #endregion
  223. #region Expand and collapse Panel
  224. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  225. {
  226. PropertyContainer.Width = 260;
  227. PropertyContainer.Child = propertytPanel;
  228. PropertyContainer.Visibility = visible;
  229. if (visible == Visibility.Collapsed || visible == Visibility.Hidden)
  230. {
  231. RightPanelButton.IsChecked = false;
  232. }
  233. }
  234. private void ExpandLeftPanel(bool isExpand)
  235. {
  236. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  237. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  238. if (isExpand)
  239. {
  240. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  241. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  242. }
  243. else
  244. {
  245. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  246. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  247. }
  248. }
  249. #endregion
  250. #region Context menu
  251. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  252. {
  253. switch (e.CommandType)
  254. {
  255. case CommandType.Context:
  256. e.Handle = true;
  257. if (e.CommandTarget == TargetType.Annot)
  258. {
  259. e.Handle = true;
  260. e.PopupMenu = new ContextMenu();
  261. if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
  262. {
  263. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  264. MenuItem propertyMenu = new MenuItem();
  265. propertyMenu = new MenuItem();
  266. propertyMenu.Header = "Edit";
  267. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  268. propertyMenu.CommandParameter = e;
  269. e.PopupMenu.Items.Add(propertyMenu);
  270. }
  271. else if (e.PressOnAnnot)
  272. {
  273. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  274. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  275. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  276. }
  277. else if (e.PressOnMedia || e.PressOnSound)
  278. {
  279. e.Handle = true;
  280. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  281. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  282. }
  283. else if (e.PressOnSelectedText)
  284. {
  285. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  286. MenuItem highLightMenu = new MenuItem();
  287. highLightMenu.Header = "HighLight";
  288. highLightMenu.Click += (o, p) =>
  289. {
  290. TextHighlightAnnotArgs highLightArgs = new TextHighlightAnnotArgs();
  291. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  292. if (pdfAnnotationControl != null)
  293. {
  294. highLightArgs.Color = System.Windows.Media.Colors.Red;
  295. highLightArgs.Transparency = 1;
  296. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  297. pdfViewControl.PDFView.SetToolParam(highLightArgs);
  298. pdfViewControl.PDFView.SetMouseMode(oldMode);
  299. }
  300. };
  301. e.PopupMenu.Items.Add(highLightMenu);
  302. MenuItem underlineMenu = new MenuItem();
  303. underlineMenu.Header = "UnderLine";
  304. underlineMenu.Click += (o, p) =>
  305. {
  306. TextUnderlineAnnotArgs underlineArgs = new TextUnderlineAnnotArgs();
  307. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  308. if (pdfAnnotationControl != null)
  309. {
  310. underlineArgs.Color = System.Windows.Media.Colors.Red;
  311. underlineArgs.Transparency = 1;
  312. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  313. pdfViewControl.PDFView.SetToolParam(underlineArgs);
  314. pdfViewControl.PDFView.SetMouseMode(oldMode);
  315. }
  316. };
  317. e.PopupMenu.Items.Add(underlineMenu);
  318. MenuItem strikeOutMenu = new MenuItem();
  319. strikeOutMenu.Header = "StrikeOut";
  320. strikeOutMenu.Click += (o, p) =>
  321. {
  322. TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
  323. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  324. if (pdfAnnotationControl != null)
  325. {
  326. strikeoutAnnotArgs.Color = System.Windows.Media.Colors.Red;
  327. strikeoutAnnotArgs.Transparency = 1;
  328. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  329. pdfViewControl.PDFView.SetToolParam(strikeoutAnnotArgs);
  330. pdfViewControl.PDFView.SetMouseMode(oldMode);
  331. }
  332. };
  333. e.PopupMenu.Items.Add(strikeOutMenu);
  334. MenuItem SquiggleMenu = new MenuItem();
  335. SquiggleMenu.Header = "Squiggle";
  336. SquiggleMenu.Click += (o, p) =>
  337. {
  338. TextSquigglyAnnotArgs squigglyAnnotArgs = new TextSquigglyAnnotArgs();
  339. MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
  340. if (pdfAnnotationControl != null)
  341. {
  342. squigglyAnnotArgs.Color = System.Windows.Media.Colors.Red;
  343. squigglyAnnotArgs.Transparency = 1;
  344. pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  345. pdfViewControl.PDFView.SetToolParam(squigglyAnnotArgs);
  346. pdfViewControl.PDFView.SetMouseMode(oldMode);
  347. }
  348. };
  349. e.PopupMenu.Items.Add(SquiggleMenu);
  350. }
  351. else
  352. {
  353. e.Handle = true;
  354. e.PopupMenu = new ContextMenu();
  355. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  356. e.PopupMenu.Items.Add(new Separator());
  357. MenuItem fitWidthMenu = new MenuItem();
  358. fitWidthMenu.Header = "Automatically Resize";
  359. fitWidthMenu.Click += (o, p) =>
  360. {
  361. if (pdfViewControl != null)
  362. {
  363. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  364. }
  365. };
  366. e.PopupMenu.Items.Add(fitWidthMenu);
  367. MenuItem fitSizeMenu = new MenuItem();
  368. fitSizeMenu.Header = "Actual Size";
  369. fitSizeMenu.Click += (o, p) =>
  370. {
  371. if (pdfViewControl != null)
  372. {
  373. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  374. }
  375. };
  376. e.PopupMenu.Items.Add(fitSizeMenu);
  377. MenuItem zoomInMenu = new MenuItem();
  378. zoomInMenu.Header = "Zoom In";
  379. zoomInMenu.Click += (o, p) =>
  380. {
  381. if (pdfViewControl != null)
  382. {
  383. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  384. pdfViewControl.PDFView?.Zoom(newZoom);
  385. }
  386. };
  387. e.PopupMenu.Items.Add(zoomInMenu);
  388. MenuItem zoomOutMenu = new MenuItem();
  389. zoomOutMenu.Header = "Zoom Out";
  390. zoomOutMenu.Click += (o, p) =>
  391. {
  392. if (pdfViewControl != null)
  393. {
  394. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  395. pdfViewControl.PDFView?.Zoom(newZoom);
  396. }
  397. };
  398. e.PopupMenu.Items.Add(zoomOutMenu);
  399. e.PopupMenu.Items.Add(new Separator());
  400. MenuItem singleView = new MenuItem();
  401. singleView.Header = "Single Page";
  402. singleView.Click += (o, p) =>
  403. {
  404. if (pdfViewControl != null)
  405. {
  406. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  407. }
  408. };
  409. e.PopupMenu.Items.Add(singleView);
  410. MenuItem singleContinuousView = new MenuItem();
  411. singleContinuousView.Header = "Single Page Continuous";
  412. singleContinuousView.Click += (o, p) =>
  413. {
  414. if (pdfViewControl != null)
  415. {
  416. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  417. }
  418. };
  419. e.PopupMenu.Items.Add(singleContinuousView);
  420. MenuItem doubleView = new MenuItem();
  421. doubleView.Header = "Two Pages";
  422. doubleView.Click += (o, p) =>
  423. {
  424. if (pdfViewControl != null)
  425. {
  426. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  427. }
  428. };
  429. e.PopupMenu.Items.Add(doubleView);
  430. MenuItem doubleContinuousView = new MenuItem();
  431. doubleContinuousView.Header = "Two Pages Continuous";
  432. doubleContinuousView.Click += (o, p) =>
  433. {
  434. if (pdfViewControl != null)
  435. {
  436. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  437. }
  438. };
  439. e.PopupMenu.Items.Add(doubleContinuousView);
  440. }
  441. }
  442. else if (e.CommandTarget == TargetType.ImageSelection)
  443. {
  444. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  445. {
  446. e.Handle = true;
  447. e.PopupMenu = new ContextMenu();
  448. MenuItem imageCopyMenu = new MenuItem();
  449. imageCopyMenu = new MenuItem();
  450. imageCopyMenu.Header = "Copy Images";
  451. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  452. imageCopyMenu.CommandParameter = e;
  453. e.PopupMenu.Items.Add(imageCopyMenu);
  454. MenuItem imageExtraMenu = new MenuItem();
  455. imageExtraMenu = new MenuItem();
  456. imageExtraMenu.Header = "Extract Images";
  457. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  458. imageExtraMenu.CommandParameter = e;
  459. e.PopupMenu.Items.Add(imageExtraMenu);
  460. }
  461. }
  462. break;
  463. case CommandType.Copy:
  464. e.DoCommand();
  465. break;
  466. case CommandType.Cut:
  467. case CommandType.Paste:
  468. case CommandType.Delete:
  469. e.DoCommand();
  470. break;
  471. default:
  472. break;
  473. }
  474. }
  475. private void CopyImage_Click(object sender, RoutedEventArgs e)
  476. {
  477. try
  478. {
  479. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  480. if (imageDict != null && imageDict.Count > 0)
  481. {
  482. foreach (int pageIndex in imageDict.Keys)
  483. {
  484. List<Bitmap> imageList = imageDict[pageIndex];
  485. foreach (Bitmap image in imageList)
  486. {
  487. MemoryStream ms = new MemoryStream();
  488. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  489. BitmapImage imageData = new BitmapImage();
  490. imageData.BeginInit();
  491. imageData.StreamSource = ms;
  492. imageData.CacheOption = BitmapCacheOption.OnLoad;
  493. imageData.EndInit();
  494. imageData.Freeze();
  495. Clipboard.SetImage(imageData);
  496. break;
  497. }
  498. }
  499. }
  500. }
  501. catch (Exception ex)
  502. {
  503. }
  504. }
  505. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  506. {
  507. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  508. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  509. {
  510. string choosePath = folderDialog.SelectedPath;
  511. string openPath = choosePath;
  512. try
  513. {
  514. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  515. if (imageDict != null && imageDict.Count > 0)
  516. {
  517. foreach (int pageIndex in imageDict.Keys)
  518. {
  519. List<Bitmap> imageList = imageDict[pageIndex];
  520. foreach (Bitmap image in imageList)
  521. {
  522. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  523. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  524. openPath = savePath;
  525. }
  526. }
  527. }
  528. Process.Start("explorer", "/select,\"" + openPath + "\"");
  529. }
  530. catch (Exception ex)
  531. {
  532. }
  533. }
  534. }
  535. #endregion
  536. #region UI
  537. private double CheckZoomLevel(double zoom, bool IsGrowth)
  538. {
  539. double standardZoom = 100;
  540. if (zoom <= 0.01)
  541. {
  542. return 0.01;
  543. }
  544. if (zoom >= 10)
  545. {
  546. return 10;
  547. }
  548. zoom *= 100;
  549. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  550. {
  551. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  552. {
  553. standardZoom = zoomLevelList[i + 1];
  554. break;
  555. }
  556. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  557. {
  558. standardZoom = zoomLevelList[i];
  559. break;
  560. }
  561. }
  562. return standardZoom / 100;
  563. }
  564. private void ControlLeftPanel()
  565. {
  566. if (LeftToolPanelButton != null)
  567. {
  568. bool isExpand = LeftToolPanelButton.IsChecked == true;
  569. ExpandLeftPanel(isExpand);
  570. }
  571. }
  572. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  573. {
  574. ControlLeftPanel();
  575. }
  576. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  577. {
  578. ExpandLeftPanel(true);
  579. BotaSideTool.SelectBotaTool(BOTATools.Search);
  580. }
  581. private void ShowViewSettings()
  582. {
  583. if (ViewSettingBtn != null)
  584. {
  585. if (ViewSettingBtn.IsChecked == true)
  586. {
  587. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  588. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  589. displayPanel.InitWithPDFViewerTool(cPDFViewerTool);
  590. PropertyContainer.Child = displayPanel;
  591. PropertyContainer.Visibility = Visibility.Visible;
  592. if ((bool)RightPanelButton.IsChecked)
  593. {
  594. RightPanelButton.IsChecked = false;
  595. }
  596. }
  597. else
  598. {
  599. PropertyContainer.Child = null;
  600. PropertyContainer.Visibility = Visibility.Collapsed;
  601. }
  602. }
  603. }
  604. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  605. {
  606. ShowViewSettings();
  607. }
  608. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  609. {
  610. PasswordUI.Visibility = Visibility.Collapsed;
  611. FileInfoUI.Visibility = Visibility.Visible;
  612. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  613. PopupBorder.Visibility = Visibility.Visible;
  614. }
  615. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  616. {
  617. PopupBorder.Visibility = Visibility.Collapsed;
  618. }
  619. private void OpenFile()
  620. {
  621. string filePath = CommonHelper.GetFilePathOrEmpty();
  622. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  623. {
  624. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  625. {
  626. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  627. if (oldFilePath.ToLower() == filePath.ToLower())
  628. {
  629. return;
  630. }
  631. }
  632. passwordViewer = new PDFViewControl();
  633. passwordViewer.PDFView.InitDocument(filePath);
  634. if (passwordViewer.PDFView.Document == null)
  635. {
  636. MessageBox.Show("Open File Failed");
  637. return;
  638. }
  639. if (passwordViewer.PDFView.Document.IsLocked)
  640. {
  641. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  642. PasswordUI.ClearPassword();
  643. PopupBorder.Visibility = Visibility.Visible;
  644. PasswordUI.Visibility = Visibility.Visible;
  645. }
  646. else
  647. {
  648. pdfViewControl = passwordViewer;
  649. LoadDocument();
  650. }
  651. }
  652. }
  653. private void OpenFile_Click(object sender, RoutedEventArgs e)
  654. {
  655. OpenFile();
  656. }
  657. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  658. {
  659. SaveFile();
  660. pdfViewControl.PDFView.UndoManager.CanSave = false;
  661. }
  662. private void ControlRightPanel()
  663. {
  664. if (RightPanelButton != null)
  665. {
  666. if (RightPanelButton.IsChecked == true)
  667. {
  668. if (pdfAnnotationControl != null)
  669. {
  670. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  671. if ((bool)ViewSettingBtn.IsChecked)
  672. {
  673. ViewSettingBtn.IsChecked = false;
  674. }
  675. }
  676. }
  677. else
  678. {
  679. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  680. }
  681. }
  682. }
  683. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  684. {
  685. ControlRightPanel();
  686. }
  687. private void EditLink_Click(object sender, RoutedEventArgs e)
  688. {
  689. PropertyContainer.Visibility = Visibility.Visible;
  690. }
  691. private void UndoButton_Click(object sender, RoutedEventArgs e)
  692. {
  693. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  694. {
  695. pdfViewControl.PDFView.UndoManager?.Undo();
  696. }
  697. }
  698. private void RedoButton_Click(object sender, RoutedEventArgs e)
  699. {
  700. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  701. {
  702. pdfViewControl.PDFView.UndoManager?.Redo();
  703. }
  704. }
  705. #endregion
  706. #region Property changed
  707. protected void OnPropertyChanged([CallerMemberName] string name = null)
  708. {
  709. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  710. }
  711. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  712. {
  713. OnPropertyChanged(e.PropertyName);
  714. }
  715. #endregion
  716. #region Event handle
  717. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  718. {
  719. pdfAnnotationControl.AnnotationCancel();
  720. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  721. RightPanelButton.IsChecked = false;
  722. ViewSettingBtn.IsChecked = false;
  723. }
  724. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  725. {
  726. pdfAnnotationControl.LoadAnnotationPanel(e);
  727. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  728. {
  729. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  730. RightPanelButton.IsChecked = true;
  731. }
  732. }
  733. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  734. {
  735. PropertyContainer.Child = pdfAnnotationControl;
  736. pdfAnnotationControl.SetAnnotEventData(e);
  737. }
  738. private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  739. {
  740. BotaSideTool.LoadAnnotationList();
  741. }
  742. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  743. {
  744. if (e.Key == "Zoom")
  745. {
  746. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  747. }
  748. }
  749. #endregion
  750. #region Save file
  751. /// <summary>
  752. /// Save the file to another PDF file.
  753. /// </summary>
  754. public void SaveAsFile()
  755. {
  756. {
  757. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  758. {
  759. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  760. SaveFileDialog saveDialog = new SaveFileDialog();
  761. saveDialog.Filter = "(*.pdf)|*.pdf";
  762. saveDialog.DefaultExt = ".pdf";
  763. saveDialog.OverwritePrompt = true;
  764. if (saveDialog.ShowDialog() == true)
  765. {
  766. pdfDoc.WriteToFilePath(saveDialog.FileName);
  767. }
  768. }
  769. }
  770. }
  771. /// <summary>
  772. /// Save the file in the current path.
  773. /// </summary>
  774. private void SaveFile()
  775. {
  776. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  777. {
  778. try
  779. {
  780. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  781. if (pdfDoc.WriteToLoadedPath())
  782. {
  783. return;
  784. }
  785. SaveFileDialog saveDialog = new SaveFileDialog();
  786. saveDialog.Filter = "(*.pdf)|*.pdf";
  787. saveDialog.DefaultExt = ".pdf";
  788. saveDialog.OverwritePrompt = true;
  789. if (saveDialog.ShowDialog() == true)
  790. {
  791. pdfDoc.WriteToFilePath(saveDialog.FileName);
  792. }
  793. }
  794. catch (Exception ex)
  795. {
  796. }
  797. }
  798. }
  799. #endregion
  800. #region Selected changed
  801. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  802. {
  803. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  804. if ((string)item.Content == "Viewer")
  805. {
  806. AnnotationBarControl.ClearAllToolState();
  807. ToolBarContainer.Visibility = Visibility.Collapsed;
  808. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  809. pdfAnnotationControl.AnnotationCancel();
  810. RightPanelButton.IsChecked = false;
  811. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  812. {
  813. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  814. }
  815. }
  816. else if ((string)item.Content == "Annotation")
  817. {
  818. ToolBarContainer.Visibility = Visibility.Visible;
  819. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  820. {
  821. pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
  822. }
  823. }
  824. }
  825. #endregion
  826. #region Close window
  827. protected override void OnClosing(CancelEventArgs e)
  828. {
  829. if (pdfViewControl.PDFView.UndoManager.CanSave)
  830. {
  831. MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
  832. if (result == MessageBoxResult.Yes)
  833. {
  834. SaveFile();
  835. }
  836. else if (result == MessageBoxResult.No)
  837. {
  838. }
  839. else
  840. {
  841. e.Cancel = true;
  842. }
  843. }
  844. }
  845. #endregion
  846. #region Shortcut
  847. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  848. {
  849. OpenFile();
  850. }
  851. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  852. {
  853. if (CanSave)
  854. {
  855. SaveFile();
  856. }
  857. }
  858. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  859. {
  860. SaveAsFile();
  861. }
  862. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  863. {
  864. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanUndo)
  865. {
  866. pdfViewControl.PDFView.UndoManager?.Undo();
  867. }
  868. }
  869. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  870. {
  871. if (pdfViewControl != null && pdfViewControl.PDFView != null && CanRedo)
  872. {
  873. pdfViewControl.PDFView.UndoManager?.Redo();
  874. }
  875. }
  876. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  877. {
  878. LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
  879. ControlLeftPanel();
  880. }
  881. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  882. {
  883. RightPanelButton.IsChecked = !RightPanelButton.IsChecked;
  884. ControlRightPanel();
  885. }
  886. private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
  887. {
  888. if (ModeComboBox.SelectedIndex == 1)
  889. {
  890. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
  891. }
  892. }
  893. private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
  894. {
  895. if (ModeComboBox.SelectedIndex == 1)
  896. {
  897. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
  898. }
  899. }
  900. private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
  901. {
  902. if (ModeComboBox.SelectedIndex == 1)
  903. {
  904. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
  905. }
  906. }
  907. private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
  908. {
  909. if (ModeComboBox.SelectedIndex == 1)
  910. {
  911. AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
  912. }
  913. }
  914. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  915. {
  916. ExpandLeftPanel(true);
  917. LeftToolPanelButton.IsChecked = true;
  918. BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
  919. }
  920. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  921. {
  922. ExpandLeftPanel(true);
  923. LeftToolPanelButton.IsChecked = true;
  924. BotaSideTool.SelectBotaTool(BOTATools.Outline);
  925. }
  926. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  927. {
  928. ExpandLeftPanel(true);
  929. LeftToolPanelButton.IsChecked = true;
  930. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  931. }
  932. private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e)
  933. {
  934. ExpandLeftPanel(true);
  935. LeftToolPanelButton.IsChecked = true;
  936. BotaSideTool.SelectBotaTool(BOTATools.Annotation);
  937. }
  938. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  939. {
  940. ExpandLeftPanel(true);
  941. LeftToolPanelButton.IsChecked = true;
  942. BotaSideTool.SelectBotaTool(BOTATools.Search);
  943. }
  944. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  945. {
  946. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  947. pdfViewControl.PDFView?.Zoom(newZoom);
  948. }
  949. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  950. {
  951. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  952. pdfViewControl.PDFView?.Zoom(newZoom);
  953. }
  954. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  955. {
  956. ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
  957. ShowViewSettings();
  958. }
  959. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  960. {
  961. if (PopupBorder.Visibility != Visibility.Visible)
  962. {
  963. PasswordUI.Visibility = Visibility.Collapsed;
  964. FileInfoUI.Visibility = Visibility.Visible;
  965. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  966. PopupBorder.Visibility = Visibility.Visible;
  967. }
  968. else
  969. {
  970. FileInfoUI.Visibility = Visibility.Collapsed;
  971. PopupBorder.Visibility = Visibility.Collapsed;
  972. }
  973. }
  974. #endregion
  975. }
  976. }