CPDFViewerTool.Command.cs 83 KB

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