MainPage.xaml.cs 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using Compdfkit_Tools.Data;
  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.Linq;
  18. using System.Runtime.CompilerServices;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Controls;
  23. using System.Windows.Controls.Primitives;
  24. using System.Windows.Data;
  25. using System.Windows.Documents;
  26. using System.Windows.Input;
  27. using System.Windows.Media;
  28. using System.Windows.Media.Imaging;
  29. using System.Windows.Navigation;
  30. using Path = System.IO.Path;
  31. namespace PDFViewer
  32. {
  33. public partial class MainPage : UserControl, INotifyPropertyChanged
  34. {
  35. #region Property
  36. private bool isFirstLoad = true;
  37. private string currentMode = "Viewer";
  38. private PDFViewControl passwordViewer;
  39. private PDFViewControl pdfViewControl = new PDFViewControl();
  40. private CPDFAnnotationControl pdfAnnotationControl = null;
  41. private UIElement pdfEditControl = null;
  42. private CPDFPageEditControl pageEditControl = null;
  43. private FromPropertyControl pdfFormControl = new FromPropertyControl();
  44. private PDFImageEditControl imageEditControl = null;
  45. private CPDFDisplaySettingsControl displayPanel = null;
  46. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  47. public bool CanSave
  48. {
  49. get
  50. {
  51. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  52. {
  53. return pdfViewControl.PDFView.UndoManager.CanSave;
  54. }
  55. return false;
  56. }
  57. }
  58. public bool CanUndo
  59. {
  60. get
  61. {
  62. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  63. {
  64. return pdfViewControl.PDFView.UndoManager.CanUndo;
  65. }
  66. return false;
  67. }
  68. }
  69. public bool CanRedo
  70. {
  71. get
  72. {
  73. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  74. {
  75. return pdfViewControl.PDFView.UndoManager.CanRedo;
  76. }
  77. return false;
  78. }
  79. }
  80. private PDFEditEvent lastPDFEditEvent = null;
  81. public event Func<string[], bool> CheckExistBeforeOpenFileEvent;
  82. public event PropertyChangedEventHandler PropertyChanged;
  83. public event EventHandler FileChangeEvent;
  84. #endregion
  85. public MainPage()
  86. {
  87. InitializeComponent();
  88. DataContext = this;
  89. if (pdfFormControl != null)
  90. {
  91. pdfFormControl = new FromPropertyControl();
  92. }
  93. }
  94. #region Initial Load Unload
  95. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  96. {
  97. pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
  98. FormBarControl.InitWithPDFViewer(newPDFViewer.PDFView, pdfFormControl);
  99. pdfFormControl.SetPDFViewer(newPDFViewer.PDFView);
  100. pdfAnnotationControl.AnnotationCancel();
  101. AnnotationBarControl.ClearAllToolState();
  102. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  103. newPDFViewer.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  104. newPDFViewer.CustomSignHandle = true;
  105. }
  106. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  107. {
  108. pdfAnnotationControl = new CPDFAnnotationControl();
  109. BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
  110. LoadDocument();
  111. }
  112. public void SetPDFViewer(PDFViewControl newPdfViewer)
  113. {
  114. if (newPdfViewer != null)
  115. {
  116. pdfViewControl = newPdfViewer;
  117. }
  118. }
  119. private void CPDFTitleBarControl_Loaded(object sender, RoutedEventArgs e)
  120. {
  121. CPDFTitleBarControl.OpenFileEvent -= CPDFTitleBarControl_OpenFileEvent;
  122. CPDFTitleBarControl.OpenFileEvent += CPDFTitleBarControl_OpenFileEvent;
  123. CPDFTitleBarControl.SaveAsFileEvent -= CPDFTitleBarControl_SaveAsFileEvent;
  124. CPDFTitleBarControl.SaveAsFileEvent += CPDFTitleBarControl_SaveAsFileEvent;
  125. CPDFTitleBarControl.SaveFileEvent -= CPDFTitleBarControl_SaveFileEvent;
  126. CPDFTitleBarControl.SaveFileEvent += CPDFTitleBarControl_SaveFileEvent; ;
  127. }
  128. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  129. {
  130. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  131. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  132. }
  133. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  134. {
  135. 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 };
  136. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  137. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  138. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  139. }
  140. #endregion
  141. #region UI
  142. private string GetTime()
  143. {
  144. DateTime dateTime = DateTime.Now;
  145. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  146. }
  147. /// <summary>
  148. /// InfoChanged
  149. /// When the PDF zoom ratio changes, the value of the zoom control also changes.
  150. /// </summary>
  151. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  152. {
  153. if (e.Key == "Zoom")
  154. {
  155. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  156. }
  157. }
  158. private void EditLink_Click(object sender, RoutedEventArgs e)
  159. {
  160. RightPanelButton.IsChecked = true;
  161. PropertyContainer.Visibility = Visibility.Visible;
  162. }
  163. private double CheckZoomLevel(double zoom, bool IsGrowth)
  164. {
  165. double standardZoom = 100;
  166. if (zoom <= 0.01)
  167. {
  168. return 0.01;
  169. }
  170. if (zoom >= 10)
  171. {
  172. return 10;
  173. }
  174. zoom *= 100;
  175. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  176. {
  177. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  178. {
  179. standardZoom = zoomLevelList[i + 1];
  180. break;
  181. }
  182. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  183. {
  184. standardZoom = zoomLevelList[i];
  185. break;
  186. }
  187. }
  188. return standardZoom / 100;
  189. }
  190. private void PDFView_AnnotEditHandler(object sender, List<ComPDFKitViewer.AnnotEvent.AnnotEditEvent> e)
  191. {
  192. BotaSideTool.LoadAnnotationList();
  193. }
  194. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  195. {
  196. OnPropertyChanged(e.PropertyName);
  197. }
  198. private void PasswordUI_Closed(object sender, EventArgs e)
  199. {
  200. PopupBorder.Visibility = Visibility.Collapsed;
  201. PasswordUI.Visibility = Visibility.Collapsed;
  202. }
  203. private void PasswordUI_Canceled(object sender, EventArgs e)
  204. {
  205. PopupBorder.Visibility = Visibility.Collapsed;
  206. PasswordUI.Visibility = Visibility.Collapsed;
  207. }
  208. private void PasswordUI_Confirmed(object sender, string e)
  209. {
  210. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  211. {
  212. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  213. if (passwordViewer.PDFView.Document.IsLocked == false)
  214. {
  215. PasswordUI.SetShowError("", Visibility.Collapsed);
  216. PasswordUI.ClearPassword();
  217. PasswordUI.Visibility = Visibility.Collapsed;
  218. PopupBorder.Visibility = Visibility.Collapsed;
  219. pdfViewControl = passwordViewer;
  220. LoadDocument();
  221. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  222. }
  223. else
  224. {
  225. PasswordUI.SetShowError("error", Visibility.Visible);
  226. }
  227. }
  228. }
  229. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  230. {
  231. SaveFile();
  232. }
  233. private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  234. {
  235. UIElement pageView = sender as UIElement;
  236. if (pageView != null)
  237. {
  238. pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp;
  239. }
  240. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  241. {
  242. if (imageEditControl == null)
  243. {
  244. imageEditControl = new PDFImageEditControl();
  245. }
  246. imageEditControl.InitWithPDFViewer(pdfViewControl.PDFView);
  247. imageEditControl.SetPDFImageEditData(lastPDFEditEvent);
  248. pdfEditControl = imageEditControl as UIElement;
  249. PropertyContainer.Child = pdfEditControl;
  250. ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
  251. }
  252. }
  253. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  254. {
  255. PasswordUI.Visibility = Visibility.Collapsed;
  256. FileInfoUI.Visibility = Visibility.Visible;
  257. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  258. PopupBorder.Visibility = Visibility.Visible;
  259. }
  260. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  261. {
  262. PopupBorder.Visibility = Visibility.Collapsed;
  263. }
  264. private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
  265. {
  266. ToggleButton senderBtn = sender as ToggleButton;
  267. if (senderBtn != null && pdfViewControl != null)
  268. {
  269. ClearPDFEditState(senderBtn);
  270. if (senderBtn.IsChecked == true)
  271. {
  272. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  273. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText);
  274. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
  275. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  276. pdfViewControl.PDFView?.ReloadDocument();
  277. }
  278. else
  279. {
  280. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  281. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  282. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  283. pdfViewControl.PDFView?.ReloadDocument();
  284. }
  285. }
  286. }
  287. private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
  288. {
  289. ToggleButton senderBtn = sender as ToggleButton;
  290. if (senderBtn != null && pdfViewControl != null)
  291. {
  292. senderBtn.IsChecked = false;
  293. OpenFileDialog openFileDialog = new OpenFileDialog();
  294. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  295. if (openFileDialog.ShowDialog() == true)
  296. {
  297. ClearPDFEditState(senderBtn);
  298. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  299. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText);
  300. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  301. pdfViewControl.PDFView?.ReloadDocument();
  302. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  303. pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
  304. pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName);
  305. }
  306. }
  307. }
  308. private void UndoBtn_Click(object sender, RoutedEventArgs e)
  309. {
  310. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  311. {
  312. pdfViewControl.PDFView.UndoManager?.Undo();
  313. }
  314. }
  315. private void RedoBtn_Click(object sender, RoutedEventArgs e)
  316. {
  317. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  318. {
  319. pdfViewControl.PDFView.UndoManager?.Redo();
  320. }
  321. }
  322. private void ClearPDFEditState(ToggleButton ignoreBtn = null)
  323. {
  324. List<ToggleButton> clearBtnList = new List<ToggleButton>()
  325. {
  326. PDFTextEditButton,
  327. PDFImageEditButton
  328. };
  329. foreach (ToggleButton item in clearBtnList)
  330. {
  331. if (ignoreBtn == item)
  332. {
  333. continue;
  334. }
  335. item.IsChecked = false;
  336. }
  337. }
  338. private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
  339. {
  340. AnnotationBarControl.ClearAllToolState();
  341. }
  342. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  343. {
  344. pdfAnnotationControl.AnnotationCancel();
  345. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  346. RightPanelButton.IsChecked = false;
  347. ViewSettingBtn.IsChecked = false;
  348. }
  349. #endregion
  350. #region ContextMenu
  351. /// <summary>
  352. /// Annotation Context menu
  353. /// </summary>
  354. private void PDFView_AnnotCommandHandler(object sender, ComPDFKitViewer.AnnotEvent.AnnotCommandArgs e)
  355. {
  356. switch (e.CommandType)
  357. {
  358. case CommandType.Context:
  359. e.Handle = true;
  360. if (e.CommandTarget == TargetType.Annot)
  361. {
  362. e.Handle = true;
  363. e.PopupMenu = new ContextMenu();
  364. if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
  365. {
  366. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  367. MenuItem propertyMenu = new MenuItem();
  368. propertyMenu = new MenuItem();
  369. propertyMenu.Header = "Edit";
  370. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  371. propertyMenu.CommandParameter = e;
  372. e.PopupMenu.Items.Add(propertyMenu);
  373. }
  374. else if (e.PressOnAnnot)
  375. {
  376. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  377. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  378. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  379. }
  380. else if (e.PressOnMedia || e.PressOnSound)
  381. {
  382. e.Handle = true;
  383. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  384. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  385. }
  386. else if (e.PressOnSelectedText)
  387. {
  388. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  389. }
  390. else
  391. {
  392. e.Handle = true;
  393. e.PopupMenu = new ContextMenu();
  394. if (currentMode == "Annotations" || currentMode == "Forms")
  395. {
  396. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  397. e.PopupMenu.Items.Add(new Separator());
  398. }
  399. MenuItem fitWidthMenu = new MenuItem();
  400. fitWidthMenu.Header = "Automatically Resize";
  401. fitWidthMenu.Click += (o, p) =>
  402. {
  403. if (pdfViewControl != null)
  404. {
  405. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  406. }
  407. };
  408. e.PopupMenu.Items.Add(fitWidthMenu);
  409. MenuItem fitSizeMenu = new MenuItem();
  410. fitSizeMenu.Header = "Actual Size";
  411. fitSizeMenu.Click += (o, p) =>
  412. {
  413. if (pdfViewControl != null)
  414. {
  415. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  416. }
  417. };
  418. e.PopupMenu.Items.Add(fitSizeMenu);
  419. MenuItem zoomInMenu = new MenuItem();
  420. zoomInMenu.Header = "Zoom In";
  421. zoomInMenu.Click += (o, p) =>
  422. {
  423. if (pdfViewControl != null)
  424. {
  425. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  426. pdfViewControl.PDFView?.Zoom(newZoom);
  427. }
  428. };
  429. e.PopupMenu.Items.Add(zoomInMenu);
  430. MenuItem zoomOutMenu = new MenuItem();
  431. zoomOutMenu.Header = "Zoom Out";
  432. zoomOutMenu.Click += (o, p) =>
  433. {
  434. if (pdfViewControl != null)
  435. {
  436. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  437. pdfViewControl.PDFView?.Zoom(newZoom);
  438. }
  439. };
  440. e.PopupMenu.Items.Add(zoomOutMenu);
  441. e.PopupMenu.Items.Add(new Separator());
  442. MenuItem singleView = new MenuItem();
  443. singleView.Header = "Single Page";
  444. singleView.Click += (o, p) =>
  445. {
  446. if (pdfViewControl != null)
  447. {
  448. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  449. }
  450. };
  451. e.PopupMenu.Items.Add(singleView);
  452. MenuItem singleContinuousView = new MenuItem();
  453. singleContinuousView.Header = "Single Page Continuous";
  454. singleContinuousView.Click += (o, p) =>
  455. {
  456. if (pdfViewControl != null)
  457. {
  458. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  459. }
  460. };
  461. e.PopupMenu.Items.Add(singleContinuousView);
  462. MenuItem doubleView = new MenuItem();
  463. doubleView.Header = "Two Pages";
  464. doubleView.Click += (o, p) =>
  465. {
  466. if (pdfViewControl != null)
  467. {
  468. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  469. }
  470. };
  471. e.PopupMenu.Items.Add(doubleView);
  472. MenuItem doubleContinuousView = new MenuItem();
  473. doubleContinuousView.Header = "Two Pages Continuous";
  474. doubleContinuousView.Click += (o, p) =>
  475. {
  476. if (pdfViewControl != null)
  477. {
  478. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  479. }
  480. };
  481. e.PopupMenu.Items.Add(doubleContinuousView);
  482. }
  483. }
  484. else if (e.CommandTarget == TargetType.WidgetView)
  485. {
  486. e.Handle = true;
  487. e.PopupMenu = new ContextMenu();
  488. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  489. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  490. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  491. }
  492. else if (e.CommandTarget == TargetType.ImageSelection)
  493. {
  494. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  495. {
  496. e.Handle = true;
  497. e.PopupMenu = new ContextMenu();
  498. MenuItem imageCopyMenu = new MenuItem();
  499. imageCopyMenu = new MenuItem();
  500. imageCopyMenu.Header = "Copy Images";
  501. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  502. imageCopyMenu.CommandParameter = e;
  503. e.PopupMenu.Items.Add(imageCopyMenu);
  504. MenuItem imageExtraMenu = new MenuItem();
  505. imageExtraMenu = new MenuItem();
  506. imageExtraMenu.Header = "Extract Images";
  507. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  508. imageExtraMenu.CommandParameter = e;
  509. e.PopupMenu.Items.Add(imageExtraMenu);
  510. }
  511. }
  512. break;
  513. case CommandType.Copy:
  514. e.DoCommand();
  515. break;
  516. case CommandType.Cut:
  517. case CommandType.Paste:
  518. case CommandType.Delete:
  519. e.DoCommand();
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. /// <summary>
  526. /// TextEdit context menu.
  527. /// </summary>
  528. private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand)
  529. {
  530. editCommand.PopupMenu = new ContextMenu();
  531. if (lastPDFEditEvent != null)
  532. {
  533. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  534. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  535. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  536. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  537. }
  538. else
  539. {
  540. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  541. if (editCommand.TextAreaCopied)
  542. {
  543. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender });
  544. }
  545. }
  546. }
  547. /// <summary>
  548. /// ImageEdit right-click menu.
  549. /// </summary>
  550. private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand)
  551. {
  552. editCommand.PopupMenu = new ContextMenu();
  553. if (lastPDFEditEvent != null)
  554. {
  555. MenuItem rotateLeftMenu = new MenuItem();
  556. rotateLeftMenu.Header = "Left Rotate";
  557. rotateLeftMenu.Click += (o, p) =>
  558. {
  559. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  560. {
  561. lastPDFEditEvent.Rotate = -90;
  562. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  563. imageEditControl?.SetImageThumb();
  564. }
  565. };
  566. editCommand.PopupMenu.Items.Add(rotateLeftMenu);
  567. MenuItem rotateRightMenu = new MenuItem();
  568. rotateRightMenu.Header = "Right Rotate";
  569. rotateRightMenu.Click += (o, p) =>
  570. {
  571. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  572. {
  573. lastPDFEditEvent.Rotate = 90;
  574. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  575. imageEditControl?.SetImageThumb();
  576. }
  577. };
  578. editCommand.PopupMenu.Items.Add(rotateRightMenu);
  579. MenuItem replaceMenu = new MenuItem();
  580. replaceMenu.Header = "Replace";
  581. replaceMenu.Click += (o, p) =>
  582. {
  583. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  584. {
  585. OpenFileDialog openFileDialog = new OpenFileDialog();
  586. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  587. if (openFileDialog.ShowDialog() == true)
  588. {
  589. lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName;
  590. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  591. pdfViewControl.PDFView?.ClearSelectPDFEdit();
  592. imageEditControl?.SetImageThumb();
  593. }
  594. }
  595. };
  596. editCommand.PopupMenu.Items.Add(replaceMenu);
  597. MenuItem exportMenu = new MenuItem();
  598. exportMenu.Header = "Export";
  599. exportMenu.Click += (o, p) =>
  600. {
  601. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  602. {
  603. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView.GetSelectedImages();
  604. if (imageDict != null && imageDict.Count > 0)
  605. {
  606. System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
  607. if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  608. {
  609. string choosePath = folderBrowser.SelectedPath;
  610. string openPath = choosePath;
  611. foreach (int pageIndex in imageDict.Keys)
  612. {
  613. List<Bitmap> imageList = imageDict[pageIndex];
  614. foreach (Bitmap image in imageList)
  615. {
  616. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  617. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  618. openPath = savePath;
  619. }
  620. }
  621. Process.Start("explorer", "/select,\"" + openPath + "\"");
  622. }
  623. }
  624. }
  625. };
  626. editCommand.PopupMenu.Items.Add(exportMenu);
  627. MenuItem opacityMenu = new MenuItem();
  628. opacityMenu.Header = "Opacity";
  629. editCommand.PopupMenu.Items.Add(opacityMenu);
  630. AppendOpacityMenu(opacityMenu);
  631. MenuItem horizonMirror = new MenuItem();
  632. horizonMirror.Header = "HMirror";
  633. horizonMirror.Click += (o, p) =>
  634. {
  635. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  636. {
  637. lastPDFEditEvent.HorizontalMirror = true;
  638. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  639. }
  640. };
  641. editCommand.PopupMenu.Items.Add(horizonMirror);
  642. MenuItem verticalMirror = new MenuItem();
  643. verticalMirror.Header = "VMirror";
  644. verticalMirror.Click += (o, p) =>
  645. {
  646. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  647. {
  648. lastPDFEditEvent.VerticalMirror = true;
  649. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  650. }
  651. };
  652. editCommand.PopupMenu.Items.Add(verticalMirror);
  653. MenuItem cropMenu = new MenuItem();
  654. cropMenu.Header = "Crop";
  655. cropMenu.Click += (o, p) =>
  656. {
  657. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  658. {
  659. lastPDFEditEvent.ClipImage = true;
  660. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  661. }
  662. };
  663. editCommand.PopupMenu.Items.Add(cropMenu);
  664. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  665. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  666. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  667. }
  668. else
  669. {
  670. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  671. }
  672. }
  673. /// <summary>
  674. /// ImageEdit transparency submenu.
  675. /// </summary>
  676. private void AppendOpacityMenu(MenuItem parentMenu)
  677. {
  678. List<int> opacityList = new List<int>()
  679. {
  680. 25,50,75,100
  681. };
  682. foreach (int opacity in opacityList)
  683. {
  684. MenuItem opacityMenu = new MenuItem();
  685. opacityMenu.Header = string.Format("{0}%", opacity);
  686. opacityMenu.Click += (o, p) =>
  687. {
  688. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  689. {
  690. lastPDFEditEvent.Transparency = (int)Math.Ceiling(opacity * 255 / 100D);
  691. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  692. }
  693. };
  694. parentMenu.Items.Add(opacityMenu);
  695. }
  696. }
  697. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  698. {
  699. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  700. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  701. {
  702. string choosePath = folderDialog.SelectedPath;
  703. string openPath = choosePath;
  704. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  705. if (imageDict != null && imageDict.Count > 0)
  706. {
  707. foreach (int pageIndex in imageDict.Keys)
  708. {
  709. List<Bitmap> imageList = imageDict[pageIndex];
  710. foreach (Bitmap image in imageList)
  711. {
  712. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  713. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  714. openPath = savePath;
  715. }
  716. }
  717. }
  718. Process.Start("explorer", "/select,\"" + openPath + "\"");
  719. }
  720. }
  721. private void CopyImage_Click(object sender, RoutedEventArgs e)
  722. {
  723. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  724. if (imageDict != null && imageDict.Count > 0)
  725. {
  726. foreach (int pageIndex in imageDict.Keys)
  727. {
  728. List<Bitmap> imageList = imageDict[pageIndex];
  729. foreach (Bitmap image in imageList)
  730. {
  731. MemoryStream ms = new MemoryStream();
  732. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  733. BitmapImage imageData = new BitmapImage();
  734. imageData.BeginInit();
  735. imageData.StreamSource = ms;
  736. imageData.CacheOption = BitmapCacheOption.OnLoad;
  737. imageData.EndInit();
  738. imageData.Freeze();
  739. Clipboard.SetImage(imageData);
  740. break;
  741. }
  742. }
  743. }
  744. }
  745. #endregion
  746. #region Property changed
  747. protected void OnPropertyChanged([CallerMemberName] string name = null)
  748. {
  749. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  750. }
  751. #endregion
  752. #region Open and save file
  753. public void InitWithFilePath(string filePath)
  754. {
  755. pdfViewControl = new PDFViewControl();
  756. pdfViewControl.PDFView.InitDocument(filePath);
  757. }
  758. private void LoadDocument()
  759. {
  760. if (pdfViewControl.PDFView.Document == null)
  761. {
  762. return;
  763. }
  764. //Load the Document
  765. pdfViewControl.PDFView?.Load();
  766. //Highlight the link annotation
  767. pdfViewControl.PDFView?.SetShowLink(true);
  768. //Add PDFViewControl to PDFGrid
  769. PDFGrid.Child = pdfViewControl;
  770. // Register the InfoChanged event handler,
  771. // which is used to notify the CPDFScalingControl of real-time changes in the displayed number when the page size changes.
  772. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  773. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  774. //
  775. pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
  776. pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  777. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  778. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  779. pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  780. pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  781. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  782. PasswordUI.Closed -= PasswordUI_Closed;
  783. PasswordUI.Canceled -= PasswordUI_Canceled;
  784. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  785. PasswordUI.Confirmed += PasswordUI_Confirmed;
  786. PasswordUI.Canceled += PasswordUI_Canceled;
  787. PasswordUI.Closed += PasswordUI_Closed;
  788. pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  789. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
  790. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
  791. InitialPDFViewControl(pdfViewControl);
  792. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  793. FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
  794. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  795. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  796. ViewSettingBtn.IsChecked = false;
  797. PropertyContainer.Child = null;
  798. PropertyContainer.Visibility = Visibility.Collapsed;
  799. ModeComboBox.SelectedIndex = 0;
  800. }
  801. /// <summary>
  802. /// Save the file to another PDF file.
  803. /// </summary>
  804. public void SaveAsFile()
  805. {
  806. {
  807. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  808. {
  809. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  810. SaveFileDialog saveDialog = new SaveFileDialog();
  811. saveDialog.Filter = "(*.pdf)|*.pdf";
  812. saveDialog.DefaultExt = ".pdf";
  813. saveDialog.OverwritePrompt = true;
  814. if (saveDialog.ShowDialog() == true)
  815. {
  816. pdfDoc.WriteToFilePath(saveDialog.FileName);
  817. }
  818. }
  819. }
  820. }
  821. /// <summary>
  822. /// Save the file in the current path.
  823. /// </summary>
  824. public void SaveFile()
  825. {
  826. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  827. {
  828. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  829. if (pdfDoc.WriteToLoadedPath())
  830. {
  831. pdfViewControl.PDFView.UndoManager.CanSave = false;
  832. return;
  833. }
  834. SaveFileDialog saveDialog = new SaveFileDialog();
  835. saveDialog.Filter = "(*.pdf)|*.pdf";
  836. saveDialog.DefaultExt = ".pdf";
  837. saveDialog.OverwritePrompt = true;
  838. if (saveDialog.ShowDialog() == true)
  839. {
  840. pdfDoc.WriteToFilePath(saveDialog.FileName);
  841. }
  842. }
  843. }
  844. public void OpenFile()
  845. {
  846. string filePath = CommonHelper.GetFilePathOrEmpty();
  847. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  848. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  849. {
  850. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  851. {
  852. if (oldFilePath.ToLower() == filePath.ToLower())
  853. {
  854. return;
  855. }
  856. }
  857. if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath }))
  858. {
  859. return;
  860. }
  861. passwordViewer = new PDFViewControl();
  862. passwordViewer.PDFView.InitDocument(filePath);
  863. if (passwordViewer.PDFView.Document == null)
  864. {
  865. MessageBox.Show("Open File Failed");
  866. return;
  867. }
  868. if (passwordViewer.PDFView.Document.IsLocked)
  869. {
  870. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  871. PasswordUI.ClearPassword();
  872. PopupBorder.Visibility = Visibility.Visible;
  873. PasswordUI.Visibility = Visibility.Visible;
  874. }
  875. else
  876. {
  877. pdfViewControl = passwordViewer;
  878. LoadDocument();
  879. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  880. }
  881. }
  882. }
  883. private void OpenFile_Click(object sender, RoutedEventArgs e)
  884. {
  885. OpenFile();
  886. }
  887. #endregion
  888. #region ToolPanel expand and collapse
  889. private void ExpandLeftPanel(bool isExpand)
  890. {
  891. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  892. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  893. if (isExpand)
  894. {
  895. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  896. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  897. }
  898. else
  899. {
  900. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  901. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  902. }
  903. }
  904. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  905. {
  906. ToggleButton expandBtn = sender as ToggleButton;
  907. if (expandBtn != null)
  908. {
  909. bool isExpand = expandBtn.IsChecked == true;
  910. ExpandLeftPanel(isExpand);
  911. }
  912. }
  913. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  914. {
  915. ExpandLeftPanel(true);
  916. LeftToolPanelButton.IsChecked = true;
  917. BotaSideTool.SelectBotaTool(BOTATools.Search);
  918. }
  919. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  920. {
  921. ToggleButton toggleButton = sender as ToggleButton;
  922. if (toggleButton != null)
  923. {
  924. if (toggleButton.IsChecked == true)
  925. {
  926. if (displayPanel == null)
  927. {
  928. displayPanel = new CPDFDisplaySettingsControl();
  929. }
  930. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  931. PropertyContainer.Child = displayPanel;
  932. PropertyContainer.Visibility = Visibility.Visible;
  933. if ((bool)RightPanelButton.IsChecked)
  934. {
  935. RightPanelButton.IsChecked = false;
  936. }
  937. }
  938. else
  939. {
  940. PropertyContainer.Child = null;
  941. PropertyContainer.Visibility = Visibility.Collapsed;
  942. }
  943. }
  944. }
  945. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  946. {
  947. PropertyContainer.Width = 260;
  948. PropertyContainer.Child = properytPanel;
  949. PropertyContainer.Visibility = visible;
  950. if (visible == Visibility.Hidden || visible == Visibility.Collapsed)
  951. {
  952. RightPanelButton.IsChecked = false;
  953. }
  954. }
  955. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  956. {
  957. ToggleButton toggleButton = sender as ToggleButton;
  958. if (toggleButton != null)
  959. {
  960. if (toggleButton.IsChecked == true)
  961. {
  962. if (currentMode == "Annotation")
  963. {
  964. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  965. }
  966. else if (currentMode == "Edit")
  967. {
  968. ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
  969. }
  970. else if (currentMode == "Forms")
  971. {
  972. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  973. }
  974. else
  975. {
  976. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  977. }
  978. if ((bool)ViewSettingBtn.IsChecked)
  979. {
  980. ViewSettingBtn.IsChecked = false;
  981. }
  982. }
  983. else
  984. {
  985. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  986. }
  987. }
  988. }
  989. #endregion
  990. #region Event handle
  991. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  992. {
  993. pdfAnnotationControl.LoadAnnotationPanel(e);
  994. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  995. {
  996. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  997. RightPanelButton.IsChecked = true;
  998. }
  999. }
  1000. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  1001. {
  1002. PropertyContainer.Child = pdfAnnotationControl;
  1003. pdfAnnotationControl.SetAnnotEventData(e);
  1004. }
  1005. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  1006. {
  1007. if (e is WidgetSignArgs)
  1008. {
  1009. RightPanelButton.IsChecked = true;
  1010. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1011. pdfFormControl.SetPropertyForType(e, null);
  1012. }
  1013. else
  1014. {
  1015. RightPanelButton.IsChecked = false;
  1016. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1017. }
  1018. }
  1019. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  1020. {
  1021. if (e == null)
  1022. {
  1023. return;
  1024. }
  1025. if (e.EditType == CPDFEditType.EditText)
  1026. {
  1027. e.Handle = true;
  1028. PDFEditTextContextMenu(sender, e);
  1029. }
  1030. if (e.EditType == CPDFEditType.EditImage)
  1031. {
  1032. e.Handle = true;
  1033. PDFEditImageContextMenu(sender, e);
  1034. }
  1035. }
  1036. /// <summary>
  1037. /// Text and Image Selected Event
  1038. /// </summary>
  1039. private void PDFView_PDFEditActiveHandler(object sender, ComPDFKitViewer.PDFEditEvent e)
  1040. {
  1041. lastPDFEditEvent = e;
  1042. ViewSettingBtn.IsChecked = false;
  1043. if (e == null)
  1044. {
  1045. pdfEditControl = null;
  1046. PropertyContainer.Child = pdfEditControl;
  1047. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1048. return;
  1049. }
  1050. if (Mouse.RightButton == MouseButtonState.Pressed)
  1051. {
  1052. return;
  1053. }
  1054. if (e.EditType == CPDFEditType.EditText)
  1055. {
  1056. RightPanelButton.IsChecked = true;
  1057. PDFTextEditControl textEditControl = new PDFTextEditControl();
  1058. textEditControl.SetPDFTextEditData(e);
  1059. pdfEditControl = textEditControl as UIElement;
  1060. PropertyContainer.Child = pdfEditControl;
  1061. ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
  1062. return;
  1063. }
  1064. if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null)
  1065. {
  1066. RightPanelButton.IsChecked = true;
  1067. UIElement pageView = sender as UIElement;
  1068. if (pageView != null)
  1069. {
  1070. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; ;
  1071. }
  1072. return;
  1073. }
  1074. }
  1075. private void PDFView_FormEditHandler(object sender, List<AnnotEditEvent> e)
  1076. {
  1077. if (e != null && e.Count > 0)
  1078. {
  1079. AnnotEditEvent editEvent = e[e.Count - 1];
  1080. if (editEvent.EditAction == ActionType.Add)
  1081. {
  1082. if (pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs != null)
  1083. {
  1084. WidgetArgs widgetArgs = pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs as WidgetArgs;
  1085. if (widgetArgs != null)
  1086. {
  1087. switch (widgetArgs.WidgeType)
  1088. {
  1089. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1090. widgetArgs.FieldName = "Button" + GetTime();
  1091. break;
  1092. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_CHECKBOX:
  1093. widgetArgs.FieldName = "Checkbox" + GetTime();
  1094. break;
  1095. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  1096. widgetArgs.FieldName = "Radio button" + GetTime();
  1097. break;
  1098. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  1099. widgetArgs.FieldName = "Text" + GetTime();
  1100. break;
  1101. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1102. widgetArgs.FieldName = "Combobox" + GetTime();
  1103. break;
  1104. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  1105. widgetArgs.FieldName = "List" + GetTime();
  1106. break;
  1107. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  1108. widgetArgs.FieldName = "Signature" + GetTime();
  1109. break;
  1110. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_UNKNOWN:
  1111. break;
  1112. default:
  1113. break;
  1114. }
  1115. }
  1116. }
  1117. pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
  1118. }
  1119. else if (editEvent.EditAction == ActionType.Del)
  1120. {
  1121. pdfFormControl.CleanProperty();
  1122. }
  1123. }
  1124. }
  1125. private void PDFView_FormActiveHandler(object sender, AnnotAttribEvent e)
  1126. {
  1127. if (e == null || e.IsAnnotCreateReset)
  1128. {
  1129. pdfFormControl.SetPropertyForType(null, null);
  1130. }
  1131. else
  1132. {
  1133. switch (e.GetAnnotTypes())
  1134. {
  1135. case AnnotArgsType.WidgetViewForm:
  1136. WidgetArgs formArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.WidgetViewForm).First() as WidgetArgs;
  1137. switch (formArgs.WidgeType)
  1138. {
  1139. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1140. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1141. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  1142. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1143. if ((bool)ViewSettingBtn.IsChecked)
  1144. {
  1145. ViewSettingBtn.IsChecked = false;
  1146. }
  1147. RightPanelButton.IsChecked = true;
  1148. break;
  1149. default:
  1150. break;
  1151. }
  1152. pdfFormControl.SetPropertyForType(formArgs, e);
  1153. break;
  1154. }
  1155. }
  1156. }
  1157. private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
  1158. {
  1159. ModeComboBox.SelectedIndex = 0;
  1160. }
  1161. private void PageEditControl_PageMoved(object sender, RoutedEventArgs e)
  1162. {
  1163. if (pdfViewControl == null || pdfViewControl.PDFView == null)
  1164. {
  1165. return;
  1166. }
  1167. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  1168. }
  1169. private void CPDFPageEditBarControl_PageEditEvent(object sender, string e)
  1170. {
  1171. pageEditControl.PageEdit(e);
  1172. }
  1173. private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
  1174. {
  1175. SaveFile();
  1176. }
  1177. private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
  1178. {
  1179. SaveAsFile();
  1180. }
  1181. private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
  1182. {
  1183. OpenFile();
  1184. }
  1185. #endregion
  1186. #region Change mode
  1187. /// <summary>
  1188. /// Actions performed when switching modes:
  1189. /// Separated into actions performed upon entering and exiting the mode.
  1190. /// </summary>
  1191. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1192. {
  1193. if (isFirstLoad)
  1194. {
  1195. isFirstLoad = false;
  1196. return;
  1197. }
  1198. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  1199. //Behavior when exit mode
  1200. switch (currentMode)
  1201. {
  1202. case "Viewer":
  1203. break;
  1204. /*
  1205. * Behavior when exit Edit mode
  1206. * Clear and collapse the ToolBar
  1207. * Clear And collapse the property panel.
  1208. * Change the mousemode to PanTool
  1209. * Unsubscribe the event handles
  1210. * Clear the undo history
  1211. */
  1212. case "Annotations":
  1213. AnnotationBarControl.Visibility = Visibility.Collapsed;
  1214. AnnotationBarControl.ClearAllToolState();
  1215. pdfAnnotationControl.ClearPanel();
  1216. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1217. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1218. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  1219. pdfAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar;
  1220. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  1221. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1222. break;
  1223. /*
  1224. * Behavior when exit edit mode:
  1225. * Collapsed the ToolBar
  1226. * Highlight the link annotation
  1227. * Set the mode to not allow edit
  1228. * Set the mouse mode to PanTool
  1229. * Reload Document
  1230. * Unsubscribe click event.
  1231. * Clear And collapse the property panel.
  1232. * Clear and collapse the ToolBar
  1233. * Clear undo history
  1234. * CPDFDisplaySettingsControl
  1235. * Enable the configuration of the split view.
  1236. */
  1237. case "Content Editor":
  1238. PDFEditTool.Visibility = Visibility.Collapsed;
  1239. pdfViewControl.PDFView?.SetShowLink(true);
  1240. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
  1241. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1242. pdfViewControl.PDFView?.ReloadDocument();
  1243. pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
  1244. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1245. ClearPDFEditState();
  1246. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1247. if (displayPanel != null)
  1248. {
  1249. displayPanel.SetSplitContainerVisibility(Visibility.Visible);
  1250. }
  1251. break;
  1252. /*
  1253. * Behavior when exit form mode:
  1254. * Clear and collapsed the ToolBar
  1255. * Clear And Collapse the property panel.
  1256. * Unsubscribe event.
  1257. * Clear undo history
  1258. */
  1259. case "Forms":
  1260. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1261. FormBarControl.ClearAllToolState();
  1262. FormBarControl.Visibility = Visibility.Collapsed;
  1263. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1264. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_FormActiveHandler;
  1265. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_FormEditHandler;
  1266. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1267. break;
  1268. /*
  1269. * Unsubscribe event.
  1270. * Show Undo Redo Button
  1271. * Collapse PageEditBarControl
  1272. * Replace PDFGrid.Child to pdfViewControl.
  1273. * Reload Document
  1274. * Enable Button
  1275. */
  1276. case "Docs Editor":
  1277. pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit;
  1278. pageEditControl.PageMoved -= PageEditControl_PageMoved;
  1279. PageEditBarControl.PageEditEvent -= CPDFPageEditBarControl_PageEditEvent;
  1280. UndoRedoStackPanel.Visibility = Visibility.Visible;
  1281. PageEditBarControl.Visibility = Visibility.Collapsed;
  1282. PDFGrid.Child = pdfViewControl;
  1283. FloatPageTool.Visibility = Visibility.Visible;
  1284. pdfViewControl.PDFView.ReloadDocument();
  1285. LeftToolPanelButton.IsEnabled = true;
  1286. SearchButton.IsEnabled = true;
  1287. RightPanelButton.IsEnabled = true;
  1288. ViewSettingBtn.IsEnabled = true;
  1289. break;
  1290. default:
  1291. break;
  1292. }
  1293. /*
  1294. * Behavior when switching to Viewer mode:
  1295. * Hide the ToolBar
  1296. * Set mousemode to Viewer
  1297. */
  1298. if ((string)item.Content == "Viewer")
  1299. {
  1300. ToolBarContainer.Visibility = Visibility.Collapsed;
  1301. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  1302. }
  1303. /*
  1304. * Behavior when switching to Annotation mode:
  1305. * Show the ToolBar
  1306. * Show the AnnotationBar
  1307. */
  1308. else if ((string)item.Content == "Annotations")
  1309. {
  1310. ToolBarContainer.Visibility = Visibility.Visible;
  1311. AnnotationBarControl.Visibility = Visibility.Visible;
  1312. pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
  1313. pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  1314. pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  1315. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  1316. }
  1317. /*
  1318. * Behavior when switching to Edit mode:
  1319. * Show the ToolBar
  1320. * Show the EditBar
  1321. * Set the mode to allow editing of both text and images
  1322. * Set the mouse mode to PDFEdit
  1323. * Reload Document
  1324. * Set the splite mode to None and unable the split setting
  1325. */
  1326. else if ((string)item.Content == "Content Editor")
  1327. {
  1328. ToolBarContainer.Visibility = Visibility.Visible;
  1329. PDFEditTool.Visibility = Visibility.Visible;
  1330. PageEditBarControl.Visibility = Visibility.Collapsed;
  1331. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  1332. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  1333. pdfViewControl.PDFView?.ReloadDocument();
  1334. pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  1335. pdfViewControl.PDFView.SetSplitMode(SplitMode.None);
  1336. if (displayPanel != null)
  1337. {
  1338. displayPanel.SetSplitContainerVisibility(Visibility.Collapsed);
  1339. }
  1340. }
  1341. /*
  1342. * Behavior when switching to Form mode:
  1343. *
  1344. */
  1345. else if ((string)item.Content == "Forms")
  1346. {
  1347. ToolBarContainer.Visibility = Visibility.Visible;
  1348. FormBarControl.Visibility = Visibility.Visible;
  1349. pdfViewControl.PDFView?.SetShowLink(true);
  1350. pdfViewControl.PDFView?.SetMouseMode(MouseModes.FormEditTool);
  1351. pdfViewControl.PDFView?.ReloadDocument();
  1352. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_FormActiveHandler;
  1353. pdfViewControl.PDFView.AnnotEditHandler += PDFView_FormEditHandler;
  1354. }
  1355. /*
  1356. *
  1357. */
  1358. else if ((string)item.Content == "Docs Editor")
  1359. {
  1360. ToolBarContainer.Visibility = Visibility.Visible;
  1361. if (pageEditControl == null)
  1362. {
  1363. pageEditControl = new CPDFPageEditControl();
  1364. }
  1365. pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit; ;
  1366. pageEditControl.PageMoved += PageEditControl_PageMoved; ;
  1367. PageEditBarControl.PageEditEvent += CPDFPageEditBarControl_PageEditEvent;
  1368. pageEditControl.LoadThumbnails(pdfViewControl.PDFView);
  1369. PDFGrid.Child = pageEditControl;
  1370. UndoRedoStackPanel.Visibility = Visibility.Collapsed;
  1371. PageEditBarControl.Visibility = Visibility.Visible;
  1372. LeftToolPanelButton.IsChecked = false;
  1373. LeftToolPanelButton.IsEnabled = false;
  1374. SearchButton.IsEnabled = false;
  1375. RightPanelButton.IsChecked = false;
  1376. RightPanelButton.IsEnabled = false;
  1377. ViewSettingBtn.IsChecked = false;
  1378. ViewSettingBtn.IsEnabled = false;
  1379. ExpandLeftPanel(false);
  1380. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1381. FloatPageTool.Visibility = Visibility.Collapsed;
  1382. }
  1383. currentMode = (string)item.Content;
  1384. }
  1385. #endregion
  1386. }
  1387. }