CPDFViewerTool.Command.cs 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFAnnotation.Form;
  4. using ComPDFKit.PDFDocument;
  5. using ComPDFKit.PDFPage;
  6. using ComPDFKit.PDFPage.Edit;
  7. using ComPDFKit.Tool.DrawTool;
  8. using ComPDFKit.Tool.Help;
  9. using ComPDFKit.Tool.UndoManger;
  10. using ComPDFKit.Viewer.Annot;
  11. using ComPDFKit.Viewer.Helper;
  12. using ComPDFKitViewer;
  13. using ComPDFKitViewer.BaseObject;
  14. using ComPDFKitViewer.Helper;
  15. using ComPDFKitViewer.Widget;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Dynamic;
  19. using System.Text;
  20. using System.Windows;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. namespace ComPDFKit.Tool
  25. {
  26. partial class CPDFViewerTool
  27. {
  28. public class CommandData
  29. {
  30. public ExecutedRoutedEventArgs uIEventArgs { get; set; }
  31. public bool StartCommand { get; set; }
  32. public CPDFEditType PDFEditType { get; set; }
  33. public AnnotParam CurrentParam { get; set; } = null;
  34. }
  35. /// <summary>
  36. /// System provided related command time notification
  37. /// </summary>
  38. public event EventHandler<CommandData> CommandExecutedHandler;
  39. public AnnotParam PasteParam { get; set; }
  40. internal class PDFEditCommandData
  41. {
  42. public string TextContent { get; set; }
  43. /// <summary>
  44. /// Original rectangle in PDF (72DPI)
  45. /// </summary>
  46. public Rect PDFRect { get; set; }
  47. public bool EditAreaCopied { get; set; }
  48. public CPDFEditType EditType { get; set; }
  49. public CPDFCopyEditArea CopyArea { get; set; }
  50. public CPDFEditPage EditPage { get; set; }
  51. }
  52. internal Point rightPressPoint = new Point(-1, -1);
  53. internal static List<PDFEditCommandData> lastPDFEditArgsList { get; private set; } = new List<PDFEditCommandData>();
  54. internal static List<AnnotParam> lastAnnotList { get; private set; } = new List<AnnotParam>();
  55. internal void BindCommand()
  56. {
  57. CommandBinding copyBind = new CommandBinding(ApplicationCommands.Copy);
  58. copyBind.CanExecute += CommandCanExecute;
  59. copyBind.Executed += CommandBind_Executed;
  60. CommandBindings.Add(copyBind);
  61. CommandBinding cutBind = new CommandBinding(ApplicationCommands.Cut);
  62. cutBind.CanExecute += CommandCanExecute;
  63. cutBind.Executed += CommandBind_Executed;
  64. CommandBindings.Add(cutBind);
  65. CommandBinding pasteBind = new CommandBinding(ApplicationCommands.Paste);
  66. pasteBind.CanExecute += CommandCanExecute;
  67. pasteBind.Executed += CommandBind_Executed;
  68. CommandBindings.Add(pasteBind);
  69. CommandBinding deleteBind = new CommandBinding(ApplicationCommands.Delete);
  70. deleteBind.CanExecute += CommandCanExecute;
  71. deleteBind.Executed += CommandBind_Executed;
  72. CommandBindings.Add(deleteBind);
  73. CommandBinding playBind = new CommandBinding(MediaCommands.Play);
  74. playBind.CanExecute += PlayBind_CanExecute;
  75. playBind.Executed += PlayBind_Executed;
  76. CommandBindings.Add(playBind);
  77. CommandBinding pastWithoutStyle = new CommandBinding(CustomCommands.PasteWithoutStyle);
  78. pastWithoutStyle.CanExecute += CommandCanExecute;
  79. pastWithoutStyle.Executed += CommandBind_Executed;
  80. CommandBindings.Add(pastWithoutStyle);
  81. CommandBinding selectAllBind = new CommandBinding(ApplicationCommands.SelectAll);
  82. selectAllBind.CanExecute += CommandCanExecute;
  83. selectAllBind.Executed += CommandBind_Executed;
  84. CommandBindings.Add(selectAllBind);
  85. }
  86. private void PlayBind_Executed(object sender, ExecutedRoutedEventArgs e)
  87. {
  88. BaseAnnot cmdArgs = e.Parameter as BaseAnnot;
  89. if (cmdArgs != null && cmdArgs.GetAnnotData() != null)
  90. {
  91. if (cmdArgs is SoundAnnot)
  92. {
  93. (cmdArgs as SoundAnnot).Play();
  94. }
  95. if (cmdArgs is MovieAnnot)
  96. {
  97. (cmdArgs as MovieAnnot).Play();
  98. }
  99. }
  100. e.Handled = true;
  101. }
  102. private void PlayBind_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  103. {
  104. e.CanExecute = false;
  105. BaseAnnot cmdArgs = e.Parameter as BaseAnnot;
  106. if (cmdArgs != null && cmdArgs.GetAnnotData() != null)
  107. {
  108. e.CanExecute = true;
  109. }
  110. e.Handled = true;
  111. e.ContinueRouting = false;
  112. }
  113. public void ReBindCommand(List<CommandBinding> commandBindings)
  114. {
  115. CommandBindings.Clear();
  116. foreach (CommandBinding binding in commandBindings)
  117. {
  118. CommandBindings.Add(binding);
  119. }
  120. }
  121. private bool IsCanDoCommand = true;
  122. public void CanDoCommand()
  123. {
  124. IsCanDoCommand = true;
  125. }
  126. public void NotDoCommand()
  127. {
  128. IsCanDoCommand = false;
  129. }
  130. private void CommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
  131. {
  132. if (e.Command is RoutedUICommand uiCommand)
  133. {
  134. switch (currentModel)
  135. {
  136. case ToolType.Viewer:
  137. CheckViewerCommandStatus(uiCommand, e);
  138. break;
  139. case ToolType.CreateAnnot:
  140. case ToolType.WidgetEdit:
  141. case ToolType.Pan:
  142. CheckAnnotCommandStatus(uiCommand, e);
  143. break;
  144. case ToolType.Customize:
  145. break;
  146. case ToolType.ContentEdit:
  147. {
  148. CheckPDFEditCommandStatus(uiCommand, e);
  149. }
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. }
  156. private void CommandBind_Executed(object sender, ExecutedRoutedEventArgs e)
  157. {
  158. RoutedUICommand uiCommand = e.Command as RoutedUICommand;
  159. if (uiCommand != null)
  160. {
  161. try
  162. {
  163. PasteParam = null;
  164. CommandData commandData = new CommandData();
  165. commandData.uIEventArgs = e;
  166. commandData.StartCommand = true;
  167. CPDFEditType editType = CPDFEditType.None;
  168. CommandExecutedHandler?.Invoke(this, commandData);
  169. if (e.Handled)
  170. {
  171. return;
  172. }
  173. if (!IsCanDoCommand)
  174. {
  175. CanDoCommand();
  176. return;
  177. }
  178. switch (currentModel)
  179. {
  180. case ToolType.Viewer:
  181. ExecuteViewerCommand(uiCommand);
  182. break;
  183. case ToolType.CreateAnnot:
  184. case ToolType.WidgetEdit:
  185. case ToolType.Pan:
  186. ExecuteAnnotCommand(uiCommand);
  187. break;
  188. case ToolType.ContentEdit:
  189. {
  190. ExecutePDFEditCommand(uiCommand, out editType);
  191. }
  192. break;
  193. case ToolType.Customize:
  194. break;
  195. default:
  196. break;
  197. }
  198. commandData.StartCommand = false;
  199. commandData.PDFEditType = editType;
  200. commandData.CurrentParam = PasteParam;
  201. CommandExecutedHandler?.Invoke(this, commandData);
  202. }
  203. catch (Exception ex)
  204. {
  205. }
  206. }
  207. }
  208. public void SetPastePoint(Point point)
  209. {
  210. rightPressPoint = point;
  211. }
  212. #region ContentEdit
  213. private void CheckPDFEditCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  214. {
  215. switch (uiCommand.Name)
  216. {
  217. case "Copy":
  218. case "Cut":
  219. case "Delete":
  220. case "SelectAll":
  221. {
  222. if (currentEditAreaObject != null)
  223. {
  224. e.CanExecute = true;
  225. }
  226. break;
  227. }
  228. case "Paste":
  229. case "PasteWithoutStyle":
  230. foreach (PDFEditCommandData checkItem in lastPDFEditArgsList)
  231. {
  232. if (checkItem.EditType == CPDFEditType.EditText && string.IsNullOrEmpty(checkItem.TextContent) == false)
  233. {
  234. e.CanExecute = true;
  235. break;
  236. }
  237. if (checkItem.EditType == CPDFEditType.EditImage && checkItem.EditAreaCopied)
  238. {
  239. e.CanExecute = true;
  240. break;
  241. }
  242. }
  243. try
  244. {
  245. if (Clipboard.ContainsText())
  246. {
  247. if (!string.IsNullOrEmpty(Clipboard.GetText()))
  248. {
  249. e.CanExecute = true;
  250. }
  251. }
  252. if (Clipboard.ContainsImage())
  253. {
  254. if (Clipboard.GetImage() != null)
  255. {
  256. e.CanExecute = true;
  257. }
  258. }
  259. }
  260. catch (Exception ex)
  261. {
  262. }
  263. break;
  264. }
  265. }
  266. private void ExecutePDFEditCommand(RoutedUICommand uiCommand, out CPDFEditType editType)
  267. {
  268. editType = CPDFEditType.None;
  269. switch (uiCommand.Name)
  270. {
  271. case "Copy":
  272. CopyPDFEditData(out editType);
  273. break;
  274. case "Cut":
  275. CopyPDFEditData(out editType);
  276. DelPDFEditData(out editType);
  277. break;
  278. case "Delete":
  279. DelPDFEditData(out editType);
  280. break;
  281. case "Paste":
  282. SetEditCopyData();
  283. if (lastPDFEditArgsList.Count > 0)
  284. {
  285. PastePDFEditData(out editType);
  286. PDFViewer.UpdateRenderFrame();
  287. }
  288. break;
  289. case "PasteWithoutStyle":
  290. SetEditCopyData();
  291. if (lastPDFEditArgsList.Count > 0)
  292. {
  293. PastePDFEditData(out editType, false);
  294. PDFViewer.UpdateRenderFrame();
  295. }
  296. break;
  297. case "SelectAll":
  298. SelectAllPDFEditData(out editType);
  299. break;
  300. }
  301. }
  302. private void SetEditCopyData()
  303. {
  304. try
  305. {
  306. if (Clipboard.ContainsText())
  307. {
  308. string copyText = Clipboard.GetText();
  309. bool findCopy = false;
  310. if (lastPDFEditArgsList != null && lastPDFEditArgsList.Count > 0)
  311. {
  312. foreach (PDFEditCommandData checkItem in lastPDFEditArgsList)
  313. {
  314. if (checkItem.EditType == CPDFEditType.EditText && copyText == checkItem.TextContent)
  315. {
  316. findCopy = true;
  317. }
  318. }
  319. }
  320. if (findCopy == false)
  321. {
  322. lastPDFEditArgsList?.Clear();
  323. if (string.IsNullOrEmpty(copyText) == false)
  324. {
  325. PDFEditCommandData commandData = new PDFEditCommandData();
  326. commandData.EditType = CPDFEditType.EditText;
  327. commandData.TextContent = copyText;
  328. int PageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  329. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(PageIndex);
  330. Rect rect = render.PaintRect;
  331. Point centerPoint = new Point(
  332. rect.Width / PDFViewer.CurrentRenderFrame.ZoomFactor / 2,
  333. rect.Height / PDFViewer.CurrentRenderFrame.ZoomFactor / 2);
  334. commandData.PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(centerPoint.X, centerPoint.Y, 0, 0));
  335. lastPDFEditArgsList.Add(commandData);
  336. }
  337. }
  338. }
  339. else if (Clipboard.ContainsImage())
  340. {
  341. if (Clipboard.GetImage() != null)
  342. {
  343. BitmapSource bitmapSource = Clipboard.GetImage();
  344. PDFEditCommandData commandData = new PDFEditCommandData();
  345. commandData.EditType = CPDFEditType.EditImage;
  346. int PageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  347. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(PageIndex);
  348. Rect rect = render.PaintRect;
  349. Point centerPoint = new Point(
  350. rect.Width / PDFViewer.CurrentRenderFrame.ZoomFactor / 2,
  351. rect.Height / PDFViewer.CurrentRenderFrame.ZoomFactor / 2);
  352. commandData.PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(centerPoint.X, centerPoint.Y, bitmapSource.PixelWidth, bitmapSource.PixelHeight));
  353. lastPDFEditArgsList.Clear();
  354. lastPDFEditArgsList.Add(commandData);
  355. }
  356. }
  357. }
  358. catch (Exception ex)
  359. {
  360. }
  361. }
  362. private void CopyPDFEditData(out CPDFEditType editType)
  363. {
  364. editType = CPDFEditType.None;
  365. if (CPDFEditPage.CopyPage != null)
  366. {
  367. CPDFEditPage.CopyPage.ReleaseCopyEditAreaList();
  368. }
  369. try
  370. {
  371. lastPDFEditArgsList.Clear();
  372. Clipboard.Clear();
  373. if (currentEditAreaObject != null)
  374. {
  375. PDFEditCommandData commandData = new PDFEditCommandData();
  376. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  377. {
  378. editType = CPDFEditType.EditText;
  379. CPDFEditTextArea editTextArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  380. commandData.TextContent = editTextArea.SelectText;
  381. if (selectAllCharsForLine || string.IsNullOrEmpty(commandData.TextContent))
  382. {
  383. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  384. commandData.EditType = CPDFEditType.EditText;
  385. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  386. if (commandData.EditAreaCopied)
  387. {
  388. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  389. CPDFCopyEditArea CopyArea = copyList[copyList.Count - 1];
  390. commandData.TextContent = CopyArea.GetCopyTextAreaContent();
  391. commandData.CopyArea = CopyArea;
  392. commandData.EditPage = editPage;
  393. }
  394. }
  395. try
  396. {
  397. Clipboard.Clear();
  398. Clipboard.SetText(commandData.TextContent);
  399. Clipboard.Flush();
  400. }
  401. catch (Exception)
  402. {
  403. }
  404. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  405. lastPDFEditArgsList.Add(commandData);
  406. }
  407. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  408. {
  409. editType = CPDFEditType.EditImage;
  410. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  411. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  412. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  413. commandData.EditType = CPDFEditType.EditImage;
  414. if (commandData.EditAreaCopied)
  415. {
  416. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  417. commandData.CopyArea = copyList[copyList.Count - 1];
  418. commandData.EditPage = editPage;
  419. lastPDFEditArgsList.Add(commandData);
  420. }
  421. }
  422. }
  423. }
  424. catch
  425. {
  426. }
  427. }
  428. private void DelPDFEditData(out CPDFEditType editType)
  429. {
  430. editType = CPDFEditType.None;
  431. if (currentEditAreaObject != null)
  432. {
  433. GroupHistory groupHistory = new GroupHistory();
  434. PDFEditHistory editHistory = new PDFEditHistory();
  435. editHistory.EditPage = currentEditAreaObject.cPDFEditPage;
  436. editHistory.PageIndex = currentEditAreaObject.PageIndex;
  437. groupHistory.Histories.Add(editHistory);
  438. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  439. {
  440. editType = CPDFEditType.EditText;
  441. CPDFEditTextArea editTextArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  442. string selectContent = editTextArea.SelectText;
  443. if (string.IsNullOrEmpty(selectContent) == false && !selectAllCharsForLine)
  444. {
  445. DeleteChars();
  446. EndEdit();
  447. }
  448. else
  449. {
  450. if (string.IsNullOrEmpty(selectContent))
  451. {
  452. editTextArea.SelectAllChars();
  453. }
  454. RemoveTextBlock();
  455. }
  456. PDFEditHistory deleteHistory = new PDFEditHistory();
  457. deleteHistory.EditPage = currentEditAreaObject.cPDFEditPage;
  458. deleteHistory.PageIndex = currentEditAreaObject.PageIndex;
  459. groupHistory.Histories.Add(deleteHistory);
  460. }
  461. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  462. {
  463. editType = CPDFEditType.EditImage;
  464. RemoveImageBlock();
  465. }
  466. //After removing the data, you need to get the data again.
  467. PDFViewer.UpdateRenderFrame();
  468. if (PDFViewer != null && PDFViewer.UndoManager != null)
  469. {
  470. PDFViewer.UndoManager.AddHistory(groupHistory);
  471. }
  472. }
  473. }
  474. private void PastePDFEditData(out CPDFEditType editType, bool pasteMatchStyle = true)
  475. {
  476. editType = CPDFEditType.None;
  477. double left = 0;
  478. double right = 0;
  479. double top = 0;
  480. double bottom = 0;
  481. bool initial = false;
  482. #region Calculate the maximum rectangle
  483. foreach (PDFEditCommandData commandData in lastPDFEditArgsList)
  484. {
  485. if (initial == false)
  486. {
  487. left = commandData.PDFRect.Left;
  488. right = commandData.PDFRect.Right;
  489. top = commandData.PDFRect.Top;
  490. bottom = commandData.PDFRect.Bottom;
  491. initial = true;
  492. continue;
  493. }
  494. left = Math.Min(left, commandData.PDFRect.Left);
  495. right = Math.Max(right, commandData.PDFRect.Right);
  496. top = Math.Min(top, commandData.PDFRect.Top);
  497. bottom = Math.Max(bottom, commandData.PDFRect.Bottom);
  498. }
  499. left = DpiHelper.PDFNumToStandardNum(left);
  500. right = DpiHelper.PDFNumToStandardNum(right);
  501. top = DpiHelper.PDFNumToStandardNum(top);
  502. bottom = DpiHelper.PDFNumToStandardNum(bottom);
  503. int offsetX = 25;
  504. int offsetY = 25;
  505. Point hoverPoint = rightPressPoint;
  506. rightPressPoint = new Point(-1, -1);
  507. int pageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  508. // Judge whether the right-click coordinate is on the page.
  509. PDFViewer.GetePointToPage(hoverPoint, out RenderData renderData, out Point pagePoint);
  510. if (renderData != null)
  511. {
  512. //Calculate the offset from the upper left corner.
  513. offsetX = (int)(pagePoint.X / currentZoom - left);
  514. offsetY = (int)(pagePoint.Y / currentZoom - top);
  515. if (left + offsetX < 0)
  516. {
  517. offsetX = (int)-left;
  518. }
  519. if (right + offsetX > renderData.PageBound.Width / currentZoom)
  520. {
  521. offsetX = (int)(renderData.PageBound.Width / currentZoom - right);
  522. }
  523. if (top + offsetY < 0)
  524. {
  525. offsetY = (int)-top;
  526. }
  527. if (bottom + offsetY > renderData.PageBound.Height / currentZoom)
  528. {
  529. offsetY = (int)(renderData.PageBound.Height / currentZoom - bottom);
  530. }
  531. pageIndex = renderData.PageIndex;
  532. }
  533. else
  534. {
  535. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(pageIndex);
  536. //Max rectangle (standard DPI).
  537. Rect maxRect = new Rect((int)left + render.PaintRect.Left / currentZoom, (int)top + render.PaintRect.Top / currentZoom, (int)(right - left), (int)(bottom - top));
  538. //Center point of the visible range.
  539. Point centerPoint = new Point(
  540. render.PaintRect.Left / currentZoom + render.PaintRect.Width / currentZoom / 2,
  541. render.PaintRect.Top / currentZoom + render.PaintRect.Height / currentZoom / 2);
  542. //Visible range.
  543. Rect checkRect = new Rect(
  544. render.PaintRect.Left / currentZoom,
  545. render.PaintRect.Top / currentZoom,
  546. render.PaintRect.Width / currentZoom,
  547. render.PaintRect.Height / currentZoom);
  548. if (!checkRect.IntersectsWith(maxRect))
  549. {
  550. offsetX = (int)(left - centerPoint.X);
  551. offsetY = (int)(top - centerPoint.Y);
  552. }
  553. if (left + offsetX < 0)
  554. {
  555. offsetX = (int)-left;
  556. }
  557. if (right + offsetX > render.RenderRect.Width / currentZoom)
  558. {
  559. offsetX = (int)(render.RenderRect.Width / currentZoom / 2 - right);
  560. }
  561. if (top + offsetY < 0)
  562. {
  563. offsetY = (int)-top;
  564. }
  565. if (bottom + offsetY > render.RenderRect.Height / currentZoom)
  566. {
  567. offsetY = (int)(render.RenderRect.Height / currentZoom / 2 - bottom);
  568. }
  569. }
  570. #endregion
  571. foreach (PDFEditCommandData commandData in lastPDFEditArgsList)
  572. {
  573. GroupHistory groupHistory = new GroupHistory();
  574. CPDFPage docPage = PDFViewer.GetDocument().PageAtIndex(pageIndex);
  575. CPDFEditPage editPage = docPage.GetEditPage();
  576. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(pageIndex);
  577. Rect offsetRect = AddPasteOffset(commandData.PDFRect, (int)DpiHelper.StandardNumToPDFNum(offsetX), (int)DpiHelper.StandardNumToPDFNum(offsetY), new Size(DpiHelper.StandardNumToPDFNum(render.PageBound.Width / currentZoom), DpiHelper.StandardNumToPDFNum(render.PageBound.Height / currentZoom)));
  578. commandData.PDFRect = offsetRect;
  579. if (commandData.EditType == CPDFEditType.EditText)
  580. {
  581. editType = CPDFEditType.EditText;
  582. if (pasteMatchStyle && commandData.EditAreaCopied)
  583. {
  584. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  585. if (editPage.CanUndo())
  586. {
  587. PDFEditHistory editHistory = new PDFEditHistory();
  588. editHistory.EditPage = editPage;
  589. editHistory.PageIndex = pageIndex;
  590. groupHistory.Histories.Add(editHistory);
  591. CPDFViewer pdfViewer = GetCPDFViewer();
  592. if (pdfViewer != null && pdfViewer.UndoManager != null)
  593. {
  594. pdfViewer.UndoManager.AddHistory(groupHistory);
  595. }
  596. }
  597. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  598. }
  599. else
  600. {
  601. PDFEditHistory editHistory = new PDFEditHistory();
  602. if (!string.IsNullOrEmpty(commandData.TextContent))
  603. {
  604. EditAreaObject editAreaObject = GetHitTestAreaObject(hoverPoint);
  605. if (editAreaObject != null && editAreaObject.cPDFEditArea is CPDFEditTextArea)
  606. {
  607. CPDFEditTextArea TextArea = editAreaObject.cPDFEditArea as CPDFEditTextArea;
  608. if (TextArea.SelectLineRects.Count > 0)
  609. {
  610. TextArea.DeleteChars();
  611. TextArea.ClearSelectChars();
  612. PDFEditHistory deleteHistory = new PDFEditHistory();
  613. deleteHistory.EditPage = editPage;
  614. deleteHistory.PageIndex = pageIndex;
  615. groupHistory.Histories.Add(deleteHistory);
  616. }
  617. TextArea.InsertText(commandData.TextContent);
  618. editHistory.EditPage = editPage;
  619. editHistory.PageIndex = pageIndex;
  620. groupHistory.Histories.Add(editHistory);
  621. CPDFViewer pdfViewer = GetCPDFViewer();
  622. if (pdfViewer != null && pdfViewer.UndoManager != null)
  623. {
  624. pdfViewer.UndoManager.AddHistory(groupHistory);
  625. }
  626. }
  627. else
  628. {
  629. PDFEditHistory createHistory = new PDFEditHistory();
  630. CPDFEditTextArea editArea = editPage.CreateNewTextArea(DataConversionForWPF.RectConversionForCRect(offsetRect), "Helvetica", 14, new byte[3] { 0, 0, 0 });
  631. createHistory.EditPage = editPage;
  632. createHistory.PageIndex = pageIndex;
  633. groupHistory.Histories.Add(createHistory);
  634. if (editArea != null)
  635. {
  636. if (editArea.InsertText(commandData.TextContent))
  637. {
  638. editHistory.EditPage = editPage;
  639. editHistory.PageIndex = pageIndex;
  640. groupHistory.Histories.Add(editHistory);
  641. }
  642. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  643. }
  644. CPDFViewer pdfViewer = GetCPDFViewer();
  645. if (pdfViewer != null && pdfViewer.UndoManager != null)
  646. {
  647. pdfViewer.UndoManager.AddHistory(groupHistory);
  648. }
  649. }
  650. }
  651. }
  652. }
  653. if (commandData.EditType == CPDFEditType.EditImage)
  654. {
  655. editType = CPDFEditType.EditImage;
  656. PDFEditHistory editHistory = new PDFEditHistory();
  657. if (commandData.EditAreaCopied)
  658. {
  659. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  660. editHistory.EditPage = editPage;
  661. editHistory.PageIndex = pageIndex;
  662. CPDFViewer pdfViewer = GetCPDFViewer();
  663. if (pdfViewer != null && pdfViewer.UndoManager != null)
  664. {
  665. pdfViewer.UndoManager.AddHistory(editHistory);
  666. }
  667. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  668. }
  669. else
  670. {
  671. BitmapSource bitmapSource = BinaryStructConverter.ImageFromClipboardDib();
  672. if (bitmapSource != null)
  673. {
  674. PDFEditHistory createHistory = new PDFEditHistory();
  675. byte[] imageData = new byte[bitmapSource.PixelWidth * bitmapSource.PixelHeight * 4];
  676. if (bitmapSource.Format != PixelFormats.Bgra32)
  677. {
  678. FormatConvertedBitmap covert = new FormatConvertedBitmap(bitmapSource, PixelFormats.Bgra32, bitmapSource.Palette, 0);
  679. covert.CopyPixels(imageData, bitmapSource.PixelWidth * 4, 0);
  680. }
  681. else
  682. {
  683. bitmapSource.CopyPixels(imageData, bitmapSource.PixelWidth * 4, 0);
  684. }
  685. CPDFEditImageArea editArea = editPage.CreateNewImageArea(DataConversionForWPF.RectConversionForCRect(offsetRect), imageData, bitmapSource.PixelWidth, bitmapSource.PixelHeight);
  686. createHistory.EditPage = editPage;
  687. createHistory.PageIndex = pageIndex;
  688. groupHistory.Histories.Add(createHistory);
  689. if (editArea != null)
  690. {
  691. editHistory.EditPage = editPage;
  692. editHistory.PageIndex = pageIndex;
  693. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  694. }
  695. CPDFViewer pdfViewer = GetCPDFViewer();
  696. if (pdfViewer != null && pdfViewer.UndoManager != null)
  697. {
  698. pdfViewer.UndoManager.AddHistory(groupHistory);
  699. }
  700. }
  701. }
  702. }
  703. editPage.EndEdit();
  704. }
  705. }
  706. private void SelectAllPDFEditData(out CPDFEditType editType)
  707. {
  708. editType = CPDFEditType.None;
  709. if (currentEditAreaObject != null && currentEditAreaObject.cPDFEditPage != null)
  710. {
  711. CPDFEditTextArea textArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  712. textArea.SelectAllChars();
  713. editType = CPDFEditType.EditText;
  714. PDFViewer.UpdateRenderFrame();
  715. }
  716. }
  717. private Rect AddPasteOffset(Rect clientRect, int offsetX, int offsetY, Size pageSize)
  718. {
  719. clientRect.X += offsetX;
  720. clientRect.Y += offsetY;
  721. if (clientRect.Left < 0)
  722. {
  723. clientRect.X = 0;
  724. }
  725. if (clientRect.Top < 0)
  726. {
  727. clientRect.Y = 0;
  728. }
  729. if (clientRect.Right > pageSize.Width)
  730. {
  731. clientRect.X = pageSize.Width - Math.Min(clientRect.Width, pageSize.Width);
  732. }
  733. if (clientRect.Bottom > pageSize.Height)
  734. {
  735. clientRect.Y = pageSize.Height - Math.Min(clientRect.Height, pageSize.Height);
  736. }
  737. return clientRect;
  738. }
  739. #endregion
  740. #region Annot
  741. private bool CheckCacheHitTestAnnot(string cmdName = "")
  742. {
  743. if (cacheHitTestAnnot == null)
  744. {
  745. return false;
  746. }
  747. AnnotData hitData = cacheHitTestAnnot.GetAnnotData();
  748. if (hitData == null)
  749. {
  750. return false;
  751. }
  752. if (hitData.Annot == null)
  753. {
  754. return false;
  755. }
  756. if (cmdName == "Delete" && hitData.Annot is CPDFSignatureWidget)
  757. {
  758. CPDFSignatureWidget signAnnot = hitData.Annot as CPDFSignatureWidget;
  759. if (signAnnot != null && signAnnot.IsSigned())
  760. {
  761. return false;
  762. }
  763. }
  764. return true;
  765. }
  766. private bool CheckAnnotCanDoCopyCut(bool isCut = false)
  767. {
  768. if (!CheckCacheHitTestAnnot(isCut ? "Delete" : ""))
  769. {
  770. return false;
  771. }
  772. if (cacheHitTestAnnot.GetAnnotData().Annot.GetIsLocked() && isCut)
  773. {
  774. return false;
  775. }
  776. switch (cacheHitTestAnnot.CurrentType)
  777. {
  778. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  779. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  780. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  781. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  782. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  783. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  784. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  785. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  786. return false;
  787. default:
  788. break;
  789. }
  790. return true;
  791. }
  792. private void CheckViewerCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  793. {
  794. switch (uiCommand.Name)
  795. {
  796. case "Copy":
  797. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  798. if (!e.CanExecute && textSelectInfo != null)
  799. {
  800. foreach (int key in textSelectInfo.PageSelectText.Keys)
  801. {
  802. if (textSelectInfo.PageSelectText[key] != string.Empty)
  803. {
  804. e.CanExecute = true;
  805. break;
  806. }
  807. }
  808. }
  809. break;
  810. default:
  811. return;
  812. }
  813. }
  814. private void CheckAnnotCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  815. {
  816. switch (uiCommand.Name)
  817. {
  818. case "Copy":
  819. e.CanExecute = CheckAnnotCanDoCopyCut();
  820. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  821. if (!e.CanExecute && textSelectInfo != null)
  822. {
  823. foreach (int key in textSelectInfo.PageSelectText.Keys)
  824. {
  825. if (textSelectInfo.PageSelectText[key] != string.Empty)
  826. {
  827. e.CanExecute = true;
  828. break;
  829. }
  830. }
  831. }
  832. break;
  833. case "Cut":
  834. e.CanExecute = CheckAnnotCanDoCopyCut(true);
  835. break;
  836. case "Delete":
  837. e.CanExecute = CheckCacheHitTestAnnot("Delete");
  838. break;
  839. case "Paste":
  840. if (lastAnnotList.Count > 0)
  841. {
  842. e.CanExecute = true;
  843. }
  844. break;
  845. }
  846. }
  847. private void ExecuteViewerCommand(RoutedUICommand uiCommand)
  848. {
  849. switch (uiCommand.Name)
  850. {
  851. case "Copy":
  852. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  853. if (textSelectInfo != null)
  854. {
  855. StringBuilder copyTextBuilder = new StringBuilder();
  856. foreach (int key in textSelectInfo.PageSelectText.Keys)
  857. {
  858. if (textSelectInfo.PageSelectText[key] != string.Empty)
  859. {
  860. copyTextBuilder.Append(textSelectInfo.PageSelectText[key]);
  861. }
  862. }
  863. if (copyTextBuilder.Length > 0)
  864. {
  865. try
  866. {
  867. Clipboard.Clear();
  868. Clipboard.SetText(copyTextBuilder.ToString());
  869. }
  870. catch (Exception)
  871. {
  872. }
  873. }
  874. }
  875. break;
  876. default:
  877. return;
  878. }
  879. }
  880. private void ExecuteAnnotCommand(RoutedUICommand uiCommand)
  881. {
  882. switch (uiCommand.Name)
  883. {
  884. case "Copy":
  885. if (cacheHitTestAnnot != null)
  886. {
  887. lastAnnotList.Clear();
  888. AnnotParam annotParam = ParamConverter.AnnotConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  889. lastAnnotList.Add(annotParam);
  890. }
  891. if (cacheMoveWidget != null)
  892. {
  893. lastAnnotList.Clear();
  894. AnnotParam annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  895. lastAnnotList.Add(annotParam);
  896. }
  897. else
  898. {
  899. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  900. if (textSelectInfo != null)
  901. {
  902. StringBuilder copyTextBuilder = new StringBuilder();
  903. foreach (int key in textSelectInfo.PageSelectText.Keys)
  904. {
  905. if (textSelectInfo.PageSelectText[key] != string.Empty)
  906. {
  907. copyTextBuilder.Append(textSelectInfo.PageSelectText[key]);
  908. }
  909. }
  910. if (copyTextBuilder.Length > 0)
  911. {
  912. try
  913. {
  914. Clipboard.Clear();
  915. Clipboard.SetText(copyTextBuilder.ToString());
  916. }
  917. catch (Exception)
  918. {
  919. }
  920. }
  921. }
  922. }
  923. break;
  924. case "Cut":
  925. if (cacheHitTestAnnot != null)
  926. {
  927. lastAnnotList.Clear();
  928. AnnotParam annotParam;
  929. if (cacheHitTestAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  930. {
  931. annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  932. }
  933. else
  934. {
  935. annotParam = ParamConverter.AnnotConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  936. }
  937. lastAnnotList.Add(annotParam);
  938. }
  939. if (cacheMoveWidget != null)
  940. {
  941. lastAnnotList.Clear();
  942. AnnotParam annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  943. lastAnnotList.Add(annotParam);
  944. }
  945. DeleteAnnotData();
  946. PDFViewer.UpdateAnnotFrame();
  947. break;
  948. case "Delete":
  949. DeleteAnnotData();
  950. PDFViewer.UpdateAnnotFrame();
  951. break;
  952. case "Paste":
  953. PasteAnnotData();
  954. PDFViewer.UpdateAnnotFrame();
  955. break;
  956. }
  957. }
  958. private void PasteAnnotData(bool pasteMatchStyle = false)
  959. {
  960. AnnotHistory annotHistory = null;
  961. foreach (AnnotParam item in lastAnnotList)
  962. {
  963. if (item == null)
  964. {
  965. continue;
  966. }
  967. Point point = rightPressPoint;
  968. rightPressPoint = new Point(-1, -1);
  969. switch (item.CurrentType)
  970. {
  971. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  972. {
  973. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  974. Rect rect;
  975. annotHistory = new StickyNoteAnnotHistory();
  976. annotHistory.PDFDoc = cPDFDocument;
  977. int index = -1;
  978. if (point.Equals(new Point(-1, -1)))
  979. {
  980. index = item.PageIndex;
  981. rect = new Rect(
  982. (item.ClientRect.left + 25),
  983. (item.ClientRect.top + 25),
  984. item.ClientRect.width(),
  985. item.ClientRect.height()
  986. );
  987. }
  988. else
  989. {
  990. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  991. CRect cRect = item.ClientRect;
  992. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  993. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  994. rect = new Rect(
  995. (pdfPoint.X - cRect.width() / 2),
  996. (pdfPoint.Y - cRect.height() / 2),
  997. cRect.width(),
  998. cRect.height()
  999. );
  1000. }
  1001. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1002. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1003. if (cPDFPage == null)
  1004. return;
  1005. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1006. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1007. cPDFAnnotation.SetRect(setRect);
  1008. cPDFAnnotation.UpdateAp();
  1009. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1010. (annotHistory as StickyNoteAnnotHistory).CurrentParam = (StickyNoteParam)annotParam;
  1011. }
  1012. break;
  1013. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  1014. {
  1015. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1016. Rect rect;
  1017. annotHistory = new LinkAnnotHistory();
  1018. annotHistory.PDFDoc = cPDFDocument;
  1019. int index = -1;
  1020. if (point.Equals(new Point(-1, -1)))
  1021. {
  1022. index = item.PageIndex;
  1023. rect = new Rect(
  1024. (item.ClientRect.left + 25),
  1025. (item.ClientRect.top + 25),
  1026. item.ClientRect.width(),
  1027. item.ClientRect.height()
  1028. );
  1029. }
  1030. else
  1031. {
  1032. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1033. CRect cRect = item.ClientRect;
  1034. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1035. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1036. rect = new Rect(
  1037. (pdfPoint.X - cRect.width() / 2),
  1038. (pdfPoint.Y - cRect.height() / 2),
  1039. cRect.width(),
  1040. cRect.height()
  1041. );
  1042. }
  1043. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1044. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1045. if (cPDFPage == null)
  1046. return;
  1047. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1048. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1049. cPDFAnnotation.SetRect(setRect);
  1050. cPDFAnnotation.UpdateAp();
  1051. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1052. (annotHistory as LinkAnnotHistory).CurrentParam = (LinkParam)annotParam;
  1053. }
  1054. break;
  1055. case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
  1056. {
  1057. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1058. Rect rect;
  1059. annotHistory = new FreeTextAnnotHistory();
  1060. annotHistory.PDFDoc = cPDFDocument;
  1061. int index = -1;
  1062. if (point.Equals(new Point(-1, -1)))
  1063. {
  1064. index = item.PageIndex;
  1065. rect = new Rect(
  1066. (item.ClientRect.left + 25),
  1067. (item.ClientRect.top + 25),
  1068. item.ClientRect.width(),
  1069. item.ClientRect.height()
  1070. );
  1071. }
  1072. else
  1073. {
  1074. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1075. CRect cRect = item.ClientRect;
  1076. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1077. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1078. rect = new Rect(
  1079. (pdfPoint.X - cRect.width() / 2),
  1080. (pdfPoint.Y - cRect.height() / 2),
  1081. cRect.width(),
  1082. cRect.height()
  1083. );
  1084. }
  1085. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1086. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1087. if (cPDFPage == null)
  1088. return;
  1089. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1090. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1091. cPDFAnnotation.SetRect(setRect);
  1092. cPDFAnnotation.UpdateAp();
  1093. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1094. (annotHistory as FreeTextAnnotHistory).CurrentParam = (FreeTextParam)annotParam;
  1095. }
  1096. break;
  1097. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1098. {
  1099. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1100. Rect rect;
  1101. annotHistory = new LineAnnotHistory();
  1102. annotHistory.PDFDoc = cPDFDocument;
  1103. int index = -1;
  1104. if (point.Equals(new Point(-1, -1)))
  1105. {
  1106. index = item.PageIndex;
  1107. rect = new Rect(
  1108. (item.ClientRect.left + 25),
  1109. (item.ClientRect.top + 25),
  1110. item.ClientRect.width(),
  1111. item.ClientRect.height()
  1112. );
  1113. }
  1114. else
  1115. {
  1116. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1117. CRect cRect = item.ClientRect;
  1118. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1119. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1120. rect = new Rect(
  1121. (pdfPoint.X - cRect.width() / 2),
  1122. (pdfPoint.Y - cRect.height() / 2),
  1123. cRect.width(),
  1124. cRect.height()
  1125. );
  1126. }
  1127. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1128. LineParam newLineParam = new LineParam();
  1129. item.CopyTo(newLineParam);
  1130. {
  1131. float offsetX = setRect.left - item.ClientRect.left;
  1132. float offsetY = setRect.top - item.ClientRect.top;
  1133. newLineParam.HeadPoint = new CPoint(newLineParam.HeadPoint.x + offsetX, newLineParam.HeadPoint.y + offsetY);
  1134. newLineParam.TailPoint = new CPoint(newLineParam.TailPoint.x + offsetX, newLineParam.TailPoint.y + offsetY);
  1135. }
  1136. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1137. if (cPDFPage == null)
  1138. return;
  1139. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1140. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1141. (cPDFAnnotation as CPDFLineAnnotation)?.SetLinePoints(newLineParam.HeadPoint, newLineParam.TailPoint);
  1142. cPDFAnnotation.SetRect(setRect);
  1143. cPDFAnnotation.UpdateAp();
  1144. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1145. (annotHistory as LineAnnotHistory).CurrentParam = (LineParam)annotParam;
  1146. }
  1147. break;
  1148. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
  1149. {
  1150. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1151. Rect rect;
  1152. annotHistory = new SquareAnnotHistory();
  1153. annotHistory.PDFDoc = cPDFDocument;
  1154. int index = -1;
  1155. if (point.Equals(new Point(-1, -1)))
  1156. {
  1157. index = item.PageIndex;
  1158. rect = new Rect(
  1159. (item.ClientRect.left + 25),
  1160. (item.ClientRect.top + 25),
  1161. item.ClientRect.width(),
  1162. item.ClientRect.height()
  1163. );
  1164. }
  1165. else
  1166. {
  1167. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1168. CRect cRect = item.ClientRect;
  1169. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1170. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1171. rect = new Rect(
  1172. (pdfPoint.X - cRect.width() / 2),
  1173. (pdfPoint.Y - cRect.height() / 2),
  1174. cRect.width(),
  1175. cRect.height()
  1176. );
  1177. }
  1178. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1179. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1180. if (cPDFPage == null)
  1181. return;
  1182. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1183. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1184. cPDFAnnotation.SetRect(setRect);
  1185. cPDFAnnotation.UpdateAp();
  1186. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1187. (annotHistory as SquareAnnotHistory).CurrentParam = (SquareParam)annotParam;
  1188. }
  1189. break;
  1190. case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
  1191. {
  1192. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1193. Rect rect;
  1194. annotHistory = new CircleAnnotHistory();
  1195. annotHistory.PDFDoc = cPDFDocument;
  1196. int index = -1;
  1197. if (point.Equals(new Point(-1, -1)))
  1198. {
  1199. index = item.PageIndex;
  1200. rect = new Rect(
  1201. (item.ClientRect.left + 25),
  1202. (item.ClientRect.top + 25),
  1203. item.ClientRect.width(),
  1204. item.ClientRect.height()
  1205. );
  1206. }
  1207. else
  1208. {
  1209. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1210. CRect cRect = item.ClientRect;
  1211. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1212. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1213. rect = new Rect(
  1214. (pdfPoint.X - cRect.width() / 2),
  1215. (pdfPoint.Y - cRect.height() / 2),
  1216. cRect.width(),
  1217. cRect.height()
  1218. );
  1219. }
  1220. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1221. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1222. if (cPDFPage == null)
  1223. return;
  1224. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1225. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1226. cPDFAnnotation.SetRect(setRect);
  1227. cPDFAnnotation.UpdateAp();
  1228. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1229. (annotHistory as CircleAnnotHistory).CurrentParam = (CircleParam)annotParam;
  1230. }
  1231. break;
  1232. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1233. break;
  1234. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  1235. break;
  1236. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  1237. {
  1238. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1239. Rect rect;
  1240. annotHistory = new StampAnnotHistory();
  1241. annotHistory.PDFDoc = cPDFDocument;
  1242. int index = -1;
  1243. if (point.Equals(new Point(-1, -1)))
  1244. {
  1245. index = item.PageIndex;
  1246. rect = new Rect(
  1247. (item.ClientRect.left + 25),
  1248. (item.ClientRect.top + 25),
  1249. item.ClientRect.width(),
  1250. item.ClientRect.height()
  1251. );
  1252. }
  1253. else
  1254. {
  1255. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1256. CRect cRect = item.ClientRect;
  1257. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1258. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1259. rect = new Rect(
  1260. (pdfPoint.X - cRect.width() / 2),
  1261. (pdfPoint.Y - cRect.height() / 2),
  1262. cRect.width(),
  1263. cRect.height()
  1264. );
  1265. }
  1266. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1267. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1268. if (cPDFPage == null)
  1269. return;
  1270. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1271. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1272. cPDFAnnotation.SetRect(setRect);
  1273. cPDFAnnotation.UpdateAp();
  1274. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1275. (annotHistory as StampAnnotHistory).CurrentParam = (StampParam)annotParam;
  1276. }
  1277. break;
  1278. case C_ANNOTATION_TYPE.C_ANNOTATION_CARET:
  1279. break;
  1280. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  1281. {
  1282. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1283. Rect rect;
  1284. annotHistory = new InkAnnotHistory();
  1285. annotHistory.PDFDoc = cPDFDocument;
  1286. int index = -1;
  1287. if (point.Equals(new Point(-1, -1)))
  1288. {
  1289. index = item.PageIndex;
  1290. rect = new Rect(
  1291. (item.ClientRect.left + 25),
  1292. (item.ClientRect.top + 25),
  1293. item.ClientRect.width(),
  1294. item.ClientRect.height()
  1295. );
  1296. }
  1297. else
  1298. {
  1299. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1300. CRect cRect = item.ClientRect;
  1301. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1302. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1303. rect = new Rect(
  1304. (pdfPoint.X - cRect.width() / 2),
  1305. (pdfPoint.Y - cRect.height() / 2),
  1306. cRect.width(),
  1307. cRect.height()
  1308. );
  1309. }
  1310. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1311. InkParam newInkParam = new InkParam();
  1312. item.CopyTo(newInkParam);
  1313. if (newInkParam.InkPath != null && newInkParam.InkPath.Count > 0)
  1314. {
  1315. float offsetX = setRect.left - item.ClientRect.left;
  1316. float offsetY = setRect.top - item.ClientRect.top;
  1317. List<List<CPoint>> arrangeList = new List<List<CPoint>>();
  1318. foreach (List<CPoint> inkNode in newInkParam.InkPath)
  1319. {
  1320. List<CPoint> inkPath = new List<CPoint>();
  1321. arrangeList.Add(inkPath);
  1322. foreach (CPoint addPoint in inkNode)
  1323. {
  1324. inkPath.Add(new CPoint(addPoint.x + offsetX, addPoint.y + offsetY));
  1325. }
  1326. }
  1327. newInkParam.InkPath = arrangeList;
  1328. newInkParam.ClientRect = setRect;
  1329. }
  1330. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1331. if (cPDFPage == null)
  1332. return;
  1333. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(newInkParam.CurrentType);
  1334. CreateDefaultAnnot(cPDFAnnotation, newInkParam.CurrentType, newInkParam);
  1335. cPDFAnnotation.UpdateAp();
  1336. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1337. (annotHistory as InkAnnotHistory).CurrentParam = (InkParam)annotParam;
  1338. }
  1339. break;
  1340. case C_ANNOTATION_TYPE.C_ANNOTATION_POPUP:
  1341. break;
  1342. case C_ANNOTATION_TYPE.C_ANNOTATION_FILEATTACHMENT:
  1343. break;
  1344. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  1345. {
  1346. rightPressPoint = point;
  1347. bool result = PasteWidgetData(ref annotHistory, item as WidgetParm);
  1348. if (!result)
  1349. return;
  1350. }
  1351. break;
  1352. case C_ANNOTATION_TYPE.C_ANNOTATION_SCREEN:
  1353. break;
  1354. case C_ANNOTATION_TYPE.C_ANNOTATION_PRINTERMARK:
  1355. break;
  1356. case C_ANNOTATION_TYPE.C_ANNOTATION_TRAPNET:
  1357. break;
  1358. case C_ANNOTATION_TYPE.C_ANNOTATION_WATERMARK:
  1359. break;
  1360. case C_ANNOTATION_TYPE.C_ANNOTATION_3D:
  1361. break;
  1362. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  1363. break;
  1364. case C_ANNOTATION_TYPE.C_ANNOTATION_INTERCHANGE:
  1365. break;
  1366. default:
  1367. break;
  1368. }
  1369. PasteParam = annotHistory.CurrentParam;
  1370. PDFViewer.UndoManager.AddHistory(annotHistory);
  1371. }
  1372. }
  1373. private void DeleteAnnotData()
  1374. {
  1375. dynamic notifyData = null;
  1376. if (cacheHitTestAnnot != null)
  1377. {
  1378. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheHitTestAnnot.GetAnnotData().Annot);
  1379. if (annotHistory == null)
  1380. {
  1381. return;
  1382. }
  1383. AnnotParam annotParam = null;
  1384. if (cacheHitTestAnnot is BaseWidget)
  1385. {
  1386. annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1387. }
  1388. else
  1389. {
  1390. annotParam = ParamConverter.AnnotConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1391. }
  1392. if (annotParam != null)
  1393. {
  1394. notifyData = new ExpandoObject();
  1395. notifyData.Action = HistoryAction.Remove;
  1396. notifyData.PageIndex = annotParam.PageIndex;
  1397. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1398. notifyData.AnnotType = annotParam.CurrentType;
  1399. notifyData.CurrentParam = annotParam;
  1400. if (annotParam is WidgetParm)
  1401. {
  1402. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1403. }
  1404. annotHistory.CurrentParam = annotParam;
  1405. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1406. annotHistory.Action = HistoryAction.Remove;
  1407. PDFViewer.UndoManager.AddHistory(annotHistory);
  1408. List<C_ANNOTATION_TYPE> checkEditType = new List<C_ANNOTATION_TYPE>()
  1409. {
  1410. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1411. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1412. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON
  1413. };
  1414. if (checkEditType.Contains(cacheHitTestAnnot.CurrentType))
  1415. {
  1416. //Need to clear the measurement selected edit box.
  1417. CleanEditAnnot();
  1418. }
  1419. cacheHitTestAnnot.RemoveClean(true);
  1420. cacheHitTestAnnot.GetAnnotData().Annot.RemoveAnnot();
  1421. cacheHitTestAnnot = null;
  1422. }
  1423. CleanSelectedRect();
  1424. }
  1425. if (cacheMoveWidget != null)
  1426. {
  1427. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheMoveWidget.GetAnnotData().Annot);
  1428. if (annotHistory == null)
  1429. {
  1430. return;
  1431. }
  1432. AnnotParam annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1433. if (annotParam != null)
  1434. {
  1435. notifyData = new ExpandoObject();
  1436. notifyData.Action = HistoryAction.Remove;
  1437. notifyData.PageIndex = annotParam.PageIndex;
  1438. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1439. notifyData.AnnotType = annotParam.CurrentType;
  1440. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1441. notifyData.CurrentParam = annotParam;
  1442. annotHistory.CurrentParam = annotParam;
  1443. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1444. annotHistory.Action = HistoryAction.Remove;
  1445. PDFViewer.UndoManager.AddHistory(annotHistory);
  1446. cacheMoveWidget.GetAnnotData().Annot.RemoveAnnot();
  1447. cacheMoveWidget = null;
  1448. }
  1449. CleanSelectedRect();
  1450. }
  1451. if (notifyData != null)
  1452. {
  1453. AnnotChanged?.Invoke(this, notifyData);
  1454. }
  1455. }
  1456. private bool PasteWidgetData(ref AnnotHistory annotHistory, WidgetParm item)
  1457. {
  1458. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1459. Point point = rightPressPoint;
  1460. rightPressPoint = new Point(-1, -1);
  1461. Rect rect;
  1462. int index = -1;
  1463. if (point.Equals(new Point(-1, -1)))
  1464. {
  1465. index = item.PageIndex;
  1466. rect = new Rect(
  1467. (item.ClientRect.left + 25),
  1468. (item.ClientRect.top + 25),
  1469. item.ClientRect.width(),
  1470. item.ClientRect.height()
  1471. );
  1472. }
  1473. else
  1474. {
  1475. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1476. CRect cRect = item.ClientRect;
  1477. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1478. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1479. rect = new Rect(
  1480. (pdfPoint.X - cRect.width() / 2),
  1481. (pdfPoint.Y - cRect.height() / 2),
  1482. cRect.width(),
  1483. cRect.height()
  1484. );
  1485. }
  1486. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1487. if (cPDFPage == null)
  1488. return false;
  1489. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateWidget(item.WidgetType);
  1490. CreateDefaultWidget(cPDFAnnotation, item.WidgetType, item);
  1491. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1492. cPDFAnnotation.SetRect(setRect);
  1493. (cPDFAnnotation as CPDFWidget).UpdateFormAp();
  1494. AnnotParam annotParam = ParamConverter.WidgetConverter(cPDFDocument, cPDFAnnotation);
  1495. switch (item.WidgetType)
  1496. {
  1497. case C_WIDGET_TYPE.WIDGET_NONE:
  1498. break;
  1499. case C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1500. annotHistory = new PushButtonHistory();
  1501. (annotHistory as PushButtonHistory).CurrentParam = (PushButtonParam)annotParam;
  1502. annotHistory.PDFDoc = cPDFDocument;
  1503. break;
  1504. case C_WIDGET_TYPE.WIDGET_CHECKBOX:
  1505. annotHistory = new CheckBoxHistory();
  1506. (annotHistory as CheckBoxHistory).CurrentParam = (CheckBoxParam)annotParam;
  1507. annotHistory.PDFDoc = cPDFDocument;
  1508. break;
  1509. case C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  1510. annotHistory = new RadioButtonHistory();
  1511. (annotHistory as RadioButtonHistory).CurrentParam = (RadioButtonParam)annotParam;
  1512. annotHistory.PDFDoc = cPDFDocument;
  1513. break;
  1514. case C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  1515. annotHistory = new TextBoxHistory();
  1516. (annotHistory as TextBoxHistory).CurrentParam = (TextBoxParam)annotParam;
  1517. annotHistory.PDFDoc = cPDFDocument;
  1518. break;
  1519. case C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1520. annotHistory = new ComboBoxHistory();
  1521. (annotHistory as ComboBoxHistory).CurrentParam = (ComboBoxParam)annotParam;
  1522. annotHistory.PDFDoc = cPDFDocument;
  1523. break;
  1524. case C_WIDGET_TYPE.WIDGET_LISTBOX:
  1525. annotHistory = new ListBoxHistory();
  1526. (annotHistory as ListBoxHistory).CurrentParam = (ListBoxParam)annotParam;
  1527. annotHistory.PDFDoc = cPDFDocument;
  1528. break;
  1529. case C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  1530. annotHistory = new SignatureHistory();
  1531. (annotHistory as SignatureHistory).CurrentParam = (SignatureParam)annotParam;
  1532. annotHistory.PDFDoc = cPDFDocument;
  1533. break;
  1534. case C_WIDGET_TYPE.WIDGET_UNKNOWN:
  1535. break;
  1536. default:
  1537. break;
  1538. }
  1539. return true;
  1540. }
  1541. #endregion
  1542. }
  1543. }