MainPage.xaml.cs 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. try
  599. {
  600. foreach (int pageIndex in imageDict.Keys)
  601. {
  602. List<Bitmap> imageList = imageDict[pageIndex];
  603. foreach (Bitmap image in imageList)
  604. {
  605. string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  606. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  607. openPath = savePath;
  608. }
  609. }
  610. Process.Start("explorer", "/select,\"" + openPath + "\"");
  611. }
  612. catch (Exception ex)
  613. {
  614. }
  615. }
  616. }
  617. }
  618. };
  619. editCommand.PopupMenu.Items.Add(exportMenu);
  620. MenuItem opacityMenu = new MenuItem();
  621. opacityMenu.Header = "Opacity";
  622. editCommand.PopupMenu.Items.Add(opacityMenu);
  623. AppendOpacityMenu(opacityMenu);
  624. MenuItem horizonMirror = new MenuItem();
  625. horizonMirror.Header = "HMirror";
  626. horizonMirror.Click += (o, p) =>
  627. {
  628. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  629. {
  630. lastPDFEditEvent.HorizontalMirror = true;
  631. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  632. }
  633. };
  634. editCommand.PopupMenu.Items.Add(horizonMirror);
  635. MenuItem verticalMirror = new MenuItem();
  636. verticalMirror.Header = "VMirror";
  637. verticalMirror.Click += (o, p) =>
  638. {
  639. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  640. {
  641. lastPDFEditEvent.VerticalMirror = true;
  642. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  643. }
  644. };
  645. editCommand.PopupMenu.Items.Add(verticalMirror);
  646. MenuItem cropMenu = new MenuItem();
  647. cropMenu.Header = "Crop";
  648. cropMenu.Click += (o, p) =>
  649. {
  650. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  651. {
  652. lastPDFEditEvent.ClipImage = true;
  653. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  654. }
  655. };
  656. editCommand.PopupMenu.Items.Add(cropMenu);
  657. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  658. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  659. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  660. }
  661. else
  662. {
  663. editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  664. }
  665. }
  666. /// <summary>
  667. /// ImageEdit transparency submenu.
  668. /// </summary>
  669. private void AppendOpacityMenu(MenuItem parentMenu)
  670. {
  671. List<int> opacityList = new List<int>()
  672. {
  673. 25,50,75,100
  674. };
  675. foreach (int opacity in opacityList)
  676. {
  677. MenuItem opacityMenu = new MenuItem();
  678. opacityMenu.Header = string.Format("{0}%", opacity);
  679. opacityMenu.Click += (o, p) =>
  680. {
  681. if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
  682. {
  683. lastPDFEditEvent.Transparency = (int)Math.Ceiling(opacity * 255 / 100D);
  684. lastPDFEditEvent.UpdatePDFEditByEventArgs();
  685. }
  686. };
  687. parentMenu.Items.Add(opacityMenu);
  688. }
  689. }
  690. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  691. {
  692. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  693. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  694. {
  695. string choosePath = folderDialog.SelectedPath;
  696. string openPath = choosePath;
  697. try
  698. {
  699. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  700. if (imageDict != null && imageDict.Count > 0)
  701. {
  702. foreach (int pageIndex in imageDict.Keys)
  703. {
  704. List<Bitmap> imageList = imageDict[pageIndex];
  705. foreach (Bitmap image in imageList)
  706. {
  707. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  708. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  709. openPath = savePath;
  710. }
  711. }
  712. }
  713. Process.Start("explorer", "/select,\"" + openPath + "\"");
  714. }
  715. catch (Exception ex)
  716. {
  717. }
  718. }
  719. }
  720. private void CopyImage_Click(object sender, RoutedEventArgs e)
  721. {
  722. try
  723. {
  724. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  725. if (imageDict != null && imageDict.Count > 0)
  726. {
  727. foreach (int pageIndex in imageDict.Keys)
  728. {
  729. List<Bitmap> imageList = imageDict[pageIndex];
  730. foreach (Bitmap image in imageList)
  731. {
  732. MemoryStream ms = new MemoryStream();
  733. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  734. BitmapImage imageData = new BitmapImage();
  735. imageData.BeginInit();
  736. imageData.StreamSource = ms;
  737. imageData.CacheOption = BitmapCacheOption.OnLoad;
  738. imageData.EndInit();
  739. imageData.Freeze();
  740. Clipboard.SetImage(imageData);
  741. break;
  742. }
  743. }
  744. }
  745. }
  746. catch (Exception ex)
  747. {
  748. }
  749. }
  750. #endregion
  751. #region Property changed
  752. protected void OnPropertyChanged([CallerMemberName] string name = null)
  753. {
  754. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  755. }
  756. #endregion
  757. #region Open and save file
  758. public void InitWithFilePath(string filePath)
  759. {
  760. pdfViewControl = new PDFViewControl();
  761. pdfViewControl.PDFView.InitDocument(filePath);
  762. }
  763. private void LoadDocument()
  764. {
  765. //Load the Document
  766. pdfViewControl.PDFView?.Load();
  767. //Highlight the link annotation
  768. pdfViewControl.PDFView?.SetShowLink(true);
  769. //Add PDFViewControl to PDFGrid
  770. PDFGrid.Child = pdfViewControl;
  771. // Register the InfoChanged event handler,
  772. // which is used to notify the CPDFScalingControl of real-time changes in the displayed number when the page size changes.
  773. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  774. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  775. //
  776. pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler;
  777. pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
  778. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  779. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  780. pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  781. pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  782. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  783. PasswordUI.Closed -= PasswordUI_Closed;
  784. PasswordUI.Canceled -= PasswordUI_Canceled;
  785. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  786. PasswordUI.Confirmed += PasswordUI_Confirmed;
  787. PasswordUI.Canceled += PasswordUI_Canceled;
  788. PasswordUI.Closed += PasswordUI_Closed;
  789. pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  790. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
  791. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
  792. InitialPDFViewControl(pdfViewControl);
  793. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  794. FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
  795. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  796. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  797. ViewSettingBtn.IsChecked = false;
  798. PropertyContainer.Child = null;
  799. PropertyContainer.Visibility = Visibility.Collapsed;
  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. try
  810. {
  811. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  812. SaveFileDialog saveDialog = new SaveFileDialog();
  813. saveDialog.Filter = "(*.pdf)|*.pdf";
  814. saveDialog.DefaultExt = ".pdf";
  815. saveDialog.OverwritePrompt = true;
  816. if (saveDialog.ShowDialog() == true)
  817. {
  818. pdfDoc.WriteToFilePath(saveDialog.FileName);
  819. }
  820. }
  821. catch (Exception ex)
  822. {
  823. }
  824. }
  825. }
  826. }
  827. /// <summary>
  828. /// Save the file in the current path.
  829. /// </summary>
  830. public void SaveFile()
  831. {
  832. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  833. {
  834. try
  835. {
  836. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  837. if (pdfDoc.WriteToLoadedPath())
  838. {
  839. pdfViewControl.PDFView.UndoManager.CanSave = false;
  840. return;
  841. }
  842. SaveFileDialog saveDialog = new SaveFileDialog();
  843. saveDialog.Filter = "(*.pdf)|*.pdf";
  844. saveDialog.DefaultExt = ".pdf";
  845. saveDialog.OverwritePrompt = true;
  846. if (saveDialog.ShowDialog() == true)
  847. {
  848. pdfDoc.WriteToFilePath(saveDialog.FileName);
  849. }
  850. }
  851. catch (Exception ex)
  852. {
  853. }
  854. }
  855. }
  856. public void OpenFile()
  857. {
  858. try
  859. {
  860. string filePath = CommonHelper.GetFilePathOrEmpty();
  861. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  862. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  863. {
  864. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  865. {
  866. if (oldFilePath.ToLower() == filePath.ToLower())
  867. {
  868. return;
  869. }
  870. }
  871. if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath }))
  872. {
  873. return;
  874. }
  875. passwordViewer = new PDFViewControl();
  876. passwordViewer.PDFView.InitDocument(filePath);
  877. if (passwordViewer.PDFView.Document == null)
  878. {
  879. MessageBox.Show("Open File Failed");
  880. return;
  881. }
  882. if (passwordViewer.PDFView.Document.IsLocked)
  883. {
  884. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  885. PasswordUI.ClearPassword();
  886. PopupBorder.Visibility = Visibility.Visible;
  887. PasswordUI.Visibility = Visibility.Visible;
  888. }
  889. else
  890. {
  891. pdfViewControl = passwordViewer;
  892. LoadDocument();
  893. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  894. }
  895. }
  896. }
  897. catch (Exception ex)
  898. {
  899. }
  900. }
  901. private void OpenFile_Click(object sender, RoutedEventArgs e)
  902. {
  903. OpenFile();
  904. }
  905. #endregion
  906. #region ToolPanel expand and collapse
  907. private void ExpandLeftPanel(bool isExpand)
  908. {
  909. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  910. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  911. if (isExpand)
  912. {
  913. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  914. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  915. }
  916. else
  917. {
  918. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  919. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  920. }
  921. }
  922. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  923. {
  924. ToggleButton expandBtn = sender as ToggleButton;
  925. if (expandBtn != null)
  926. {
  927. bool isExpand = expandBtn.IsChecked == true;
  928. ExpandLeftPanel(isExpand);
  929. }
  930. }
  931. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  932. {
  933. ExpandLeftPanel(true);
  934. LeftToolPanelButton.IsChecked = true;
  935. BotaSideTool.SelectBotaTool(BOTATools.Search);
  936. }
  937. private void ViewSettingBtn_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 (displayPanel == null)
  945. {
  946. displayPanel = new CPDFDisplaySettingsControl();
  947. }
  948. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  949. PropertyContainer.Child = displayPanel;
  950. PropertyContainer.Visibility = Visibility.Visible;
  951. if ((bool)RightPanelButton.IsChecked)
  952. {
  953. RightPanelButton.IsChecked = false;
  954. }
  955. }
  956. else
  957. {
  958. PropertyContainer.Child = null;
  959. PropertyContainer.Visibility = Visibility.Collapsed;
  960. }
  961. }
  962. }
  963. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  964. {
  965. PropertyContainer.Width = 260;
  966. PropertyContainer.Child = properytPanel;
  967. PropertyContainer.Visibility = visible;
  968. if (visible == Visibility.Hidden || visible == Visibility.Collapsed)
  969. {
  970. RightPanelButton.IsChecked = false;
  971. }
  972. }
  973. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  974. {
  975. ToggleButton toggleButton = sender as ToggleButton;
  976. if (toggleButton != null)
  977. {
  978. if (toggleButton.IsChecked == true)
  979. {
  980. if (currentMode == "Annotation")
  981. {
  982. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  983. }
  984. else if (currentMode == "Edit")
  985. {
  986. ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
  987. }
  988. else if (currentMode == "Forms")
  989. {
  990. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  991. }
  992. else
  993. {
  994. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  995. }
  996. if ((bool)ViewSettingBtn.IsChecked)
  997. {
  998. ViewSettingBtn.IsChecked = false;
  999. }
  1000. }
  1001. else
  1002. {
  1003. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1004. }
  1005. }
  1006. }
  1007. #endregion
  1008. #region Event handle
  1009. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  1010. {
  1011. pdfAnnotationControl.LoadAnnotationPanel(e);
  1012. if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
  1013. {
  1014. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  1015. RightPanelButton.IsChecked = true;
  1016. }
  1017. }
  1018. private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  1019. {
  1020. PropertyContainer.Child = pdfAnnotationControl;
  1021. pdfAnnotationControl.SetAnnotEventData(e);
  1022. }
  1023. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  1024. {
  1025. if (e is WidgetSignArgs)
  1026. {
  1027. RightPanelButton.IsChecked = true;
  1028. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1029. pdfFormControl.SetPropertyForType(e, null);
  1030. }
  1031. else
  1032. {
  1033. RightPanelButton.IsChecked = false;
  1034. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1035. }
  1036. }
  1037. private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e)
  1038. {
  1039. if (e == null)
  1040. {
  1041. return;
  1042. }
  1043. if (e.EditType == CPDFEditType.EditText)
  1044. {
  1045. e.Handle = true;
  1046. PDFEditTextContextMenu(sender, e);
  1047. }
  1048. if (e.EditType == CPDFEditType.EditImage)
  1049. {
  1050. e.Handle = true;
  1051. PDFEditImageContextMenu(sender, e);
  1052. }
  1053. }
  1054. /// <summary>
  1055. /// Text and Image Selected Event
  1056. /// </summary>
  1057. private void PDFView_PDFEditActiveHandler(object sender, ComPDFKitViewer.PDFEditEvent e)
  1058. {
  1059. lastPDFEditEvent = e;
  1060. ViewSettingBtn.IsChecked = false;
  1061. if (e == null)
  1062. {
  1063. pdfEditControl = null;
  1064. PropertyContainer.Child = pdfEditControl;
  1065. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1066. return;
  1067. }
  1068. if (Mouse.RightButton == MouseButtonState.Pressed)
  1069. {
  1070. return;
  1071. }
  1072. if (e.EditType == CPDFEditType.EditText)
  1073. {
  1074. RightPanelButton.IsChecked = true;
  1075. PDFTextEditControl textEditControl = new PDFTextEditControl();
  1076. textEditControl.SetPDFTextEditData(e);
  1077. pdfEditControl = textEditControl as UIElement;
  1078. PropertyContainer.Child = pdfEditControl;
  1079. ExpandRightPropertyPanel(pdfEditControl, Visibility.Visible);
  1080. return;
  1081. }
  1082. if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null)
  1083. {
  1084. RightPanelButton.IsChecked = true;
  1085. UIElement pageView = sender as UIElement;
  1086. if (pageView != null)
  1087. {
  1088. pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; ;
  1089. }
  1090. return;
  1091. }
  1092. }
  1093. private void PDFView_FormEditHandler(object sender, List<AnnotEditEvent> e)
  1094. {
  1095. if (e != null && e.Count > 0)
  1096. {
  1097. AnnotEditEvent editEvent = e[e.Count - 1];
  1098. if (editEvent.EditAction == ActionType.Add)
  1099. {
  1100. pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
  1101. }
  1102. }
  1103. }
  1104. private void PDFView_FormActiveHandler(object sender, AnnotAttribEvent e)
  1105. {
  1106. if (e == null || e.IsAnnotCreateReset)
  1107. {
  1108. pdfFormControl.SetPropertyForType(null, null);
  1109. }
  1110. else
  1111. {
  1112. switch (e.GetAnnotTypes())
  1113. {
  1114. case AnnotArgsType.WidgetViewForm:
  1115. WidgetArgs formArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.WidgetViewForm).First() as WidgetArgs;
  1116. switch (formArgs.WidgeType)
  1117. {
  1118. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1119. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1120. case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
  1121. ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible);
  1122. if ((bool)ViewSettingBtn.IsChecked)
  1123. {
  1124. ViewSettingBtn.IsChecked = false;
  1125. }
  1126. RightPanelButton.IsChecked = true;
  1127. break;
  1128. default:
  1129. break;
  1130. }
  1131. pdfFormControl.SetPropertyForType(formArgs, e);
  1132. break;
  1133. }
  1134. }
  1135. }
  1136. private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
  1137. {
  1138. ModeComboBox.SelectedIndex = 0;
  1139. }
  1140. private void PageEditControl_PageMoved(object sender, RoutedEventArgs e)
  1141. {
  1142. if (pdfViewControl == null || pdfViewControl.PDFView == null)
  1143. {
  1144. return;
  1145. }
  1146. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  1147. }
  1148. private void CPDFPageEditBarControl_PageEditEvent(object sender, string e)
  1149. {
  1150. pageEditControl.PageEdit(e);
  1151. }
  1152. private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
  1153. {
  1154. SaveFile();
  1155. }
  1156. private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
  1157. {
  1158. SaveAsFile();
  1159. }
  1160. private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
  1161. {
  1162. OpenFile();
  1163. }
  1164. #endregion
  1165. #region Change mode
  1166. /// <summary>
  1167. /// Actions performed when switching modes:
  1168. /// Separated into actions performed upon entering and exiting the mode.
  1169. /// </summary>
  1170. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  1171. {
  1172. if (isFirstLoad)
  1173. {
  1174. isFirstLoad = false;
  1175. return;
  1176. }
  1177. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  1178. //Behavior when exit mode
  1179. switch (currentMode)
  1180. {
  1181. case "Viewer":
  1182. break;
  1183. /*
  1184. * Behavior when exit Edit mode
  1185. * Clear and collapse the ToolBar
  1186. * Clear And collapse the property panel.
  1187. * Change the mousemode to PanTool
  1188. * Unsubscribe the event handles
  1189. * Clear the undo history
  1190. */
  1191. case "Annotations":
  1192. AnnotationBarControl.Visibility = Visibility.Collapsed;
  1193. AnnotationBarControl.ClearAllToolState();
  1194. pdfAnnotationControl.ClearPanel();
  1195. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1196. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1197. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
  1198. pdfAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar;
  1199. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
  1200. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1201. break;
  1202. /*
  1203. * Behavior when exit edit mode:
  1204. * Collapsed the ToolBar
  1205. * Highlight the link annotation
  1206. * Set the mode to not allow edit
  1207. * Set the mouse mode to PanTool
  1208. * Reload Document
  1209. * Unsubscribe click event.
  1210. * Clear And collapse the property panel.
  1211. * Clear and collapse the ToolBar
  1212. * Clear undo history
  1213. * CPDFDisplaySettingsControl
  1214. * Enable the configuration of the split view.
  1215. */
  1216. case "Content Editor":
  1217. PDFEditTool.Visibility = Visibility.Collapsed;
  1218. pdfViewControl.PDFView?.SetShowLink(true);
  1219. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None);
  1220. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1221. pdfViewControl.PDFView?.ReloadDocument();
  1222. pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler;
  1223. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1224. ClearPDFEditState();
  1225. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1226. if (displayPanel != null)
  1227. {
  1228. displayPanel.SetSplitContainerVisibility(Visibility.Visible);
  1229. }
  1230. break;
  1231. /*
  1232. * Behavior when exit form mode:
  1233. * Clear and collapsed the ToolBar
  1234. * Clear And Collapse the property panel.
  1235. * Unsubscribe event.
  1236. * Clear undo history
  1237. */
  1238. case "Forms":
  1239. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1240. FormBarControl.ClearAllToolState();
  1241. FormBarControl.Visibility = Visibility.Collapsed;
  1242. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool);
  1243. pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_FormActiveHandler;
  1244. pdfViewControl.PDFView.AnnotEditHandler -= PDFView_FormEditHandler;
  1245. pdfViewControl.PDFView?.UndoManager.ClearHistory();
  1246. break;
  1247. /*
  1248. * Unsubscribe event.
  1249. * Show Undo Redo Button
  1250. * Collapse PageEditBarControl
  1251. * Replace PDFGrid.Child to pdfViewControl.
  1252. * Reload Document
  1253. * Enable Button
  1254. */
  1255. case "Docs Editor":
  1256. pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit;
  1257. pageEditControl.PageMoved -= PageEditControl_PageMoved;
  1258. PageEditBarControl.PageEditEvent -= CPDFPageEditBarControl_PageEditEvent;
  1259. UndoRedoStackPanel.Visibility = Visibility.Visible;
  1260. PageEditBarControl.Visibility = Visibility.Collapsed;
  1261. PDFGrid.Child = pdfViewControl;
  1262. FloatPageTool.Visibility = Visibility.Visible;
  1263. pdfViewControl.PDFView.ReloadDocument();
  1264. LeftToolPanelButton.IsEnabled = true;
  1265. SearchButton.IsEnabled = true;
  1266. RightPanelButton.IsEnabled = true;
  1267. ViewSettingBtn.IsEnabled = true;
  1268. break;
  1269. default:
  1270. break;
  1271. }
  1272. /*
  1273. * Behavior when switching to Viewer mode:
  1274. * Hide the ToolBar
  1275. * Set mousemode to Viewer
  1276. */
  1277. if ((string)item.Content == "Viewer")
  1278. {
  1279. ToolBarContainer.Visibility = Visibility.Collapsed;
  1280. pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  1281. }
  1282. /*
  1283. * Behavior when switching to Annotation mode:
  1284. * Show the ToolBar
  1285. * Show the AnnotationBar
  1286. */
  1287. else if ((string)item.Content == "Annotations")
  1288. {
  1289. ToolBarContainer.Visibility = Visibility.Visible;
  1290. AnnotationBarControl.Visibility = Visibility.Visible;
  1291. pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
  1292. pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  1293. pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
  1294. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
  1295. }
  1296. /*
  1297. * Behavior when switching to Edit mode:
  1298. * Show the ToolBar
  1299. * Show the EditBar
  1300. * Set the mode to allow editing of both text and images
  1301. * Set the mouse mode to PDFEdit
  1302. * Reload Document
  1303. * Set the splite mode to None and unable the split setting
  1304. */
  1305. else if ((string)item.Content == "Content Editor")
  1306. {
  1307. ToolBarContainer.Visibility = Visibility.Visible;
  1308. PDFEditTool.Visibility = Visibility.Visible;
  1309. PageEditBarControl.Visibility = Visibility.Collapsed;
  1310. pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  1311. pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  1312. pdfViewControl.PDFView?.ReloadDocument();
  1313. pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
  1314. pdfViewControl.PDFView.SetSplitMode(SplitMode.None);
  1315. if (displayPanel != null)
  1316. {
  1317. displayPanel.SetSplitContainerVisibility(Visibility.Collapsed);
  1318. }
  1319. }
  1320. /*
  1321. * Behavior when switching to Form mode:
  1322. *
  1323. */
  1324. else if ((string)item.Content == "Forms")
  1325. {
  1326. ToolBarContainer.Visibility = Visibility.Visible;
  1327. FormBarControl.Visibility = Visibility.Visible;
  1328. pdfViewControl.PDFView?.SetShowLink(true);
  1329. pdfViewControl.PDFView?.SetMouseMode(MouseModes.FormEditTool);
  1330. pdfViewControl.PDFView?.ReloadDocument();
  1331. pdfViewControl.PDFView.AnnotActiveHandler += PDFView_FormActiveHandler;
  1332. pdfViewControl.PDFView.AnnotEditHandler += PDFView_FormEditHandler;
  1333. }
  1334. /*
  1335. *
  1336. */
  1337. else if ((string)item.Content == "Docs Editor")
  1338. {
  1339. ToolBarContainer.Visibility = Visibility.Visible;
  1340. if (pageEditControl == null)
  1341. {
  1342. pageEditControl = new CPDFPageEditControl();
  1343. }
  1344. pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit; ;
  1345. pageEditControl.PageMoved += PageEditControl_PageMoved; ;
  1346. PageEditBarControl.PageEditEvent += CPDFPageEditBarControl_PageEditEvent;
  1347. pageEditControl.LoadThumbnails(pdfViewControl.PDFView);
  1348. PDFGrid.Child = pageEditControl;
  1349. UndoRedoStackPanel.Visibility = Visibility.Collapsed;
  1350. PageEditBarControl.Visibility = Visibility.Visible;
  1351. LeftToolPanelButton.IsChecked = false;
  1352. LeftToolPanelButton.IsEnabled = false;
  1353. SearchButton.IsEnabled = false;
  1354. RightPanelButton.IsChecked = false;
  1355. RightPanelButton.IsEnabled = false;
  1356. ViewSettingBtn.IsChecked = false;
  1357. ViewSettingBtn.IsEnabled = false;
  1358. ExpandLeftPanel(false);
  1359. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  1360. FloatPageTool.Visibility = Visibility.Collapsed;
  1361. }
  1362. currentMode = (string)item.Content;
  1363. }
  1364. #endregion
  1365. }
  1366. }