CPDFViewerTool.Command.cs 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  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.Linq;
  20. using System.Text;
  21. using System.Windows;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Imaging;
  25. using static ComPDFKit.Tool.Help.ImportWin32;
  26. namespace ComPDFKit.Tool
  27. {
  28. partial class CPDFViewerTool
  29. {
  30. public class CommandData
  31. {
  32. public ExecutedRoutedEventArgs uIEventArgs { get; set; }
  33. public bool StartCommand { get; set; }
  34. public CPDFEditType PDFEditType { get; set; }
  35. public AnnotParam CurrentParam { get; set; } = null;
  36. }
  37. /// <summary>
  38. /// System provided related command time notification
  39. /// </summary>
  40. public event EventHandler<CommandData> CommandExecutedHandler;
  41. public AnnotParam PasteParam { get; set; }
  42. internal class PDFEditCommandData
  43. {
  44. public string TextContent { get; set; }
  45. /// <summary>
  46. /// Original rectangle in PDF (72DPI)
  47. /// </summary>
  48. public Rect PDFRect { get; set; }
  49. public bool EditAreaCopied { get; set; }
  50. public CPDFEditType EditType { get; set; }
  51. public CPDFCopyEditArea CopyArea { get; set; }
  52. public CPDFEditPage EditPage { get; set; }
  53. }
  54. internal Point rightPressPoint = new Point(-1, -1);
  55. internal static List<PDFEditCommandData> lastPDFEditArgsList { get; private set; } = new List<PDFEditCommandData>();
  56. internal static List<AnnotParam> lastAnnotList { get; private set; } = new List<AnnotParam>();
  57. internal static string CopyText { get; private set; } = "";
  58. internal void BindCommand()
  59. {
  60. CommandBinding copyBind = new CommandBinding(ApplicationCommands.Copy);
  61. copyBind.CanExecute += CommandCanExecute;
  62. copyBind.Executed += CommandBind_Executed;
  63. CommandBindings.Add(copyBind);
  64. CommandBinding cutBind = new CommandBinding(ApplicationCommands.Cut);
  65. cutBind.CanExecute += CommandCanExecute;
  66. cutBind.Executed += CommandBind_Executed;
  67. CommandBindings.Add(cutBind);
  68. CommandBinding pasteBind = new CommandBinding(ApplicationCommands.Paste);
  69. pasteBind.CanExecute += CommandCanExecute;
  70. pasteBind.Executed += CommandBind_Executed;
  71. CommandBindings.Add(pasteBind);
  72. CommandBinding deleteBind = new CommandBinding(ApplicationCommands.Delete);
  73. deleteBind.CanExecute += CommandCanExecute;
  74. deleteBind.Executed += CommandBind_Executed;
  75. CommandBindings.Add(deleteBind);
  76. CommandBinding playBind = new CommandBinding(MediaCommands.Play);
  77. playBind.CanExecute += PlayBind_CanExecute;
  78. playBind.Executed += PlayBind_Executed;
  79. CommandBindings.Add(playBind);
  80. CommandBinding pastWithoutStyle = new CommandBinding(CustomCommands.PasteWithoutStyle);
  81. pastWithoutStyle.CanExecute += CommandCanExecute;
  82. pastWithoutStyle.Executed += CommandBind_Executed;
  83. CommandBindings.Add(pastWithoutStyle);
  84. CommandBinding selectAllBind = new CommandBinding(ApplicationCommands.SelectAll);
  85. selectAllBind.CanExecute += CommandCanExecute;
  86. selectAllBind.Executed += CommandBind_Executed;
  87. CommandBindings.Add(selectAllBind);
  88. }
  89. private void PlayBind_Executed(object sender, ExecutedRoutedEventArgs e)
  90. {
  91. BaseAnnot cmdArgs = e.Parameter as BaseAnnot;
  92. if (cmdArgs != null && cmdArgs.GetAnnotData() != null)
  93. {
  94. if (cmdArgs is SoundAnnot)
  95. {
  96. (cmdArgs as SoundAnnot).Play();
  97. }
  98. if (cmdArgs is MovieAnnot)
  99. {
  100. (cmdArgs as MovieAnnot).Play();
  101. }
  102. }
  103. e.Handled = true;
  104. }
  105. private void PlayBind_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  106. {
  107. e.CanExecute = false;
  108. BaseAnnot cmdArgs = e.Parameter as BaseAnnot;
  109. if (cmdArgs != null && cmdArgs.GetAnnotData() != null)
  110. {
  111. e.CanExecute = true;
  112. }
  113. e.Handled = true;
  114. e.ContinueRouting = false;
  115. }
  116. public void ReBindCommand(List<CommandBinding> commandBindings)
  117. {
  118. CommandBindings.Clear();
  119. foreach (CommandBinding binding in commandBindings)
  120. {
  121. CommandBindings.Add(binding);
  122. }
  123. }
  124. private bool IsCanDoCommand = true;
  125. public void CanDoCommand()
  126. {
  127. IsCanDoCommand = true;
  128. }
  129. public void NotDoCommand()
  130. {
  131. IsCanDoCommand = false;
  132. }
  133. private void CommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
  134. {
  135. if (e.Command is RoutedUICommand uiCommand)
  136. {
  137. switch (currentModel)
  138. {
  139. case ToolType.Viewer:
  140. CheckViewerCommandStatus(uiCommand, e);
  141. break;
  142. case ToolType.CreateAnnot:
  143. case ToolType.Pan:
  144. CheckAnnotCommandStatus(uiCommand, e);
  145. break;
  146. case ToolType.WidgetEdit:
  147. if (AnnotSelectCommandSupport(uiCommand))
  148. {
  149. e.CanExecute = true;
  150. }
  151. else
  152. {
  153. CheckAnnotCommandStatus(uiCommand, e);
  154. }
  155. break;
  156. case ToolType.Customize:
  157. break;
  158. case ToolType.ContentEdit:
  159. {
  160. CheckPDFEditCommandStatus(uiCommand, e);
  161. }
  162. break;
  163. default:
  164. break;
  165. }
  166. }
  167. }
  168. private void CommandBind_Executed(object sender, ExecutedRoutedEventArgs e)
  169. {
  170. RoutedUICommand uiCommand = e.Command as RoutedUICommand;
  171. if (uiCommand != null)
  172. {
  173. try
  174. {
  175. PasteParam = null;
  176. CommandData commandData = new CommandData();
  177. commandData.uIEventArgs = e;
  178. commandData.StartCommand = true;
  179. CPDFEditType editType = CPDFEditType.None;
  180. CommandExecutedHandler?.Invoke(this, commandData);
  181. if (e.Handled)
  182. {
  183. return;
  184. }
  185. if (!IsCanDoCommand)
  186. {
  187. CanDoCommand();
  188. return;
  189. }
  190. switch (currentModel)
  191. {
  192. case ToolType.Viewer:
  193. ExecuteViewerCommand(uiCommand);
  194. break;
  195. case ToolType.CreateAnnot:
  196. case ToolType.Pan:
  197. ExecuteAnnotCommand(uiCommand);
  198. break;
  199. case ToolType.WidgetEdit:
  200. if (AnnotSelectCommandSupport(uiCommand))
  201. {
  202. AnnotSelectCommandExecute(uiCommand);
  203. }
  204. else
  205. {
  206. ExecuteAnnotCommand(uiCommand);
  207. }
  208. break;
  209. case ToolType.ContentEdit:
  210. {
  211. ExecutePDFEditCommand(uiCommand, out editType);
  212. }
  213. break;
  214. case ToolType.Customize:
  215. break;
  216. default:
  217. break;
  218. }
  219. commandData.StartCommand = false;
  220. commandData.PDFEditType = editType;
  221. commandData.CurrentParam = PasteParam;
  222. CommandExecutedHandler?.Invoke(this, commandData);
  223. }
  224. catch (Exception ex)
  225. {
  226. }
  227. }
  228. }
  229. public void SetPastePoint(Point point)
  230. {
  231. rightPressPoint = point;
  232. }
  233. #region ContentEdit
  234. private void CheckPDFEditCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  235. {
  236. switch (uiCommand.Name)
  237. {
  238. case "Copy":
  239. case "Cut":
  240. case "Delete":
  241. case "SelectAll":
  242. {
  243. if (currentEditAreaObject != null)
  244. {
  245. e.CanExecute = true;
  246. }
  247. //Add multi status
  248. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  249. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  250. {
  251. e.CanExecute = true;
  252. }
  253. break;
  254. }
  255. case "Paste":
  256. case "PasteWithoutStyle":
  257. foreach (PDFEditCommandData checkItem in lastPDFEditArgsList)
  258. {
  259. if (checkItem.EditType == CPDFEditType.EditText && string.IsNullOrEmpty(checkItem.TextContent) == false)
  260. {
  261. e.CanExecute = true;
  262. break;
  263. }
  264. if (checkItem.EditType == CPDFEditType.EditImage && checkItem.EditAreaCopied)
  265. {
  266. e.CanExecute = true;
  267. break;
  268. }
  269. if (checkItem.EditType == CPDFEditType.EditPath && checkItem.EditAreaCopied)
  270. {
  271. e.CanExecute = true;
  272. break;
  273. }
  274. }
  275. try
  276. {
  277. if (Clipboard.ContainsText())
  278. {
  279. if (!string.IsNullOrEmpty(Clipboard.GetText()))
  280. {
  281. e.CanExecute = true;
  282. }
  283. }
  284. if (Clipboard.ContainsImage())
  285. {
  286. if (Clipboard.GetImage() != null)
  287. {
  288. e.CanExecute = true;
  289. }
  290. }
  291. }
  292. catch (Exception ex)
  293. {
  294. }
  295. break;
  296. }
  297. }
  298. private void ExecutePDFEditCommand(RoutedUICommand uiCommand, out CPDFEditType editType)
  299. {
  300. editType = CPDFEditType.None;
  301. switch (uiCommand.Name)
  302. {
  303. case "Copy":
  304. CopyPDFEditData(out editType);
  305. break;
  306. case "Cut":
  307. CopyPDFEditData(out editType);
  308. DelPDFEditData(out editType);
  309. break;
  310. case "Delete":
  311. DelPDFEditData(out editType);
  312. break;
  313. case "Paste":
  314. SetEditCopyData();
  315. if (lastPDFEditArgsList.Count > 0)
  316. {
  317. PastePDFEditData(out editType);
  318. PDFViewer.UpdateRenderFrame();
  319. }
  320. break;
  321. case "PasteWithoutStyle":
  322. SetEditCopyData();
  323. if (lastPDFEditArgsList.Count > 0)
  324. {
  325. PastePDFEditData(out editType, false);
  326. PDFViewer.UpdateRenderFrame();
  327. }
  328. break;
  329. case "SelectAll":
  330. SelectAllPDFEditData(out editType);
  331. break;
  332. }
  333. }
  334. private void SetEditCopyData()
  335. {
  336. try
  337. {
  338. if (Clipboard.ContainsText())
  339. {
  340. string copyText = Clipboard.GetText();
  341. bool findCopy = false;
  342. if (lastPDFEditArgsList != null && lastPDFEditArgsList.Count > 0)
  343. {
  344. foreach (PDFEditCommandData checkItem in lastPDFEditArgsList)
  345. {
  346. if (checkItem.EditType == CPDFEditType.EditText && copyText == checkItem.TextContent)
  347. {
  348. findCopy = true;
  349. }
  350. }
  351. }
  352. if (findCopy == false)
  353. {
  354. lastPDFEditArgsList?.Clear();
  355. if (string.IsNullOrEmpty(copyText) == false)
  356. {
  357. PDFEditCommandData commandData = new PDFEditCommandData();
  358. commandData.EditType = CPDFEditType.EditText;
  359. commandData.TextContent = copyText;
  360. int PageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  361. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(PageIndex);
  362. Rect rect = render.PaintRect;
  363. Point centerPoint = new Point(
  364. rect.Width / PDFViewer.CurrentRenderFrame.ZoomFactor / 2,
  365. rect.Height / PDFViewer.CurrentRenderFrame.ZoomFactor / 2);
  366. commandData.PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(centerPoint.X, centerPoint.Y, 0, 0));
  367. lastPDFEditArgsList.Add(commandData);
  368. }
  369. }
  370. }
  371. else if (Clipboard.ContainsImage())
  372. {
  373. if (Clipboard.GetImage() != null)
  374. {
  375. BitmapSource bitmapSource = Clipboard.GetImage();
  376. PDFEditCommandData commandData = new PDFEditCommandData();
  377. commandData.EditType = CPDFEditType.EditImage;
  378. int PageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  379. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(PageIndex);
  380. Rect rect = render.PaintRect;
  381. Point centerPoint = new Point(
  382. rect.Width / PDFViewer.CurrentRenderFrame.ZoomFactor / 2,
  383. rect.Height / PDFViewer.CurrentRenderFrame.ZoomFactor / 2);
  384. commandData.PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(centerPoint.X, centerPoint.Y, bitmapSource.PixelWidth, bitmapSource.PixelHeight));
  385. lastPDFEditArgsList.Clear();
  386. lastPDFEditArgsList.Add(commandData);
  387. }
  388. }
  389. }
  390. catch (Exception ex)
  391. {
  392. }
  393. }
  394. private void CopyPDFEditData(out CPDFEditType editType)
  395. {
  396. editType = CPDFEditType.None;
  397. if (CPDFEditPage.CopyPage != null)
  398. {
  399. CPDFEditPage.CopyPage.ReleaseCopyEditAreaList();
  400. }
  401. try
  402. {
  403. lastPDFEditArgsList.Clear();
  404. Clipboard.Clear();
  405. if (currentEditAreaObject != null)
  406. {
  407. PDFEditCommandData commandData = new PDFEditCommandData();
  408. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  409. {
  410. editType = CPDFEditType.EditText;
  411. CPDFEditTextArea editTextArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  412. commandData.TextContent = editTextArea.SelectText;
  413. if (selectAllCharsForLine || string.IsNullOrEmpty(commandData.TextContent))
  414. {
  415. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  416. commandData.EditType = CPDFEditType.EditText;
  417. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  418. if (commandData.EditAreaCopied)
  419. {
  420. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  421. CPDFCopyEditArea CopyArea = copyList[copyList.Count - 1];
  422. commandData.TextContent = CopyArea.GetCopyTextAreaContent();
  423. commandData.CopyArea = CopyArea;
  424. commandData.EditPage = editPage;
  425. }
  426. }
  427. try
  428. {
  429. Clipboard.Clear();
  430. Clipboard.SetText(commandData.TextContent);
  431. Clipboard.Flush();
  432. }
  433. catch (Exception)
  434. {
  435. }
  436. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  437. lastPDFEditArgsList.Add(commandData);
  438. }
  439. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  440. {
  441. editType = CPDFEditType.EditImage;
  442. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  443. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  444. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  445. commandData.EditType = CPDFEditType.EditImage;
  446. if (commandData.EditAreaCopied)
  447. {
  448. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  449. commandData.CopyArea = copyList[copyList.Count - 1];
  450. commandData.EditPage = editPage;
  451. lastPDFEditArgsList.Add(commandData);
  452. }
  453. }
  454. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditPath)
  455. {
  456. editType = CPDFEditType.EditPath;
  457. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  458. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  459. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  460. commandData.EditType = CPDFEditType.EditPath;
  461. if (commandData.EditAreaCopied)
  462. {
  463. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  464. commandData.CopyArea = copyList[copyList.Count - 1];
  465. commandData.EditPage = editPage;
  466. lastPDFEditArgsList.Add(commandData);
  467. }
  468. }
  469. }
  470. else
  471. {
  472. //Add multi status
  473. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  474. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  475. {
  476. foreach (SelectedRect selectedRect in multiSelectedRect.Children)
  477. {
  478. EditAreaObject editAreaObject = GetEditAreaObjectListForRect(selectedRect);
  479. if (editAreaObject != null)
  480. {
  481. PDFEditCommandData commandData = new PDFEditCommandData();
  482. if (editAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  483. {
  484. editType = CPDFEditType.EditText;
  485. CPDFEditTextArea editTextArea = editAreaObject.cPDFEditArea as CPDFEditTextArea;
  486. commandData.TextContent = editTextArea.SelectText;
  487. if (!string.IsNullOrEmpty(commandData.TextContent))
  488. {
  489. CPDFEditPage editPage = editAreaObject.cPDFEditPage;
  490. commandData.EditType = CPDFEditType.EditText;
  491. commandData.EditAreaCopied = editPage.CopyEditArea(editAreaObject.cPDFEditArea);
  492. if (commandData.EditAreaCopied)
  493. {
  494. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  495. CPDFCopyEditArea CopyArea = copyList[copyList.Count - 1];
  496. commandData.TextContent = CopyArea.GetCopyTextAreaContent();
  497. commandData.CopyArea = CopyArea;
  498. commandData.EditPage = editPage;
  499. }
  500. }
  501. try
  502. {
  503. Clipboard.Clear();
  504. Clipboard.SetText(commandData.TextContent);
  505. Clipboard.Flush();
  506. }
  507. catch (Exception)
  508. {
  509. }
  510. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(editAreaObject.cPDFEditArea.GetFrame());
  511. lastPDFEditArgsList.Add(commandData);
  512. }
  513. if (editAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  514. {
  515. editType = CPDFEditType.EditImage;
  516. CPDFEditPage editPage = editAreaObject.cPDFEditPage;
  517. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(editAreaObject.cPDFEditArea.GetFrame());
  518. commandData.EditAreaCopied = editPage.CopyEditArea(editAreaObject.cPDFEditArea);
  519. commandData.EditType = CPDFEditType.EditImage;
  520. if (commandData.EditAreaCopied)
  521. {
  522. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  523. commandData.CopyArea = copyList[copyList.Count - 1];
  524. commandData.EditPage = editPage;
  525. lastPDFEditArgsList.Add(commandData);
  526. }
  527. }
  528. if (editAreaObject.cPDFEditArea.Type == CPDFEditType.EditPath)
  529. {
  530. editType = CPDFEditType.EditPath;
  531. CPDFEditPage editPage = editAreaObject.cPDFEditPage;
  532. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(editAreaObject.cPDFEditArea.GetFrame());
  533. commandData.EditAreaCopied = editPage.CopyEditArea(editAreaObject.cPDFEditArea);
  534. commandData.EditType = CPDFEditType.EditPath;
  535. if (commandData.EditAreaCopied)
  536. {
  537. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  538. commandData.CopyArea = copyList[copyList.Count - 1];
  539. commandData.EditPage = editPage;
  540. lastPDFEditArgsList.Add(commandData);
  541. }
  542. }
  543. }
  544. }
  545. }
  546. }
  547. }
  548. catch
  549. {
  550. }
  551. }
  552. private void DelPDFEditData(out CPDFEditType editType)
  553. {
  554. editType = CPDFEditType.None;
  555. if (currentEditAreaObject != null)
  556. {
  557. GroupHistory groupHistory = new GroupHistory();
  558. PDFEditHistory editHistory = new PDFEditHistory();
  559. editHistory.EditPage = currentEditAreaObject.cPDFEditPage;
  560. editHistory.PageIndex = currentEditAreaObject.PageIndex;
  561. groupHistory.Histories.Add(editHistory);
  562. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  563. {
  564. editType = CPDFEditType.EditText;
  565. CPDFEditTextArea editTextArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  566. string selectContent = editTextArea.SelectText;
  567. if (!selectAllCharsForLine)
  568. {
  569. DeleteChars();
  570. }
  571. else
  572. {
  573. RemoveTextBlock();
  574. }
  575. }
  576. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  577. {
  578. editType = CPDFEditType.EditImage;
  579. RemoveImageBlock();
  580. }
  581. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditPath)
  582. {
  583. editType = CPDFEditType.EditPath;
  584. RemovePathBlock();
  585. }
  586. if (PDFViewer != null && PDFViewer.UndoManager != null)
  587. {
  588. PDFViewer.UndoManager.AddHistory(groupHistory);
  589. //After removing the data, you need to get the data again.
  590. PDFViewer.UpdateRenderFrame();
  591. }
  592. }
  593. else
  594. {
  595. //Add multi status
  596. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  597. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  598. {
  599. List<EditAreaObject> editAreaObjectList = new List<EditAreaObject>();
  600. foreach (SelectedRect selectedRect in multiSelectedRect.Children)
  601. {
  602. EditAreaObject editAreaObject = GetEditAreaObjectListForRect(selectedRect);
  603. editAreaObjectList.Add(editAreaObject);
  604. }
  605. if (editAreaObjectList != null && editAreaObjectList.Count > 0)
  606. {
  607. editAreaObjectList = editAreaObjectList.OrderByDescending(x => x.EditAreaIndex).ToList();
  608. GroupHistory groupHistory = new GroupHistory();
  609. foreach (EditAreaObject editareaobject in editAreaObjectList)
  610. {
  611. PDFEditHistory editHistory = new PDFEditHistory();
  612. editHistory.EditPage = editareaobject.cPDFEditPage;
  613. editHistory.PageIndex = editareaobject.PageIndex;
  614. groupHistory.Histories.Add(editHistory);
  615. if (editareaobject.cPDFEditArea.Type == CPDFEditType.EditText)
  616. {
  617. editType = CPDFEditType.EditText;
  618. CPDFEditTextArea editTextArea = editareaobject.cPDFEditArea as CPDFEditTextArea;
  619. string selectContent = editTextArea.SelectText;
  620. RemoveTextBlock(editareaobject);
  621. }
  622. if (editareaobject.cPDFEditArea.Type == CPDFEditType.EditImage)
  623. {
  624. editType = CPDFEditType.EditImage;
  625. RemoveImageBlock(editareaobject);
  626. }
  627. if (editareaobject.cPDFEditArea.Type == CPDFEditType.EditPath)
  628. {
  629. editType = CPDFEditType.EditPath;
  630. RemovePathBlock(editareaobject);
  631. }
  632. }
  633. if (PDFViewer != null && PDFViewer.UndoManager != null)
  634. {
  635. PDFViewer.UndoManager.AddHistory(groupHistory);
  636. //After removing the data, you need to get the data again.
  637. PDFViewer.UpdateRenderFrame();
  638. }
  639. }
  640. CleanSelectedMultiRect();
  641. OpenSelectedMulti(false);
  642. }
  643. }
  644. }
  645. private void PastePDFEditData(out CPDFEditType editType, bool pasteMatchStyle = true)
  646. {
  647. editType = CPDFEditType.None;
  648. double left = 0;
  649. double right = 0;
  650. double top = 0;
  651. double bottom = 0;
  652. bool initial = false;
  653. #region Calculate the maximum rectangle
  654. foreach (PDFEditCommandData commandData in lastPDFEditArgsList)
  655. {
  656. if (initial == false)
  657. {
  658. left = commandData.PDFRect.Left;
  659. right = commandData.PDFRect.Right;
  660. top = commandData.PDFRect.Top;
  661. bottom = commandData.PDFRect.Bottom;
  662. initial = true;
  663. continue;
  664. }
  665. left = Math.Min(left, commandData.PDFRect.Left);
  666. right = Math.Max(right, commandData.PDFRect.Right);
  667. top = Math.Min(top, commandData.PDFRect.Top);
  668. bottom = Math.Max(bottom, commandData.PDFRect.Bottom);
  669. }
  670. left = DpiHelper.PDFNumToStandardNum(left);
  671. right = DpiHelper.PDFNumToStandardNum(right);
  672. top = DpiHelper.PDFNumToStandardNum(top);
  673. bottom = DpiHelper.PDFNumToStandardNum(bottom);
  674. int offsetX = 25;
  675. int offsetY = 25;
  676. Point hoverPoint = rightPressPoint;
  677. rightPressPoint = new Point(-1, -1);
  678. int pageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  679. // Judge whether the right-click coordinate is on the page.
  680. PDFViewer.GetePointToPage(hoverPoint, out RenderData renderData, out Point pagePoint);
  681. if (renderData != null)
  682. {
  683. //Calculate the offset from the upper left corner.
  684. offsetX = (int)(pagePoint.X / currentZoom - left);
  685. offsetY = (int)(pagePoint.Y / currentZoom - top);
  686. if (left + offsetX < 0)
  687. {
  688. offsetX = (int)-left;
  689. }
  690. if (right + offsetX > renderData.PageBound.Width / currentZoom)
  691. {
  692. offsetX = (int)(renderData.PageBound.Width / currentZoom - right);
  693. }
  694. if (top + offsetY < 0)
  695. {
  696. offsetY = (int)-top;
  697. }
  698. if (bottom + offsetY > renderData.PageBound.Height / currentZoom)
  699. {
  700. offsetY = (int)(renderData.PageBound.Height / currentZoom - bottom);
  701. }
  702. pageIndex = renderData.PageIndex;
  703. }
  704. else
  705. {
  706. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(pageIndex);
  707. //Max rectangle (standard DPI).
  708. Rect maxRect = new Rect((int)left + render.PaintRect.Left / currentZoom, (int)top + render.PaintRect.Top / currentZoom, (int)(right - left), (int)(bottom - top));
  709. //Center point of the visible range.
  710. Point centerPoint = new Point(
  711. render.PaintRect.Left / currentZoom + render.PaintRect.Width / currentZoom / 2,
  712. render.PaintRect.Top / currentZoom + render.PaintRect.Height / currentZoom / 2);
  713. //Visible range.
  714. Rect checkRect = new Rect(
  715. render.PaintRect.Left / currentZoom,
  716. render.PaintRect.Top / currentZoom,
  717. render.PaintRect.Width / currentZoom,
  718. render.PaintRect.Height / currentZoom);
  719. if (!checkRect.IntersectsWith(maxRect))
  720. {
  721. offsetX = (int)(left - centerPoint.X);
  722. offsetY = (int)(top - centerPoint.Y);
  723. }
  724. if (left + offsetX < 0)
  725. {
  726. offsetX = (int)-left;
  727. }
  728. if (right + offsetX > render.RenderRect.Width / currentZoom)
  729. {
  730. offsetX = (int)(render.RenderRect.Width / currentZoom / 2 - right);
  731. }
  732. if (top + offsetY < 0)
  733. {
  734. offsetY = (int)-top;
  735. }
  736. if (bottom + offsetY > render.RenderRect.Height / currentZoom)
  737. {
  738. offsetY = (int)(render.RenderRect.Height / currentZoom / 2 - bottom);
  739. }
  740. }
  741. #endregion
  742. foreach (PDFEditCommandData commandData in lastPDFEditArgsList)
  743. {
  744. GroupHistory groupHistory = new GroupHistory();
  745. CPDFPage docPage = PDFViewer.GetDocument().PageAtIndex(pageIndex);
  746. CPDFEditPage editPage = docPage.GetEditPage();
  747. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(pageIndex);
  748. 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)));
  749. commandData.PDFRect = offsetRect;
  750. if (commandData.EditType == CPDFEditType.EditText)
  751. {
  752. editType = CPDFEditType.EditText;
  753. if (pasteMatchStyle && commandData.EditAreaCopied)
  754. {
  755. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  756. if (editPage.CanUndo())
  757. {
  758. PDFEditHistory editHistory = new PDFEditHistory();
  759. editHistory.EditPage = editPage;
  760. editHistory.PageIndex = pageIndex;
  761. groupHistory.Histories.Add(editHistory);
  762. CPDFViewer pdfViewer = GetCPDFViewer();
  763. if (pdfViewer != null && pdfViewer.UndoManager != null)
  764. {
  765. pdfViewer.UndoManager.AddHistory(groupHistory);
  766. }
  767. }
  768. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  769. }
  770. else
  771. {
  772. PDFEditHistory editHistory = new PDFEditHistory();
  773. if (!string.IsNullOrEmpty(commandData.TextContent))
  774. {
  775. EditAreaObject editAreaObject = GetHitTestAreaObject(hoverPoint);
  776. if (editAreaObject != null && editAreaObject.cPDFEditArea is CPDFEditTextArea)
  777. {
  778. CPDFEditTextArea TextArea = editAreaObject.cPDFEditArea as CPDFEditTextArea;
  779. if (TextArea.SelectLineRects.Count > 0)
  780. {
  781. TextArea.DeleteChars();
  782. TextArea.ClearSelectChars();
  783. PDFEditHistory deleteHistory = new PDFEditHistory();
  784. deleteHistory.EditPage = editPage;
  785. deleteHistory.PageIndex = pageIndex;
  786. groupHistory.Histories.Add(deleteHistory);
  787. }
  788. TextArea.InsertText(commandData.TextContent);
  789. editHistory.EditPage = editPage;
  790. editHistory.PageIndex = pageIndex;
  791. groupHistory.Histories.Add(editHistory);
  792. CPDFViewer pdfViewer = GetCPDFViewer();
  793. if (pdfViewer != null && pdfViewer.UndoManager != null)
  794. {
  795. pdfViewer.UndoManager.AddHistory(groupHistory);
  796. }
  797. }
  798. else
  799. {
  800. PDFEditHistory createHistory = new PDFEditHistory();
  801. CPDFEditTextArea editArea = editPage.CreateNewTextArea(DataConversionForWPF.RectConversionForCRect(offsetRect), "Helvetica", 14, new byte[3] { 0, 0, 0 });
  802. createHistory.EditPage = editPage;
  803. createHistory.PageIndex = pageIndex;
  804. groupHistory.Histories.Add(createHistory);
  805. if (editArea != null)
  806. {
  807. if (editArea.InsertText(commandData.TextContent))
  808. {
  809. editHistory.EditPage = editPage;
  810. editHistory.PageIndex = pageIndex;
  811. groupHistory.Histories.Add(editHistory);
  812. }
  813. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  814. }
  815. CPDFViewer pdfViewer = GetCPDFViewer();
  816. if (pdfViewer != null && pdfViewer.UndoManager != null)
  817. {
  818. pdfViewer.UndoManager.AddHistory(groupHistory);
  819. }
  820. }
  821. }
  822. }
  823. }
  824. if (commandData.EditType == CPDFEditType.EditImage)
  825. {
  826. editType = CPDFEditType.EditImage;
  827. PDFEditHistory editHistory = new PDFEditHistory();
  828. if (commandData.EditAreaCopied)
  829. {
  830. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  831. editHistory.EditPage = editPage;
  832. editHistory.PageIndex = pageIndex;
  833. CPDFViewer pdfViewer = GetCPDFViewer();
  834. if (pdfViewer != null && pdfViewer.UndoManager != null)
  835. {
  836. pdfViewer.UndoManager.AddHistory(editHistory);
  837. }
  838. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  839. }
  840. else
  841. {
  842. BitmapSource bitmapSource = BinaryStructConverter.ImageFromClipboardDib();
  843. if (bitmapSource != null)
  844. {
  845. PDFEditHistory createHistory = new PDFEditHistory();
  846. byte[] imageData = new byte[bitmapSource.PixelWidth * bitmapSource.PixelHeight * 4];
  847. if (bitmapSource.Format != PixelFormats.Bgra32)
  848. {
  849. FormatConvertedBitmap covert = new FormatConvertedBitmap(bitmapSource, PixelFormats.Bgra32, bitmapSource.Palette, 0);
  850. covert.CopyPixels(imageData, bitmapSource.PixelWidth * 4, 0);
  851. }
  852. else
  853. {
  854. bitmapSource.CopyPixels(imageData, bitmapSource.PixelWidth * 4, 0);
  855. }
  856. CPDFEditImageArea editArea = editPage.CreateNewImageArea(DataConversionForWPF.RectConversionForCRect(offsetRect), imageData, bitmapSource.PixelWidth, bitmapSource.PixelHeight);
  857. createHistory.EditPage = editPage;
  858. createHistory.PageIndex = pageIndex;
  859. groupHistory.Histories.Add(createHistory);
  860. if (editArea != null)
  861. {
  862. editHistory.EditPage = editPage;
  863. editHistory.PageIndex = pageIndex;
  864. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  865. }
  866. CPDFViewer pdfViewer = GetCPDFViewer();
  867. if (pdfViewer != null && pdfViewer.UndoManager != null)
  868. {
  869. pdfViewer.UndoManager.AddHistory(groupHistory);
  870. }
  871. }
  872. }
  873. }
  874. if (commandData.EditType == CPDFEditType.EditPath)
  875. {
  876. editType = CPDFEditType.EditPath;
  877. PDFEditHistory editHistory = new PDFEditHistory();
  878. if (commandData.EditAreaCopied)
  879. {
  880. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  881. editHistory.EditPage = editPage;
  882. editHistory.PageIndex = pageIndex;
  883. CPDFViewer pdfViewer = GetCPDFViewer();
  884. if (pdfViewer != null && pdfViewer.UndoManager != null)
  885. {
  886. pdfViewer.UndoManager.AddHistory(editHistory);
  887. }
  888. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  889. }
  890. }
  891. editPage.EndEdit();
  892. }
  893. }
  894. private void SelectAllPDFEditData(out CPDFEditType editType)
  895. {
  896. editType = CPDFEditType.None;
  897. if (currentEditAreaObject != null && currentEditAreaObject.cPDFEditPage != null)
  898. {
  899. CPDFEditTextArea textArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  900. textArea.SelectAllChars();
  901. editType = CPDFEditType.EditText;
  902. PDFViewer.UpdateRenderFrame();
  903. }
  904. }
  905. private Rect AddPasteOffset(Rect clientRect, int offsetX, int offsetY, Size pageSize)
  906. {
  907. clientRect.X += offsetX;
  908. clientRect.Y += offsetY;
  909. if (clientRect.Left < 0)
  910. {
  911. clientRect.X = 0;
  912. }
  913. if (clientRect.Top < 0)
  914. {
  915. clientRect.Y = 0;
  916. }
  917. if (clientRect.Right > pageSize.Width)
  918. {
  919. clientRect.X = pageSize.Width - Math.Min(clientRect.Width, pageSize.Width);
  920. }
  921. if (clientRect.Bottom > pageSize.Height)
  922. {
  923. clientRect.Y = pageSize.Height - Math.Min(clientRect.Height, pageSize.Height);
  924. }
  925. return clientRect;
  926. }
  927. #endregion
  928. #region Annot
  929. private bool CheckCacheHitTestAnnot(string cmdName = "")
  930. {
  931. if (cacheHitTestAnnot == null)
  932. {
  933. return false;
  934. }
  935. AnnotData hitData = cacheHitTestAnnot.GetAnnotData();
  936. if (hitData == null)
  937. {
  938. return false;
  939. }
  940. if (hitData.Annot == null)
  941. {
  942. return false;
  943. }
  944. if (cmdName == "Delete" && hitData.Annot is CPDFSignatureWidget)
  945. {
  946. CPDFSignatureWidget signAnnot = hitData.Annot as CPDFSignatureWidget;
  947. if (signAnnot != null && signAnnot.IsSigned())
  948. {
  949. return false;
  950. }
  951. }
  952. return true;
  953. }
  954. private bool CheckAnnotCanDoCopyCut(bool isCut = false)
  955. {
  956. if (!CheckCacheHitTestAnnot(isCut ? "Delete" : ""))
  957. {
  958. return false;
  959. }
  960. if (cacheHitTestAnnot.GetAnnotData().Annot.GetIsLocked() && isCut)
  961. {
  962. return false;
  963. }
  964. switch (cacheHitTestAnnot.CurrentType)
  965. {
  966. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  967. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  968. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  969. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  970. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  971. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  972. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  973. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  974. return false;
  975. default:
  976. break;
  977. }
  978. return true;
  979. }
  980. private void CheckViewerCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  981. {
  982. switch (uiCommand.Name)
  983. {
  984. case "Copy":
  985. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  986. if (!e.CanExecute && textSelectInfo != null)
  987. {
  988. foreach (int key in textSelectInfo.PageSelectText.Keys)
  989. {
  990. if (textSelectInfo.PageSelectText[key] != string.Empty)
  991. {
  992. e.CanExecute = true;
  993. break;
  994. }
  995. }
  996. }
  997. break;
  998. default:
  999. return;
  1000. }
  1001. }
  1002. private void CheckAnnotCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  1003. {
  1004. switch (uiCommand.Name)
  1005. {
  1006. case "Copy":
  1007. e.CanExecute = CheckAnnotCanDoCopyCut();
  1008. if (!e.CanExecute)
  1009. {
  1010. AnnotSelector annotSelector = GetAnnotSelectsRect();
  1011. if (annotSelector != null && annotSelector.SelectAnnots.Count > 0)
  1012. {
  1013. e.CanExecute = true;
  1014. }
  1015. }
  1016. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  1017. if (!e.CanExecute && textSelectInfo != null)
  1018. {
  1019. foreach (int key in textSelectInfo.PageSelectText.Keys)
  1020. {
  1021. if (textSelectInfo.PageSelectText[key] != string.Empty)
  1022. {
  1023. e.CanExecute = true;
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. break;
  1029. case "Cut":
  1030. e.CanExecute = CheckAnnotCanDoCopyCut(true);
  1031. if (!e.CanExecute)
  1032. {
  1033. AnnotSelector annotSelector = GetAnnotSelectsRect();
  1034. if (annotSelector != null && annotSelector.SelectAnnots.Count > 0)
  1035. {
  1036. e.CanExecute = true;
  1037. }
  1038. }
  1039. break;
  1040. case "Delete":
  1041. e.CanExecute = CheckCacheHitTestAnnot("Delete");
  1042. if (!e.CanExecute)
  1043. {
  1044. AnnotSelector annotSelector = GetAnnotSelectsRect();
  1045. if (annotSelector != null && annotSelector.SelectAnnots.Count > 0)
  1046. {
  1047. e.CanExecute = true;
  1048. }
  1049. }
  1050. break;
  1051. case "Paste":
  1052. if (lastAnnotList.Count > 0)
  1053. {
  1054. e.CanExecute = true;
  1055. }
  1056. try
  1057. {
  1058. if (Clipboard.ContainsText())
  1059. {
  1060. if (!string.IsNullOrEmpty(Clipboard.GetText()))
  1061. {
  1062. e.CanExecute = true;
  1063. }
  1064. }
  1065. }
  1066. catch (Exception ex)
  1067. {
  1068. }
  1069. break;
  1070. }
  1071. }
  1072. private void ExecuteViewerCommand(RoutedUICommand uiCommand)
  1073. {
  1074. switch (uiCommand.Name)
  1075. {
  1076. case "Copy":
  1077. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  1078. if (textSelectInfo != null)
  1079. {
  1080. StringBuilder copyTextBuilder = new StringBuilder();
  1081. foreach (int key in textSelectInfo.PageSelectText.Keys)
  1082. {
  1083. if (textSelectInfo.PageSelectText[key] != string.Empty)
  1084. {
  1085. copyTextBuilder.Append(textSelectInfo.PageSelectText[key]);
  1086. }
  1087. }
  1088. if (copyTextBuilder.Length > 0)
  1089. {
  1090. try
  1091. {
  1092. Clipboard.Clear();
  1093. Clipboard.SetText(copyTextBuilder.ToString());
  1094. }
  1095. catch (Exception)
  1096. {
  1097. }
  1098. }
  1099. }
  1100. break;
  1101. default:
  1102. return;
  1103. }
  1104. }
  1105. private void ExecuteAnnotCommand(RoutedUICommand uiCommand)
  1106. {
  1107. AnnotSelector mrect = GetAnnotSelectsRect();
  1108. switch (uiCommand.Name)
  1109. {
  1110. case "Copy":
  1111. try
  1112. {
  1113. if (Clipboard.ContainsText())
  1114. {
  1115. Clipboard.SetText("");
  1116. }
  1117. }
  1118. catch (Exception ex)
  1119. {
  1120. }
  1121. if (mrect != null && mrect.SelectAnnots.Count > 0)
  1122. {
  1123. lastAnnotList.Clear();
  1124. foreach (var annotData in mrect.SelectAnnots)
  1125. {
  1126. var annotParam = ParamConverter.AnnotConverter(PDFViewer.GetDocument(), annotData.Annot);
  1127. lastAnnotList.Add(annotParam);
  1128. }
  1129. return;
  1130. }
  1131. if (cacheHitTestAnnot != null)
  1132. {
  1133. lastAnnotList.Clear();
  1134. AnnotParam annotParam = ParamConverter.AnnotConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1135. lastAnnotList.Add(annotParam);
  1136. }
  1137. if (cacheMoveWidget != null)
  1138. {
  1139. lastAnnotList.Clear();
  1140. AnnotParam annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1141. lastAnnotList.Add(annotParam);
  1142. }
  1143. else
  1144. {
  1145. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  1146. if (textSelectInfo != null)
  1147. {
  1148. StringBuilder copyTextBuilder = new StringBuilder();
  1149. foreach (int key in textSelectInfo.PageSelectText.Keys)
  1150. {
  1151. if (textSelectInfo.PageSelectText[key] != string.Empty)
  1152. {
  1153. copyTextBuilder.Append(textSelectInfo.PageSelectText[key]);
  1154. }
  1155. }
  1156. if (copyTextBuilder.Length > 0)
  1157. {
  1158. try
  1159. {
  1160. Clipboard.Clear();
  1161. Clipboard.SetText(copyTextBuilder.ToString());
  1162. }
  1163. catch (Exception)
  1164. {
  1165. }
  1166. }
  1167. }
  1168. }
  1169. break;
  1170. case "Cut":
  1171. try
  1172. {
  1173. if (Clipboard.ContainsText())
  1174. {
  1175. Clipboard.SetText("");
  1176. }
  1177. }
  1178. catch (Exception ex)
  1179. {
  1180. }
  1181. if (mrect != null && mrect.SelectAnnots.Count > 0)
  1182. {
  1183. lastAnnotList.Clear();
  1184. foreach (var annotData in mrect.SelectAnnots)
  1185. {
  1186. var annotParam = ParamConverter.AnnotConverter(PDFViewer.GetDocument(), annotData.Annot);
  1187. lastAnnotList.Add(annotParam);
  1188. }
  1189. DeleteAnnotData();
  1190. PDFViewer.UpdateAnnotFrame();
  1191. return;
  1192. }
  1193. if (cacheHitTestAnnot != null)
  1194. {
  1195. lastAnnotList.Clear();
  1196. AnnotParam annotParam;
  1197. if (cacheHitTestAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  1198. {
  1199. annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1200. }
  1201. else
  1202. {
  1203. annotParam = ParamConverter.AnnotConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1204. }
  1205. lastAnnotList.Add(annotParam);
  1206. }
  1207. if (cacheMoveWidget != null)
  1208. {
  1209. lastAnnotList.Clear();
  1210. AnnotParam annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1211. lastAnnotList.Add(annotParam);
  1212. }
  1213. DeleteAnnotData();
  1214. PDFViewer.UpdateAnnotFrame();
  1215. break;
  1216. case "Delete":
  1217. DeleteAnnotData();
  1218. PDFViewer.UpdateAnnotFrame();
  1219. break;
  1220. case "Paste":
  1221. PasteAnnotData();
  1222. PDFViewer.UpdateAnnotFrame();
  1223. break;
  1224. }
  1225. }
  1226. private void PasteAnnotData(bool pasteMatchStyle = false)
  1227. {
  1228. GroupHistory groupHistory = new GroupHistory();
  1229. string CopyString = CopyText;
  1230. try
  1231. {
  1232. if (Clipboard.ContainsText())
  1233. {
  1234. if (!string.IsNullOrEmpty(Clipboard.GetText()))
  1235. {
  1236. CopyString = Clipboard.GetText();
  1237. }
  1238. }
  1239. }
  1240. catch (Exception ex)
  1241. {
  1242. }
  1243. if (lastAnnotList.Count > 0&& CopyText== CopyString)
  1244. {
  1245. foreach (AnnotParam item in lastAnnotList)
  1246. {
  1247. AnnotHistory annotHistory = null;
  1248. if (item == null)
  1249. {
  1250. continue;
  1251. }
  1252. Point point = rightPressPoint;
  1253. rightPressPoint = new Point(-1, -1);
  1254. switch (item.CurrentType)
  1255. {
  1256. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  1257. {
  1258. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1259. Rect rect;
  1260. annotHistory = new StickyNoteAnnotHistory();
  1261. annotHistory.PDFDoc = cPDFDocument;
  1262. int index = -1;
  1263. if (point.Equals(new Point(-1, -1)))
  1264. {
  1265. index = item.PageIndex;
  1266. rect = new Rect(
  1267. (item.ClientRect.left + 25),
  1268. (item.ClientRect.top + 25),
  1269. item.ClientRect.width(),
  1270. item.ClientRect.height()
  1271. );
  1272. }
  1273. else
  1274. {
  1275. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1276. CRect cRect = item.ClientRect;
  1277. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1278. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1279. rect = new Rect(
  1280. (pdfPoint.X - cRect.width() / 2),
  1281. (pdfPoint.Y - cRect.height() / 2),
  1282. cRect.width(),
  1283. cRect.height()
  1284. );
  1285. }
  1286. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1287. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1288. if (cPDFPage == null)
  1289. return;
  1290. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1291. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1292. cPDFAnnotation.SetRect(setRect);
  1293. CommonHelper.UpdateStickyAP(cPDFAnnotation as CPDFTextAnnotation);
  1294. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1295. (annotHistory as StickyNoteAnnotHistory).CurrentParam = (StickyNoteParam)annotParam;
  1296. }
  1297. break;
  1298. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  1299. {
  1300. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1301. Rect rect;
  1302. annotHistory = new LinkAnnotHistory();
  1303. annotHistory.PDFDoc = cPDFDocument;
  1304. int index = -1;
  1305. if (point.Equals(new Point(-1, -1)))
  1306. {
  1307. index = item.PageIndex;
  1308. rect = new Rect(
  1309. (item.ClientRect.left + 25),
  1310. (item.ClientRect.top + 25),
  1311. item.ClientRect.width(),
  1312. item.ClientRect.height()
  1313. );
  1314. }
  1315. else
  1316. {
  1317. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1318. CRect cRect = item.ClientRect;
  1319. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1320. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1321. rect = new Rect(
  1322. (pdfPoint.X - cRect.width() / 2),
  1323. (pdfPoint.Y - cRect.height() / 2),
  1324. cRect.width(),
  1325. cRect.height()
  1326. );
  1327. }
  1328. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1329. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1330. if (cPDFPage == null)
  1331. return;
  1332. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1333. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1334. cPDFAnnotation.SetRect(setRect);
  1335. cPDFAnnotation.UpdateAp();
  1336. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1337. (annotHistory as LinkAnnotHistory).CurrentParam = (LinkParam)annotParam;
  1338. }
  1339. break;
  1340. case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
  1341. {
  1342. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1343. Rect rect;
  1344. annotHistory = new FreeTextAnnotHistory();
  1345. annotHistory.PDFDoc = cPDFDocument;
  1346. int index = -1;
  1347. if (point.Equals(new Point(-1, -1)))
  1348. {
  1349. index = item.PageIndex;
  1350. rect = new Rect(
  1351. (item.ClientRect.left + 25),
  1352. (item.ClientRect.top + 25),
  1353. item.ClientRect.width(),
  1354. item.ClientRect.height()
  1355. );
  1356. }
  1357. else
  1358. {
  1359. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1360. CRect cRect = item.ClientRect;
  1361. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1362. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1363. rect = new Rect(
  1364. (pdfPoint.X - cRect.width() / 2),
  1365. (pdfPoint.Y - cRect.height() / 2),
  1366. cRect.width(),
  1367. cRect.height()
  1368. );
  1369. }
  1370. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1371. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1372. if (cPDFPage == null)
  1373. return;
  1374. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1375. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1376. cPDFAnnotation.SetRect(setRect);
  1377. cPDFAnnotation.UpdateAp();
  1378. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1379. (annotHistory as FreeTextAnnotHistory).CurrentParam = (FreeTextParam)annotParam;
  1380. }
  1381. break;
  1382. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1383. {
  1384. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1385. Rect rect;
  1386. annotHistory = new LineAnnotHistory();
  1387. annotHistory.PDFDoc = cPDFDocument;
  1388. int index = -1;
  1389. if (point.Equals(new Point(-1, -1)))
  1390. {
  1391. index = item.PageIndex;
  1392. rect = new Rect(
  1393. (item.ClientRect.left + 25),
  1394. (item.ClientRect.top + 25),
  1395. item.ClientRect.width(),
  1396. item.ClientRect.height()
  1397. );
  1398. }
  1399. else
  1400. {
  1401. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1402. CRect cRect = item.ClientRect;
  1403. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1404. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1405. rect = new Rect(
  1406. (pdfPoint.X - cRect.width() / 2),
  1407. (pdfPoint.Y - cRect.height() / 2),
  1408. cRect.width(),
  1409. cRect.height()
  1410. );
  1411. }
  1412. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1413. LineParam newLineParam = new LineParam();
  1414. item.CopyTo(newLineParam);
  1415. {
  1416. float offsetX = setRect.left - item.ClientRect.left;
  1417. float offsetY = setRect.top - item.ClientRect.top;
  1418. newLineParam.HeadPoint = new CPoint(newLineParam.HeadPoint.x + offsetX, newLineParam.HeadPoint.y + offsetY);
  1419. newLineParam.TailPoint = new CPoint(newLineParam.TailPoint.x + offsetX, newLineParam.TailPoint.y + offsetY);
  1420. }
  1421. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1422. if (cPDFPage == null)
  1423. return;
  1424. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1425. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1426. (cPDFAnnotation as CPDFLineAnnotation)?.SetLinePoints(newLineParam.HeadPoint, newLineParam.TailPoint);
  1427. cPDFAnnotation.SetRect(setRect);
  1428. cPDFAnnotation.UpdateAp();
  1429. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1430. (annotHistory as LineAnnotHistory).CurrentParam = (LineParam)annotParam;
  1431. }
  1432. break;
  1433. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
  1434. {
  1435. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1436. Rect rect;
  1437. annotHistory = new SquareAnnotHistory();
  1438. annotHistory.PDFDoc = cPDFDocument;
  1439. int index = -1;
  1440. if (point.Equals(new Point(-1, -1)))
  1441. {
  1442. index = item.PageIndex;
  1443. rect = new Rect(
  1444. (item.ClientRect.left + 25),
  1445. (item.ClientRect.top + 25),
  1446. item.ClientRect.width(),
  1447. item.ClientRect.height()
  1448. );
  1449. }
  1450. else
  1451. {
  1452. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1453. CRect cRect = item.ClientRect;
  1454. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1455. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1456. rect = new Rect(
  1457. (pdfPoint.X - cRect.width() / 2),
  1458. (pdfPoint.Y - cRect.height() / 2),
  1459. cRect.width(),
  1460. cRect.height()
  1461. );
  1462. }
  1463. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1464. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1465. if (cPDFPage == null)
  1466. return;
  1467. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1468. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1469. cPDFAnnotation.SetRect(setRect);
  1470. CPDFTextPage textPage = cPDFPage.GetTextPage();
  1471. if (textPage != null)
  1472. {
  1473. string newContent = textPage.GetBoundedText(setRect);
  1474. cPDFAnnotation.SetContent(newContent);
  1475. }
  1476. cPDFAnnotation.UpdateAp();
  1477. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1478. (annotHistory as SquareAnnotHistory).CurrentParam = (SquareParam)annotParam;
  1479. }
  1480. break;
  1481. case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
  1482. {
  1483. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1484. Rect rect;
  1485. annotHistory = new CircleAnnotHistory();
  1486. annotHistory.PDFDoc = cPDFDocument;
  1487. int index = -1;
  1488. if (point.Equals(new Point(-1, -1)))
  1489. {
  1490. index = item.PageIndex;
  1491. rect = new Rect(
  1492. (item.ClientRect.left + 25),
  1493. (item.ClientRect.top + 25),
  1494. item.ClientRect.width(),
  1495. item.ClientRect.height()
  1496. );
  1497. }
  1498. else
  1499. {
  1500. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1501. CRect cRect = item.ClientRect;
  1502. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1503. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1504. rect = new Rect(
  1505. (pdfPoint.X - cRect.width() / 2),
  1506. (pdfPoint.Y - cRect.height() / 2),
  1507. cRect.width(),
  1508. cRect.height()
  1509. );
  1510. }
  1511. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1512. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1513. if (cPDFPage == null)
  1514. return;
  1515. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1516. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1517. cPDFAnnotation.SetRect(setRect);
  1518. CPDFTextPage textPage = cPDFPage.GetTextPage();
  1519. if (textPage != null)
  1520. {
  1521. string newContent = textPage.GetBoundedText(setRect);
  1522. cPDFAnnotation.SetContent(newContent);
  1523. }
  1524. cPDFAnnotation.UpdateAp();
  1525. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1526. (annotHistory as CircleAnnotHistory).CurrentParam = (CircleParam)annotParam;
  1527. }
  1528. break;
  1529. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1530. {
  1531. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1532. Rect rect;
  1533. annotHistory = new PolygonMeasureAnnotHistory();
  1534. annotHistory.PDFDoc = cPDFDocument;
  1535. int index = -1;
  1536. if (point.Equals(new Point(-1, -1)))
  1537. {
  1538. index = item.PageIndex;
  1539. rect = new Rect(
  1540. (item.ClientRect.left + 25),
  1541. (item.ClientRect.top + 25),
  1542. item.ClientRect.width(),
  1543. item.ClientRect.height()
  1544. );
  1545. }
  1546. else
  1547. {
  1548. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1549. CRect cRect = item.ClientRect;
  1550. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1551. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1552. rect = new Rect(
  1553. (pdfPoint.X - cRect.width() / 2),
  1554. (pdfPoint.Y - cRect.height() / 2),
  1555. cRect.width(),
  1556. cRect.height()
  1557. );
  1558. }
  1559. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1560. PolygonMeasureParam newPolygonParam = new PolygonMeasureParam();
  1561. item.CopyTo(newPolygonParam);
  1562. if (newPolygonParam.SavePoints != null && newPolygonParam.SavePoints.Count > 0)
  1563. {
  1564. float offsetX = setRect.left - item.ClientRect.left;
  1565. float offsetY = setRect.top - item.ClientRect.top;
  1566. List<CPoint> arrangeList = new List<CPoint>();
  1567. foreach (CPoint addPoint in newPolygonParam.SavePoints)
  1568. {
  1569. arrangeList.Add(new CPoint(addPoint.x + offsetX, addPoint.y + offsetY));
  1570. }
  1571. newPolygonParam.SavePoints = arrangeList;
  1572. newPolygonParam.ClientRect = setRect;
  1573. }
  1574. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1575. if (cPDFPage == null)
  1576. return;
  1577. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(newPolygonParam.CurrentType);
  1578. CreateDefaultAnnot(cPDFAnnotation, newPolygonParam.CurrentType, newPolygonParam);
  1579. cPDFAnnotation.UpdateAp();
  1580. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1581. (annotHistory as PolygonMeasureAnnotHistory).CurrentParam = (PolygonMeasureParam)annotParam;
  1582. }
  1583. break;
  1584. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  1585. break;
  1586. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  1587. {
  1588. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1589. Rect rect;
  1590. annotHistory = new StampAnnotHistory();
  1591. annotHistory.PDFDoc = cPDFDocument;
  1592. int index = -1;
  1593. if (point.Equals(new Point(-1, -1)))
  1594. {
  1595. index = item.PageIndex;
  1596. rect = new Rect(
  1597. (item.ClientRect.left + 25),
  1598. (item.ClientRect.top + 25),
  1599. item.ClientRect.width(),
  1600. item.ClientRect.height()
  1601. );
  1602. }
  1603. else
  1604. {
  1605. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1606. CRect cRect = (item as StampParam).SourceRect;
  1607. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1608. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1609. rect = new Rect(
  1610. (pdfPoint.X - cRect.width() / 2),
  1611. (pdfPoint.Y - cRect.height() / 2),
  1612. cRect.width(),
  1613. cRect.height()
  1614. );
  1615. }
  1616. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1617. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1618. if (cPDFPage == null)
  1619. return;
  1620. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1621. (cPDFAnnotation as CPDFStampAnnotation).SetSourceRect(setRect);
  1622. //cPDFAnnotation.SetRect(setRect);
  1623. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1624. cPDFAnnotation.UpdateAp();
  1625. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1626. (annotHistory as StampAnnotHistory).CurrentParam = (StampParam)annotParam;
  1627. }
  1628. break;
  1629. case C_ANNOTATION_TYPE.C_ANNOTATION_CARET:
  1630. break;
  1631. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  1632. {
  1633. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1634. Rect rect;
  1635. annotHistory = new InkAnnotHistory();
  1636. annotHistory.PDFDoc = cPDFDocument;
  1637. int index = -1;
  1638. if (point.Equals(new Point(-1, -1)))
  1639. {
  1640. index = item.PageIndex;
  1641. rect = new Rect(
  1642. (item.ClientRect.left + 25),
  1643. (item.ClientRect.top + 25),
  1644. item.ClientRect.width(),
  1645. item.ClientRect.height()
  1646. );
  1647. }
  1648. else
  1649. {
  1650. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1651. CRect cRect = item.ClientRect;
  1652. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1653. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1654. rect = new Rect(
  1655. (pdfPoint.X - cRect.width() / 2),
  1656. (pdfPoint.Y - cRect.height() / 2),
  1657. cRect.width(),
  1658. cRect.height()
  1659. );
  1660. }
  1661. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1662. InkParam newInkParam = new InkParam();
  1663. item.CopyTo(newInkParam);
  1664. if (newInkParam.InkPath != null && newInkParam.InkPath.Count > 0)
  1665. {
  1666. float offsetX = setRect.left - item.ClientRect.left;
  1667. float offsetY = setRect.top - item.ClientRect.top;
  1668. List<List<CPoint>> arrangeList = new List<List<CPoint>>();
  1669. foreach (List<CPoint> inkNode in newInkParam.InkPath)
  1670. {
  1671. List<CPoint> inkPath = new List<CPoint>();
  1672. arrangeList.Add(inkPath);
  1673. foreach (CPoint addPoint in inkNode)
  1674. {
  1675. inkPath.Add(new CPoint(addPoint.x + offsetX, addPoint.y + offsetY));
  1676. }
  1677. }
  1678. newInkParam.InkPath = arrangeList;
  1679. newInkParam.ClientRect = setRect;
  1680. }
  1681. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1682. if (cPDFPage == null)
  1683. return;
  1684. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(newInkParam.CurrentType);
  1685. CreateDefaultAnnot(cPDFAnnotation, newInkParam.CurrentType, newInkParam);
  1686. cPDFAnnotation.UpdateAp();
  1687. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1688. (annotHistory as InkAnnotHistory).CurrentParam = (InkParam)annotParam;
  1689. }
  1690. break;
  1691. case C_ANNOTATION_TYPE.C_ANNOTATION_POPUP:
  1692. break;
  1693. case C_ANNOTATION_TYPE.C_ANNOTATION_FILEATTACHMENT:
  1694. break;
  1695. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  1696. {
  1697. rightPressPoint = point;
  1698. bool result = PasteWidgetData(ref annotHistory, item as WidgetParm);
  1699. if (!result)
  1700. return;
  1701. }
  1702. break;
  1703. case C_ANNOTATION_TYPE.C_ANNOTATION_SCREEN:
  1704. break;
  1705. case C_ANNOTATION_TYPE.C_ANNOTATION_PRINTERMARK:
  1706. break;
  1707. case C_ANNOTATION_TYPE.C_ANNOTATION_TRAPNET:
  1708. break;
  1709. case C_ANNOTATION_TYPE.C_ANNOTATION_WATERMARK:
  1710. break;
  1711. case C_ANNOTATION_TYPE.C_ANNOTATION_3D:
  1712. break;
  1713. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  1714. break;
  1715. case C_ANNOTATION_TYPE.C_ANNOTATION_INTERCHANGE:
  1716. break;
  1717. default:
  1718. break;
  1719. }
  1720. if (annotHistory != null)
  1721. {
  1722. PasteParam = annotHistory.CurrentParam;
  1723. groupHistory.Histories.Add(annotHistory);
  1724. }
  1725. }
  1726. PDFViewer.UndoManager.AddHistory(groupHistory);
  1727. }
  1728. else
  1729. {
  1730. AnnotHistory annotHistory = null;
  1731. if (string.IsNullOrEmpty(CopyString))
  1732. {
  1733. return;
  1734. }
  1735. Point point = rightPressPoint;
  1736. rightPressPoint = new Point(-1, -1);
  1737. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1738. Rect rect;
  1739. annotHistory = new FreeTextAnnotHistory();
  1740. annotHistory.PDFDoc = cPDFDocument;
  1741. int index = -1;
  1742. FreeTextParam freeTextParam = new FreeTextParam();
  1743. freeTextParam.Content = CopyString;
  1744. freeTextParam.FontSize = 14;
  1745. freeTextParam.FontColor = new byte[3] { 0, 0, 0 };
  1746. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1747. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1748. if (cPDFPage == null)
  1749. return;
  1750. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(freeTextParam.CurrentType);
  1751. var freeTextAnnot = cPDFAnnotation as CPDFFreeTextAnnotation;
  1752. freeTextAnnot.SetContent(CopyString);
  1753. freeTextParam.ClientRect = freeTextAnnot.GetTextRect();
  1754. if (point.Equals(new Point(-1, -1)))
  1755. {
  1756. return;
  1757. }
  1758. else
  1759. {
  1760. CRect cRect = freeTextParam.ClientRect;
  1761. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1762. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1763. rect = new Rect(
  1764. (pdfPoint.X - cRect.width() / 2),
  1765. (pdfPoint.Y - cRect.height() / 2),
  1766. cRect.width(),
  1767. cRect.height()
  1768. );
  1769. }
  1770. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1771. CreateDefaultAnnot(cPDFAnnotation, freeTextParam.CurrentType, freeTextParam);
  1772. cPDFAnnotation.SetRect(setRect);
  1773. cPDFAnnotation.UpdateAp();
  1774. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1775. (annotHistory as FreeTextAnnotHistory).CurrentParam = (FreeTextParam)annotParam;
  1776. PasteParam = annotHistory.CurrentParam;
  1777. PDFViewer.UndoManager.AddHistory(annotHistory);
  1778. }
  1779. }
  1780. private void DeleteAnnotData()
  1781. {
  1782. dynamic notifyData = null;
  1783. AnnotSelector mrect = GetAnnotSelectsRect();
  1784. if (mrect != null && mrect.SelectAnnots.Count > 0)
  1785. {
  1786. GroupHistory historyGroup = new GroupHistory();
  1787. List<AnnotData> annotDatas = new List<AnnotData>();
  1788. foreach (var annotData in mrect.SelectAnnots)
  1789. {
  1790. annotDatas.Add(annotData);
  1791. }
  1792. annotDatas = annotDatas.OrderByDescending(x => x.AnnotIndex).ToList();
  1793. List<AnnotParam> annotParams = new List<AnnotParam>();
  1794. foreach (var annotData in annotDatas)
  1795. {
  1796. annotParams.Add(ParamConverter.AnnotConverter(PDFViewer.GetDocument(), annotData.Annot));
  1797. }
  1798. foreach (var annotData in annotDatas)
  1799. {
  1800. var annotHistory = ParamConverter.CreateHistory(annotData.Annot);
  1801. var annotParam = annotParams[annotDatas.IndexOf(annotData)];
  1802. if (annotParam != null)
  1803. {
  1804. notifyData = new ExpandoObject();
  1805. notifyData.Action = HistoryAction.Remove;
  1806. notifyData.PageIndex = annotParam.PageIndex;
  1807. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1808. notifyData.AnnotType = annotParam.CurrentType;
  1809. notifyData.CurrentParam = annotParam;
  1810. if (annotParam is WidgetParm)
  1811. {
  1812. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1813. }
  1814. annotHistory.CurrentParam = annotParam;
  1815. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1816. annotHistory.Action = HistoryAction.Remove;
  1817. historyGroup.Histories.Add(annotHistory);
  1818. List<C_ANNOTATION_TYPE> checkEditType = new List<C_ANNOTATION_TYPE>()
  1819. {
  1820. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1821. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1822. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON
  1823. };
  1824. if (checkEditType.Contains(annotData.AnnotType))
  1825. {
  1826. //Need to clear the measurement selected edit box.
  1827. CleanEditAnnot();
  1828. }
  1829. annotData.Annot.RemoveAnnot();
  1830. }
  1831. }
  1832. mrect.ClearItem();
  1833. mrect.CleanDraw();
  1834. mrect = null;
  1835. PDFViewer.UndoManager.AddHistory(historyGroup);
  1836. }
  1837. else
  1838. if (cacheHitTestAnnot != null)
  1839. {
  1840. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheHitTestAnnot.GetAnnotData().Annot);
  1841. if (annotHistory == null)
  1842. {
  1843. return;
  1844. }
  1845. AnnotParam annotParam = null;
  1846. if (cacheHitTestAnnot is BaseWidget)
  1847. {
  1848. annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1849. }
  1850. else
  1851. {
  1852. annotParam = ParamConverter.AnnotConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1853. }
  1854. if (annotParam != null)
  1855. {
  1856. notifyData = new ExpandoObject();
  1857. notifyData.Action = HistoryAction.Remove;
  1858. notifyData.PageIndex = annotParam.PageIndex;
  1859. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1860. notifyData.AnnotType = annotParam.CurrentType;
  1861. notifyData.CurrentParam = annotParam;
  1862. if (annotParam is WidgetParm)
  1863. {
  1864. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1865. }
  1866. annotHistory.CurrentParam = annotParam;
  1867. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1868. annotHistory.Action = HistoryAction.Remove;
  1869. PDFViewer.UndoManager.AddHistory(annotHistory);
  1870. List<C_ANNOTATION_TYPE> checkEditType = new List<C_ANNOTATION_TYPE>()
  1871. {
  1872. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1873. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1874. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON
  1875. };
  1876. if (checkEditType.Contains(cacheHitTestAnnot.CurrentType))
  1877. {
  1878. //Need to clear the measurement selected edit box.
  1879. CleanEditAnnot();
  1880. }
  1881. cacheHitTestAnnot.RemoveClean(true);
  1882. cacheHitTestAnnot.GetAnnotData().Annot.RemoveAnnot();
  1883. cacheHitTestAnnot = null;
  1884. }
  1885. CleanSelectedRect();
  1886. }
  1887. else if (cacheMoveWidget != null)
  1888. {
  1889. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheMoveWidget.GetAnnotData().Annot);
  1890. if (annotHistory == null)
  1891. {
  1892. return;
  1893. }
  1894. AnnotParam annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1895. if (annotParam != null)
  1896. {
  1897. notifyData = new ExpandoObject();
  1898. notifyData.Action = HistoryAction.Remove;
  1899. notifyData.PageIndex = annotParam.PageIndex;
  1900. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1901. notifyData.AnnotType = annotParam.CurrentType;
  1902. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1903. notifyData.CurrentParam = annotParam;
  1904. annotHistory.CurrentParam = annotParam;
  1905. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1906. annotHistory.Action = HistoryAction.Remove;
  1907. PDFViewer.UndoManager.AddHistory(annotHistory);
  1908. cacheMoveWidget.GetAnnotData().Annot.RemoveAnnot();
  1909. cacheMoveWidget = null;
  1910. }
  1911. CleanSelectedRect();
  1912. }
  1913. if (notifyData != null)
  1914. {
  1915. AnnotChanged?.Invoke(this, notifyData);
  1916. }
  1917. }
  1918. private bool PasteWidgetData(ref AnnotHistory annotHistory, WidgetParm item)
  1919. {
  1920. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1921. Point point = rightPressPoint;
  1922. rightPressPoint = new Point(-1, -1);
  1923. Rect rect;
  1924. int index = -1;
  1925. if (point.Equals(new Point(-1, -1)))
  1926. {
  1927. index = item.PageIndex;
  1928. rect = new Rect(
  1929. (item.ClientRect.left + 25),
  1930. (item.ClientRect.top + 25),
  1931. item.ClientRect.width(),
  1932. item.ClientRect.height()
  1933. );
  1934. }
  1935. else
  1936. {
  1937. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1938. CRect cRect = item.ClientRect;
  1939. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1940. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1941. rect = new Rect(
  1942. (pdfPoint.X - cRect.width() / 2),
  1943. (pdfPoint.Y - cRect.height() / 2),
  1944. cRect.width(),
  1945. cRect.height()
  1946. );
  1947. }
  1948. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1949. if (cPDFPage == null)
  1950. return false;
  1951. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateWidget(item.WidgetType);
  1952. CreateDefaultWidget(cPDFAnnotation, item.WidgetType, item);
  1953. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1954. cPDFAnnotation.SetRect(setRect);
  1955. (cPDFAnnotation as CPDFWidget).UpdateFormAp();
  1956. AnnotParam annotParam = ParamConverter.WidgetConverter(cPDFDocument, cPDFAnnotation);
  1957. switch (item.WidgetType)
  1958. {
  1959. case C_WIDGET_TYPE.WIDGET_NONE:
  1960. break;
  1961. case C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1962. annotHistory = new PushButtonHistory();
  1963. (annotHistory as PushButtonHistory).CurrentParam = (PushButtonParam)annotParam;
  1964. annotHistory.PDFDoc = cPDFDocument;
  1965. break;
  1966. case C_WIDGET_TYPE.WIDGET_CHECKBOX:
  1967. annotHistory = new CheckBoxHistory();
  1968. (annotHistory as CheckBoxHistory).CurrentParam = (CheckBoxParam)annotParam;
  1969. annotHistory.PDFDoc = cPDFDocument;
  1970. break;
  1971. case C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  1972. annotHistory = new RadioButtonHistory();
  1973. (annotHistory as RadioButtonHistory).CurrentParam = (RadioButtonParam)annotParam;
  1974. annotHistory.PDFDoc = cPDFDocument;
  1975. break;
  1976. case C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  1977. annotHistory = new TextBoxHistory();
  1978. (annotHistory as TextBoxHistory).CurrentParam = (TextBoxParam)annotParam;
  1979. annotHistory.PDFDoc = cPDFDocument;
  1980. break;
  1981. case C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1982. annotHistory = new ComboBoxHistory();
  1983. (annotHistory as ComboBoxHistory).CurrentParam = (ComboBoxParam)annotParam;
  1984. annotHistory.PDFDoc = cPDFDocument;
  1985. break;
  1986. case C_WIDGET_TYPE.WIDGET_LISTBOX:
  1987. annotHistory = new ListBoxHistory();
  1988. (annotHistory as ListBoxHistory).CurrentParam = (ListBoxParam)annotParam;
  1989. annotHistory.PDFDoc = cPDFDocument;
  1990. break;
  1991. case C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  1992. annotHistory = new SignatureHistory();
  1993. (annotHistory as SignatureHistory).CurrentParam = (SignatureParam)annotParam;
  1994. annotHistory.PDFDoc = cPDFDocument;
  1995. break;
  1996. case C_WIDGET_TYPE.WIDGET_UNKNOWN:
  1997. break;
  1998. default:
  1999. break;
  2000. }
  2001. return true;
  2002. }
  2003. public void RemoveLastAnnotList()
  2004. {
  2005. if (lastAnnotList != null)
  2006. {
  2007. lastAnnotList.Clear();
  2008. }
  2009. }
  2010. public void SetCopyText(string copyText)
  2011. {
  2012. CopyText = copyText;
  2013. }
  2014. #endregion
  2015. }
  2016. }