MainPage.xaml.cs 64 KB

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