MainPage.xaml.cs 61 KB

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