CPDFViewerTool.Command.cs 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  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. if (checkItem.EditType == CPDFEditType.EditPath && checkItem.EditAreaCopied)
  268. {
  269. e.CanExecute = true;
  270. break;
  271. }
  272. }
  273. try
  274. {
  275. if (Clipboard.ContainsText())
  276. {
  277. if (!string.IsNullOrEmpty(Clipboard.GetText()))
  278. {
  279. e.CanExecute = true;
  280. }
  281. }
  282. if (Clipboard.ContainsImage())
  283. {
  284. if (Clipboard.GetImage() != null)
  285. {
  286. e.CanExecute = true;
  287. }
  288. }
  289. }
  290. catch (Exception ex)
  291. {
  292. }
  293. break;
  294. }
  295. }
  296. private void ExecutePDFEditCommand(RoutedUICommand uiCommand, out CPDFEditType editType)
  297. {
  298. editType = CPDFEditType.None;
  299. switch (uiCommand.Name)
  300. {
  301. case "Copy":
  302. CopyPDFEditData(out editType);
  303. break;
  304. case "Cut":
  305. CopyPDFEditData(out editType);
  306. DelPDFEditData(out editType);
  307. break;
  308. case "Delete":
  309. DelPDFEditData(out editType);
  310. break;
  311. case "Paste":
  312. SetEditCopyData();
  313. if (lastPDFEditArgsList.Count > 0)
  314. {
  315. PastePDFEditData(out editType);
  316. PDFViewer.UpdateRenderFrame();
  317. }
  318. break;
  319. case "PasteWithoutStyle":
  320. SetEditCopyData();
  321. if (lastPDFEditArgsList.Count > 0)
  322. {
  323. PastePDFEditData(out editType, false);
  324. PDFViewer.UpdateRenderFrame();
  325. }
  326. break;
  327. case "SelectAll":
  328. SelectAllPDFEditData(out editType);
  329. break;
  330. }
  331. }
  332. private void SetEditCopyData()
  333. {
  334. try
  335. {
  336. if (Clipboard.ContainsText())
  337. {
  338. string copyText = Clipboard.GetText();
  339. bool findCopy = false;
  340. if (lastPDFEditArgsList != null && lastPDFEditArgsList.Count > 0)
  341. {
  342. foreach (PDFEditCommandData checkItem in lastPDFEditArgsList)
  343. {
  344. if (checkItem.EditType == CPDFEditType.EditText && copyText == checkItem.TextContent)
  345. {
  346. findCopy = true;
  347. }
  348. }
  349. }
  350. if (findCopy == false)
  351. {
  352. lastPDFEditArgsList?.Clear();
  353. if (string.IsNullOrEmpty(copyText) == false)
  354. {
  355. PDFEditCommandData commandData = new PDFEditCommandData();
  356. commandData.EditType = CPDFEditType.EditText;
  357. commandData.TextContent = copyText;
  358. int PageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  359. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(PageIndex);
  360. Rect rect = render.PaintRect;
  361. Point centerPoint = new Point(
  362. rect.Width / PDFViewer.CurrentRenderFrame.ZoomFactor / 2,
  363. rect.Height / PDFViewer.CurrentRenderFrame.ZoomFactor / 2);
  364. commandData.PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(centerPoint.X, centerPoint.Y, 0, 0));
  365. lastPDFEditArgsList.Add(commandData);
  366. }
  367. }
  368. }
  369. else if (Clipboard.ContainsImage())
  370. {
  371. if (Clipboard.GetImage() != null)
  372. {
  373. BitmapSource bitmapSource = Clipboard.GetImage();
  374. PDFEditCommandData commandData = new PDFEditCommandData();
  375. commandData.EditType = CPDFEditType.EditImage;
  376. int PageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  377. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(PageIndex);
  378. Rect rect = render.PaintRect;
  379. Point centerPoint = new Point(
  380. rect.Width / PDFViewer.CurrentRenderFrame.ZoomFactor / 2,
  381. rect.Height / PDFViewer.CurrentRenderFrame.ZoomFactor / 2);
  382. commandData.PDFRect = DpiHelper.StandardRectToPDFRect(new Rect(centerPoint.X, centerPoint.Y, bitmapSource.PixelWidth, bitmapSource.PixelHeight));
  383. lastPDFEditArgsList.Clear();
  384. lastPDFEditArgsList.Add(commandData);
  385. }
  386. }
  387. }
  388. catch (Exception ex)
  389. {
  390. }
  391. }
  392. private void CopyPDFEditData(out CPDFEditType editType)
  393. {
  394. editType = CPDFEditType.None;
  395. if (CPDFEditPage.CopyPage != null)
  396. {
  397. CPDFEditPage.CopyPage.ReleaseCopyEditAreaList();
  398. }
  399. try
  400. {
  401. lastPDFEditArgsList.Clear();
  402. Clipboard.Clear();
  403. if (currentEditAreaObject != null)
  404. {
  405. PDFEditCommandData commandData = new PDFEditCommandData();
  406. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  407. {
  408. editType = CPDFEditType.EditText;
  409. CPDFEditTextArea editTextArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  410. commandData.TextContent = editTextArea.SelectText;
  411. if (selectAllCharsForLine || string.IsNullOrEmpty(commandData.TextContent))
  412. {
  413. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  414. commandData.EditType = CPDFEditType.EditText;
  415. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  416. if (commandData.EditAreaCopied)
  417. {
  418. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  419. CPDFCopyEditArea CopyArea = copyList[copyList.Count - 1];
  420. commandData.TextContent = CopyArea.GetCopyTextAreaContent();
  421. commandData.CopyArea = CopyArea;
  422. commandData.EditPage = editPage;
  423. }
  424. }
  425. try
  426. {
  427. Clipboard.Clear();
  428. Clipboard.SetText(commandData.TextContent);
  429. Clipboard.Flush();
  430. }
  431. catch (Exception)
  432. {
  433. }
  434. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  435. lastPDFEditArgsList.Add(commandData);
  436. }
  437. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  438. {
  439. editType = CPDFEditType.EditImage;
  440. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  441. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  442. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  443. commandData.EditType = CPDFEditType.EditImage;
  444. if (commandData.EditAreaCopied)
  445. {
  446. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  447. commandData.CopyArea = copyList[copyList.Count - 1];
  448. commandData.EditPage = editPage;
  449. lastPDFEditArgsList.Add(commandData);
  450. }
  451. }
  452. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditPath)
  453. {
  454. editType = CPDFEditType.EditPath;
  455. CPDFEditPage editPage = currentEditAreaObject.cPDFEditPage;
  456. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(currentEditAreaObject.cPDFEditArea.GetFrame());
  457. commandData.EditAreaCopied = editPage.CopyEditArea(currentEditAreaObject.cPDFEditArea);
  458. commandData.EditType = CPDFEditType.EditPath;
  459. if (commandData.EditAreaCopied)
  460. {
  461. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  462. commandData.CopyArea = copyList[copyList.Count - 1];
  463. commandData.EditPage = editPage;
  464. lastPDFEditArgsList.Add(commandData);
  465. }
  466. }
  467. }
  468. else
  469. {
  470. //Add multi status
  471. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  472. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  473. {
  474. foreach (SelectedRect selectedRect in multiSelectedRect.Children)
  475. {
  476. EditAreaObject editAreaObject = GetEditAreaObjectListForRect(selectedRect);
  477. if (editAreaObject != null)
  478. {
  479. PDFEditCommandData commandData = new PDFEditCommandData();
  480. if (editAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  481. {
  482. editType = CPDFEditType.EditText;
  483. CPDFEditTextArea editTextArea = editAreaObject.cPDFEditArea as CPDFEditTextArea;
  484. commandData.TextContent = editTextArea.SelectText;
  485. if (!string.IsNullOrEmpty(commandData.TextContent))
  486. {
  487. CPDFEditPage editPage = editAreaObject.cPDFEditPage;
  488. commandData.EditType = CPDFEditType.EditText;
  489. commandData.EditAreaCopied = editPage.CopyEditArea(editAreaObject.cPDFEditArea);
  490. if (commandData.EditAreaCopied)
  491. {
  492. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  493. CPDFCopyEditArea CopyArea = copyList[copyList.Count - 1];
  494. commandData.TextContent = CopyArea.GetCopyTextAreaContent();
  495. commandData.CopyArea = CopyArea;
  496. commandData.EditPage = editPage;
  497. }
  498. }
  499. try
  500. {
  501. Clipboard.Clear();
  502. Clipboard.SetText(commandData.TextContent);
  503. Clipboard.Flush();
  504. }
  505. catch (Exception)
  506. {
  507. }
  508. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(editAreaObject.cPDFEditArea.GetFrame());
  509. lastPDFEditArgsList.Add(commandData);
  510. }
  511. if (editAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  512. {
  513. editType = CPDFEditType.EditImage;
  514. CPDFEditPage editPage = editAreaObject.cPDFEditPage;
  515. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(editAreaObject.cPDFEditArea.GetFrame());
  516. commandData.EditAreaCopied = editPage.CopyEditArea(editAreaObject.cPDFEditArea);
  517. commandData.EditType = CPDFEditType.EditImage;
  518. if (commandData.EditAreaCopied)
  519. {
  520. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  521. commandData.CopyArea = copyList[copyList.Count - 1];
  522. commandData.EditPage = editPage;
  523. lastPDFEditArgsList.Add(commandData);
  524. }
  525. }
  526. if (editAreaObject.cPDFEditArea.Type == CPDFEditType.EditPath)
  527. {
  528. editType = CPDFEditType.EditPath;
  529. CPDFEditPage editPage = editAreaObject.cPDFEditPage;
  530. commandData.PDFRect = DataConversionForWPF.CRectConversionForRect(editAreaObject.cPDFEditArea.GetFrame());
  531. commandData.EditAreaCopied = editPage.CopyEditArea(editAreaObject.cPDFEditArea);
  532. commandData.EditType = CPDFEditType.EditPath;
  533. if (commandData.EditAreaCopied)
  534. {
  535. List<CPDFCopyEditArea> copyList = editPage.GetCopyEditAreaList();
  536. commandData.CopyArea = copyList[copyList.Count - 1];
  537. commandData.EditPage = editPage;
  538. lastPDFEditArgsList.Add(commandData);
  539. }
  540. }
  541. }
  542. }
  543. }
  544. }
  545. }
  546. catch
  547. {
  548. }
  549. }
  550. private void DelPDFEditData(out CPDFEditType editType)
  551. {
  552. editType = CPDFEditType.None;
  553. if (currentEditAreaObject != null)
  554. {
  555. GroupHistory groupHistory = new GroupHistory();
  556. PDFEditHistory editHistory = new PDFEditHistory();
  557. editHistory.EditPage = currentEditAreaObject.cPDFEditPage;
  558. editHistory.PageIndex = currentEditAreaObject.PageIndex;
  559. groupHistory.Histories.Add(editHistory);
  560. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditText)
  561. {
  562. editType = CPDFEditType.EditText;
  563. CPDFEditTextArea editTextArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  564. string selectContent = editTextArea.SelectText;
  565. if (!selectAllCharsForLine)
  566. {
  567. DeleteChars();
  568. }
  569. else
  570. {
  571. RemoveTextBlock();
  572. }
  573. }
  574. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditImage)
  575. {
  576. editType = CPDFEditType.EditImage;
  577. RemoveImageBlock();
  578. }
  579. if (currentEditAreaObject.cPDFEditArea.Type == CPDFEditType.EditPath)
  580. {
  581. editType = CPDFEditType.EditPath;
  582. RemovePathBlock();
  583. }
  584. if (PDFViewer != null && PDFViewer.UndoManager != null)
  585. {
  586. PDFViewer.UndoManager.AddHistory(groupHistory);
  587. //After removing the data, you need to get the data again.
  588. PDFViewer.UpdateRenderFrame();
  589. }
  590. }
  591. else
  592. {
  593. //Add multi status
  594. MultiSelectedRect multiSelectedRect = CommonHelper.FindVisualChild<MultiSelectedRect>(PDFViewer.GetViewForTag(MultiSelectedRectViewTag));
  595. if (multiSelectedRect != null && multiSelectedRect.Children.Count > 0)
  596. {
  597. List<EditAreaObject> editAreaObjectList = new List<EditAreaObject>();
  598. foreach (SelectedRect selectedRect in multiSelectedRect.Children)
  599. {
  600. EditAreaObject editAreaObject = GetEditAreaObjectListForRect(selectedRect);
  601. editAreaObjectList.Add(editAreaObject);
  602. }
  603. if (editAreaObjectList != null && editAreaObjectList.Count > 0)
  604. {
  605. editAreaObjectList = editAreaObjectList.OrderByDescending(x => x.EditAreaIndex).ToList();
  606. GroupHistory groupHistory = new GroupHistory();
  607. foreach (EditAreaObject editareaobject in editAreaObjectList)
  608. {
  609. PDFEditHistory editHistory = new PDFEditHistory();
  610. editHistory.EditPage = editareaobject.cPDFEditPage;
  611. editHistory.PageIndex = editareaobject.PageIndex;
  612. groupHistory.Histories.Add(editHistory);
  613. if (editareaobject.cPDFEditArea.Type == CPDFEditType.EditText)
  614. {
  615. editType = CPDFEditType.EditText;
  616. CPDFEditTextArea editTextArea = editareaobject.cPDFEditArea as CPDFEditTextArea;
  617. string selectContent = editTextArea.SelectText;
  618. RemoveTextBlock(editareaobject);
  619. }
  620. if (editareaobject.cPDFEditArea.Type == CPDFEditType.EditImage)
  621. {
  622. editType = CPDFEditType.EditImage;
  623. RemoveImageBlock(editareaobject);
  624. }
  625. if (editareaobject.cPDFEditArea.Type == CPDFEditType.EditPath)
  626. {
  627. editType = CPDFEditType.EditPath;
  628. RemovePathBlock(editareaobject);
  629. }
  630. }
  631. if (PDFViewer != null && PDFViewer.UndoManager != null)
  632. {
  633. PDFViewer.UndoManager.AddHistory(groupHistory);
  634. //After removing the data, you need to get the data again.
  635. PDFViewer.UpdateRenderFrame();
  636. }
  637. }
  638. CleanSelectedMultiRect();
  639. OpenSelectedMulti(false);
  640. }
  641. }
  642. }
  643. private void PastePDFEditData(out CPDFEditType editType, bool pasteMatchStyle = true)
  644. {
  645. editType = CPDFEditType.None;
  646. double left = 0;
  647. double right = 0;
  648. double top = 0;
  649. double bottom = 0;
  650. bool initial = false;
  651. #region Calculate the maximum rectangle
  652. foreach (PDFEditCommandData commandData in lastPDFEditArgsList)
  653. {
  654. if (initial == false)
  655. {
  656. left = commandData.PDFRect.Left;
  657. right = commandData.PDFRect.Right;
  658. top = commandData.PDFRect.Top;
  659. bottom = commandData.PDFRect.Bottom;
  660. initial = true;
  661. continue;
  662. }
  663. left = Math.Min(left, commandData.PDFRect.Left);
  664. right = Math.Max(right, commandData.PDFRect.Right);
  665. top = Math.Min(top, commandData.PDFRect.Top);
  666. bottom = Math.Max(bottom, commandData.PDFRect.Bottom);
  667. }
  668. left = DpiHelper.PDFNumToStandardNum(left);
  669. right = DpiHelper.PDFNumToStandardNum(right);
  670. top = DpiHelper.PDFNumToStandardNum(top);
  671. bottom = DpiHelper.PDFNumToStandardNum(bottom);
  672. int offsetX = 25;
  673. int offsetY = 25;
  674. Point hoverPoint = rightPressPoint;
  675. rightPressPoint = new Point(-1, -1);
  676. int pageIndex = PDFViewer.CurrentRenderFrame.PageIndex;
  677. // Judge whether the right-click coordinate is on the page.
  678. PDFViewer.GetePointToPage(hoverPoint, out RenderData renderData, out Point pagePoint);
  679. if (renderData != null)
  680. {
  681. //Calculate the offset from the upper left corner.
  682. offsetX = (int)(pagePoint.X / currentZoom - left);
  683. offsetY = (int)(pagePoint.Y / currentZoom - top);
  684. if (left + offsetX < 0)
  685. {
  686. offsetX = (int)-left;
  687. }
  688. if (right + offsetX > renderData.PageBound.Width / currentZoom)
  689. {
  690. offsetX = (int)(renderData.PageBound.Width / currentZoom - right);
  691. }
  692. if (top + offsetY < 0)
  693. {
  694. offsetY = (int)-top;
  695. }
  696. if (bottom + offsetY > renderData.PageBound.Height / currentZoom)
  697. {
  698. offsetY = (int)(renderData.PageBound.Height / currentZoom - bottom);
  699. }
  700. pageIndex = renderData.PageIndex;
  701. }
  702. else
  703. {
  704. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(pageIndex);
  705. //Max rectangle (standard DPI).
  706. Rect maxRect = new Rect((int)left + render.PaintRect.Left / currentZoom, (int)top + render.PaintRect.Top / currentZoom, (int)(right - left), (int)(bottom - top));
  707. //Center point of the visible range.
  708. Point centerPoint = new Point(
  709. render.PaintRect.Left / currentZoom + render.PaintRect.Width / currentZoom / 2,
  710. render.PaintRect.Top / currentZoom + render.PaintRect.Height / currentZoom / 2);
  711. //Visible range.
  712. Rect checkRect = new Rect(
  713. render.PaintRect.Left / currentZoom,
  714. render.PaintRect.Top / currentZoom,
  715. render.PaintRect.Width / currentZoom,
  716. render.PaintRect.Height / currentZoom);
  717. if (!checkRect.IntersectsWith(maxRect))
  718. {
  719. offsetX = (int)(left - centerPoint.X);
  720. offsetY = (int)(top - centerPoint.Y);
  721. }
  722. if (left + offsetX < 0)
  723. {
  724. offsetX = (int)-left;
  725. }
  726. if (right + offsetX > render.RenderRect.Width / currentZoom)
  727. {
  728. offsetX = (int)(render.RenderRect.Width / currentZoom / 2 - right);
  729. }
  730. if (top + offsetY < 0)
  731. {
  732. offsetY = (int)-top;
  733. }
  734. if (bottom + offsetY > render.RenderRect.Height / currentZoom)
  735. {
  736. offsetY = (int)(render.RenderRect.Height / currentZoom / 2 - bottom);
  737. }
  738. }
  739. #endregion
  740. foreach (PDFEditCommandData commandData in lastPDFEditArgsList)
  741. {
  742. GroupHistory groupHistory = new GroupHistory();
  743. CPDFPage docPage = PDFViewer.GetDocument().PageAtIndex(pageIndex);
  744. CPDFEditPage editPage = docPage.GetEditPage();
  745. RenderData render = PDFViewer.GetCurrentRenderPageForIndex(pageIndex);
  746. 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)));
  747. commandData.PDFRect = offsetRect;
  748. if (commandData.EditType == CPDFEditType.EditText)
  749. {
  750. editType = CPDFEditType.EditText;
  751. if (pasteMatchStyle && commandData.EditAreaCopied)
  752. {
  753. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  754. if (editPage.CanUndo())
  755. {
  756. PDFEditHistory editHistory = new PDFEditHistory();
  757. editHistory.EditPage = editPage;
  758. editHistory.PageIndex = pageIndex;
  759. groupHistory.Histories.Add(editHistory);
  760. CPDFViewer pdfViewer = GetCPDFViewer();
  761. if (pdfViewer != null && pdfViewer.UndoManager != null)
  762. {
  763. pdfViewer.UndoManager.AddHistory(groupHistory);
  764. }
  765. }
  766. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  767. }
  768. else
  769. {
  770. PDFEditHistory editHistory = new PDFEditHistory();
  771. if (!string.IsNullOrEmpty(commandData.TextContent))
  772. {
  773. EditAreaObject editAreaObject = GetHitTestAreaObject(hoverPoint);
  774. if (editAreaObject != null && editAreaObject.cPDFEditArea is CPDFEditTextArea)
  775. {
  776. CPDFEditTextArea TextArea = editAreaObject.cPDFEditArea as CPDFEditTextArea;
  777. if (TextArea.SelectLineRects.Count > 0)
  778. {
  779. TextArea.DeleteChars();
  780. TextArea.ClearSelectChars();
  781. PDFEditHistory deleteHistory = new PDFEditHistory();
  782. deleteHistory.EditPage = editPage;
  783. deleteHistory.PageIndex = pageIndex;
  784. groupHistory.Histories.Add(deleteHistory);
  785. }
  786. TextArea.InsertText(commandData.TextContent);
  787. editHistory.EditPage = editPage;
  788. editHistory.PageIndex = pageIndex;
  789. groupHistory.Histories.Add(editHistory);
  790. CPDFViewer pdfViewer = GetCPDFViewer();
  791. if (pdfViewer != null && pdfViewer.UndoManager != null)
  792. {
  793. pdfViewer.UndoManager.AddHistory(groupHistory);
  794. }
  795. }
  796. else
  797. {
  798. PDFEditHistory createHistory = new PDFEditHistory();
  799. CPDFEditTextArea editArea = editPage.CreateNewTextArea(DataConversionForWPF.RectConversionForCRect(offsetRect), "Helvetica", 14, new byte[3] { 0, 0, 0 });
  800. createHistory.EditPage = editPage;
  801. createHistory.PageIndex = pageIndex;
  802. groupHistory.Histories.Add(createHistory);
  803. if (editArea != null)
  804. {
  805. if (editArea.InsertText(commandData.TextContent))
  806. {
  807. editHistory.EditPage = editPage;
  808. editHistory.PageIndex = pageIndex;
  809. groupHistory.Histories.Add(editHistory);
  810. }
  811. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  812. }
  813. CPDFViewer pdfViewer = GetCPDFViewer();
  814. if (pdfViewer != null && pdfViewer.UndoManager != null)
  815. {
  816. pdfViewer.UndoManager.AddHistory(groupHistory);
  817. }
  818. }
  819. }
  820. }
  821. }
  822. if (commandData.EditType == CPDFEditType.EditImage)
  823. {
  824. editType = CPDFEditType.EditImage;
  825. PDFEditHistory editHistory = new PDFEditHistory();
  826. if (commandData.EditAreaCopied)
  827. {
  828. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  829. editHistory.EditPage = editPage;
  830. editHistory.PageIndex = pageIndex;
  831. CPDFViewer pdfViewer = GetCPDFViewer();
  832. if (pdfViewer != null && pdfViewer.UndoManager != null)
  833. {
  834. pdfViewer.UndoManager.AddHistory(editHistory);
  835. }
  836. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  837. }
  838. else
  839. {
  840. BitmapSource bitmapSource = BinaryStructConverter.ImageFromClipboardDib();
  841. if (bitmapSource != null)
  842. {
  843. PDFEditHistory createHistory = new PDFEditHistory();
  844. byte[] imageData = new byte[bitmapSource.PixelWidth * bitmapSource.PixelHeight * 4];
  845. if (bitmapSource.Format != PixelFormats.Bgra32)
  846. {
  847. FormatConvertedBitmap covert = new FormatConvertedBitmap(bitmapSource, PixelFormats.Bgra32, bitmapSource.Palette, 0);
  848. covert.CopyPixels(imageData, bitmapSource.PixelWidth * 4, 0);
  849. }
  850. else
  851. {
  852. bitmapSource.CopyPixels(imageData, bitmapSource.PixelWidth * 4, 0);
  853. }
  854. CPDFEditImageArea editArea = editPage.CreateNewImageArea(DataConversionForWPF.RectConversionForCRect(offsetRect), imageData, bitmapSource.PixelWidth, bitmapSource.PixelHeight);
  855. createHistory.EditPage = editPage;
  856. createHistory.PageIndex = pageIndex;
  857. groupHistory.Histories.Add(createHistory);
  858. if (editArea != null)
  859. {
  860. editHistory.EditPage = editPage;
  861. editHistory.PageIndex = pageIndex;
  862. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  863. }
  864. CPDFViewer pdfViewer = GetCPDFViewer();
  865. if (pdfViewer != null && pdfViewer.UndoManager != null)
  866. {
  867. pdfViewer.UndoManager.AddHistory(groupHistory);
  868. }
  869. }
  870. }
  871. }
  872. if (commandData.EditType == CPDFEditType.EditPath)
  873. {
  874. editType = CPDFEditType.EditPath;
  875. PDFEditHistory editHistory = new PDFEditHistory();
  876. if (commandData.EditAreaCopied)
  877. {
  878. commandData.CopyArea?.PasteEditArea(editPage, new CPoint((float)offsetRect.Left, (float)offsetRect.Top));
  879. editHistory.EditPage = editPage;
  880. editHistory.PageIndex = pageIndex;
  881. CPDFViewer pdfViewer = GetCPDFViewer();
  882. if (pdfViewer != null && pdfViewer.UndoManager != null)
  883. {
  884. pdfViewer.UndoManager.AddHistory(editHistory);
  885. }
  886. SelectedEditAreaForIndex(pageIndex, editPage.GetEditAreaList().Count - 1, false);
  887. }
  888. }
  889. editPage.EndEdit();
  890. }
  891. }
  892. private void SelectAllPDFEditData(out CPDFEditType editType)
  893. {
  894. editType = CPDFEditType.None;
  895. if (currentEditAreaObject != null && currentEditAreaObject.cPDFEditPage != null)
  896. {
  897. CPDFEditTextArea textArea = currentEditAreaObject.cPDFEditArea as CPDFEditTextArea;
  898. textArea.SelectAllChars();
  899. editType = CPDFEditType.EditText;
  900. PDFViewer.UpdateRenderFrame();
  901. }
  902. }
  903. private Rect AddPasteOffset(Rect clientRect, int offsetX, int offsetY, Size pageSize)
  904. {
  905. clientRect.X += offsetX;
  906. clientRect.Y += offsetY;
  907. if (clientRect.Left < 0)
  908. {
  909. clientRect.X = 0;
  910. }
  911. if (clientRect.Top < 0)
  912. {
  913. clientRect.Y = 0;
  914. }
  915. if (clientRect.Right > pageSize.Width)
  916. {
  917. clientRect.X = pageSize.Width - Math.Min(clientRect.Width, pageSize.Width);
  918. }
  919. if (clientRect.Bottom > pageSize.Height)
  920. {
  921. clientRect.Y = pageSize.Height - Math.Min(clientRect.Height, pageSize.Height);
  922. }
  923. return clientRect;
  924. }
  925. #endregion
  926. #region Annot
  927. private bool CheckCacheHitTestAnnot(string cmdName = "")
  928. {
  929. if (cacheHitTestAnnot == null)
  930. {
  931. return false;
  932. }
  933. AnnotData hitData = cacheHitTestAnnot.GetAnnotData();
  934. if (hitData == null)
  935. {
  936. return false;
  937. }
  938. if (hitData.Annot == null)
  939. {
  940. return false;
  941. }
  942. if (cmdName == "Delete" && hitData.Annot is CPDFSignatureWidget)
  943. {
  944. CPDFSignatureWidget signAnnot = hitData.Annot as CPDFSignatureWidget;
  945. if (signAnnot != null && signAnnot.IsSigned())
  946. {
  947. return false;
  948. }
  949. }
  950. return true;
  951. }
  952. private bool CheckAnnotCanDoCopyCut(bool isCut = false)
  953. {
  954. if (!CheckCacheHitTestAnnot(isCut ? "Delete" : ""))
  955. {
  956. return false;
  957. }
  958. if (cacheHitTestAnnot.GetAnnotData().Annot.GetIsLocked() && isCut)
  959. {
  960. return false;
  961. }
  962. switch (cacheHitTestAnnot.CurrentType)
  963. {
  964. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  965. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  966. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  967. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  968. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  969. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  970. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  971. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  972. return false;
  973. default:
  974. break;
  975. }
  976. return true;
  977. }
  978. private void CheckViewerCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  979. {
  980. switch (uiCommand.Name)
  981. {
  982. case "Copy":
  983. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  984. if (!e.CanExecute && textSelectInfo != null)
  985. {
  986. foreach (int key in textSelectInfo.PageSelectText.Keys)
  987. {
  988. if (textSelectInfo.PageSelectText[key] != string.Empty)
  989. {
  990. e.CanExecute = true;
  991. break;
  992. }
  993. }
  994. }
  995. break;
  996. default:
  997. return;
  998. }
  999. }
  1000. private void CheckAnnotCommandStatus(RoutedUICommand uiCommand, CanExecuteRoutedEventArgs e)
  1001. {
  1002. switch (uiCommand.Name)
  1003. {
  1004. case "Copy":
  1005. e.CanExecute = CheckAnnotCanDoCopyCut();
  1006. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  1007. if (!e.CanExecute && textSelectInfo != null)
  1008. {
  1009. foreach (int key in textSelectInfo.PageSelectText.Keys)
  1010. {
  1011. if (textSelectInfo.PageSelectText[key] != string.Empty)
  1012. {
  1013. e.CanExecute = true;
  1014. break;
  1015. }
  1016. }
  1017. }
  1018. break;
  1019. case "Cut":
  1020. e.CanExecute = CheckAnnotCanDoCopyCut(true);
  1021. break;
  1022. case "Delete":
  1023. e.CanExecute = CheckCacheHitTestAnnot("Delete");
  1024. break;
  1025. case "Paste":
  1026. if (lastAnnotList.Count > 0)
  1027. {
  1028. e.CanExecute = true;
  1029. }
  1030. break;
  1031. }
  1032. }
  1033. private void ExecuteViewerCommand(RoutedUICommand uiCommand)
  1034. {
  1035. switch (uiCommand.Name)
  1036. {
  1037. case "Copy":
  1038. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  1039. if (textSelectInfo != null)
  1040. {
  1041. StringBuilder copyTextBuilder = new StringBuilder();
  1042. foreach (int key in textSelectInfo.PageSelectText.Keys)
  1043. {
  1044. if (textSelectInfo.PageSelectText[key] != string.Empty)
  1045. {
  1046. copyTextBuilder.Append(textSelectInfo.PageSelectText[key]);
  1047. }
  1048. }
  1049. if (copyTextBuilder.Length > 0)
  1050. {
  1051. try
  1052. {
  1053. Clipboard.Clear();
  1054. Clipboard.SetText(copyTextBuilder.ToString());
  1055. }
  1056. catch (Exception)
  1057. {
  1058. }
  1059. }
  1060. }
  1061. break;
  1062. default:
  1063. return;
  1064. }
  1065. }
  1066. private void ExecuteAnnotCommand(RoutedUICommand uiCommand)
  1067. {
  1068. switch (uiCommand.Name)
  1069. {
  1070. case "Copy":
  1071. if (cacheHitTestAnnot != null)
  1072. {
  1073. lastAnnotList.Clear();
  1074. AnnotParam annotParam = ParamConverter.AnnotConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1075. lastAnnotList.Add(annotParam);
  1076. }
  1077. if (cacheMoveWidget != null)
  1078. {
  1079. lastAnnotList.Clear();
  1080. AnnotParam annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1081. lastAnnotList.Add(annotParam);
  1082. }
  1083. else
  1084. {
  1085. TextSelectInfo textSelectInfo = GetTextSelectInfo();
  1086. if (textSelectInfo != null)
  1087. {
  1088. StringBuilder copyTextBuilder = new StringBuilder();
  1089. foreach (int key in textSelectInfo.PageSelectText.Keys)
  1090. {
  1091. if (textSelectInfo.PageSelectText[key] != string.Empty)
  1092. {
  1093. copyTextBuilder.Append(textSelectInfo.PageSelectText[key]);
  1094. }
  1095. }
  1096. if (copyTextBuilder.Length > 0)
  1097. {
  1098. try
  1099. {
  1100. Clipboard.Clear();
  1101. Clipboard.SetText(copyTextBuilder.ToString());
  1102. }
  1103. catch (Exception)
  1104. {
  1105. }
  1106. }
  1107. }
  1108. }
  1109. break;
  1110. case "Cut":
  1111. if (cacheHitTestAnnot != null)
  1112. {
  1113. lastAnnotList.Clear();
  1114. AnnotParam annotParam;
  1115. if (cacheHitTestAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  1116. {
  1117. annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1118. }
  1119. else
  1120. {
  1121. annotParam = ParamConverter.AnnotConverter(GetCPDFViewer().GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1122. }
  1123. lastAnnotList.Add(annotParam);
  1124. }
  1125. if (cacheMoveWidget != null)
  1126. {
  1127. lastAnnotList.Clear();
  1128. AnnotParam annotParam = ParamConverter.WidgetConverter(GetCPDFViewer().GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1129. lastAnnotList.Add(annotParam);
  1130. }
  1131. DeleteAnnotData();
  1132. PDFViewer.UpdateAnnotFrame();
  1133. break;
  1134. case "Delete":
  1135. DeleteAnnotData();
  1136. PDFViewer.UpdateAnnotFrame();
  1137. break;
  1138. case "Paste":
  1139. PasteAnnotData();
  1140. PDFViewer.UpdateAnnotFrame();
  1141. break;
  1142. }
  1143. }
  1144. private void PasteAnnotData(bool pasteMatchStyle = false)
  1145. {
  1146. AnnotHistory annotHistory = null;
  1147. foreach (AnnotParam item in lastAnnotList)
  1148. {
  1149. if (item == null)
  1150. {
  1151. continue;
  1152. }
  1153. Point point = rightPressPoint;
  1154. rightPressPoint = new Point(-1, -1);
  1155. switch (item.CurrentType)
  1156. {
  1157. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  1158. {
  1159. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1160. Rect rect;
  1161. annotHistory = new StickyNoteAnnotHistory();
  1162. annotHistory.PDFDoc = cPDFDocument;
  1163. int index = -1;
  1164. if (point.Equals(new Point(-1, -1)))
  1165. {
  1166. index = item.PageIndex;
  1167. rect = new Rect(
  1168. (item.ClientRect.left + 25),
  1169. (item.ClientRect.top + 25),
  1170. item.ClientRect.width(),
  1171. item.ClientRect.height()
  1172. );
  1173. }
  1174. else
  1175. {
  1176. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1177. CRect cRect = item.ClientRect;
  1178. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1179. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1180. rect = new Rect(
  1181. (pdfPoint.X - cRect.width() / 2),
  1182. (pdfPoint.Y - cRect.height() / 2),
  1183. cRect.width(),
  1184. cRect.height()
  1185. );
  1186. }
  1187. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1188. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1189. if (cPDFPage == null)
  1190. return;
  1191. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1192. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1193. cPDFAnnotation.SetRect(setRect);
  1194. CommonHelper.UpdateStickyAP(cPDFAnnotation as CPDFTextAnnotation);
  1195. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1196. (annotHistory as StickyNoteAnnotHistory).CurrentParam = (StickyNoteParam)annotParam;
  1197. }
  1198. break;
  1199. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  1200. {
  1201. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1202. Rect rect;
  1203. annotHistory = new LinkAnnotHistory();
  1204. annotHistory.PDFDoc = cPDFDocument;
  1205. int index = -1;
  1206. if (point.Equals(new Point(-1, -1)))
  1207. {
  1208. index = item.PageIndex;
  1209. rect = new Rect(
  1210. (item.ClientRect.left + 25),
  1211. (item.ClientRect.top + 25),
  1212. item.ClientRect.width(),
  1213. item.ClientRect.height()
  1214. );
  1215. }
  1216. else
  1217. {
  1218. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1219. CRect cRect = item.ClientRect;
  1220. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1221. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1222. rect = new Rect(
  1223. (pdfPoint.X - cRect.width() / 2),
  1224. (pdfPoint.Y - cRect.height() / 2),
  1225. cRect.width(),
  1226. cRect.height()
  1227. );
  1228. }
  1229. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1230. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1231. if (cPDFPage == null)
  1232. return;
  1233. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1234. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1235. cPDFAnnotation.SetRect(setRect);
  1236. cPDFAnnotation.UpdateAp();
  1237. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1238. (annotHistory as LinkAnnotHistory).CurrentParam = (LinkParam)annotParam;
  1239. }
  1240. break;
  1241. case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
  1242. {
  1243. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1244. Rect rect;
  1245. annotHistory = new FreeTextAnnotHistory();
  1246. annotHistory.PDFDoc = cPDFDocument;
  1247. int index = -1;
  1248. if (point.Equals(new Point(-1, -1)))
  1249. {
  1250. index = item.PageIndex;
  1251. rect = new Rect(
  1252. (item.ClientRect.left + 25),
  1253. (item.ClientRect.top + 25),
  1254. item.ClientRect.width(),
  1255. item.ClientRect.height()
  1256. );
  1257. }
  1258. else
  1259. {
  1260. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1261. CRect cRect = item.ClientRect;
  1262. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1263. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1264. rect = new Rect(
  1265. (pdfPoint.X - cRect.width() / 2),
  1266. (pdfPoint.Y - cRect.height() / 2),
  1267. cRect.width(),
  1268. cRect.height()
  1269. );
  1270. }
  1271. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1272. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1273. if (cPDFPage == null)
  1274. return;
  1275. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1276. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1277. cPDFAnnotation.SetRect(setRect);
  1278. cPDFAnnotation.UpdateAp();
  1279. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1280. (annotHistory as FreeTextAnnotHistory).CurrentParam = (FreeTextParam)annotParam;
  1281. }
  1282. break;
  1283. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  1284. {
  1285. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1286. Rect rect;
  1287. annotHistory = new LineAnnotHistory();
  1288. annotHistory.PDFDoc = cPDFDocument;
  1289. int index = -1;
  1290. if (point.Equals(new Point(-1, -1)))
  1291. {
  1292. index = item.PageIndex;
  1293. rect = new Rect(
  1294. (item.ClientRect.left + 25),
  1295. (item.ClientRect.top + 25),
  1296. item.ClientRect.width(),
  1297. item.ClientRect.height()
  1298. );
  1299. }
  1300. else
  1301. {
  1302. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1303. CRect cRect = item.ClientRect;
  1304. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1305. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1306. rect = new Rect(
  1307. (pdfPoint.X - cRect.width() / 2),
  1308. (pdfPoint.Y - cRect.height() / 2),
  1309. cRect.width(),
  1310. cRect.height()
  1311. );
  1312. }
  1313. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1314. LineParam newLineParam = new LineParam();
  1315. item.CopyTo(newLineParam);
  1316. {
  1317. float offsetX = setRect.left - item.ClientRect.left;
  1318. float offsetY = setRect.top - item.ClientRect.top;
  1319. newLineParam.HeadPoint = new CPoint(newLineParam.HeadPoint.x + offsetX, newLineParam.HeadPoint.y + offsetY);
  1320. newLineParam.TailPoint = new CPoint(newLineParam.TailPoint.x + offsetX, newLineParam.TailPoint.y + offsetY);
  1321. }
  1322. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1323. if (cPDFPage == null)
  1324. return;
  1325. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1326. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1327. (cPDFAnnotation as CPDFLineAnnotation)?.SetLinePoints(newLineParam.HeadPoint, newLineParam.TailPoint);
  1328. cPDFAnnotation.SetRect(setRect);
  1329. cPDFAnnotation.UpdateAp();
  1330. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1331. (annotHistory as LineAnnotHistory).CurrentParam = (LineParam)annotParam;
  1332. }
  1333. break;
  1334. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
  1335. {
  1336. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1337. Rect rect;
  1338. annotHistory = new SquareAnnotHistory();
  1339. annotHistory.PDFDoc = cPDFDocument;
  1340. int index = -1;
  1341. if (point.Equals(new Point(-1, -1)))
  1342. {
  1343. index = item.PageIndex;
  1344. rect = new Rect(
  1345. (item.ClientRect.left + 25),
  1346. (item.ClientRect.top + 25),
  1347. item.ClientRect.width(),
  1348. item.ClientRect.height()
  1349. );
  1350. }
  1351. else
  1352. {
  1353. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1354. CRect cRect = item.ClientRect;
  1355. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1356. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1357. rect = new Rect(
  1358. (pdfPoint.X - cRect.width() / 2),
  1359. (pdfPoint.Y - cRect.height() / 2),
  1360. cRect.width(),
  1361. cRect.height()
  1362. );
  1363. }
  1364. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1365. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1366. if (cPDFPage == null)
  1367. return;
  1368. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1369. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1370. cPDFAnnotation.SetRect(setRect);
  1371. CPDFTextPage textPage = cPDFPage.GetTextPage();
  1372. if (textPage != null)
  1373. {
  1374. string newContent = textPage.GetBoundedText(setRect);
  1375. cPDFAnnotation.SetContent(newContent);
  1376. }
  1377. cPDFAnnotation.UpdateAp();
  1378. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1379. (annotHistory as SquareAnnotHistory).CurrentParam = (SquareParam)annotParam;
  1380. }
  1381. break;
  1382. case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
  1383. {
  1384. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1385. Rect rect;
  1386. annotHistory = new CircleAnnotHistory();
  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. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1414. if (cPDFPage == null)
  1415. return;
  1416. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1417. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1418. cPDFAnnotation.SetRect(setRect);
  1419. CPDFTextPage textPage = cPDFPage.GetTextPage();
  1420. if (textPage != null)
  1421. {
  1422. string newContent = textPage.GetBoundedText(setRect);
  1423. cPDFAnnotation.SetContent(newContent);
  1424. }
  1425. cPDFAnnotation.UpdateAp();
  1426. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1427. (annotHistory as CircleAnnotHistory).CurrentParam = (CircleParam)annotParam;
  1428. }
  1429. break;
  1430. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  1431. {
  1432. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1433. Rect rect;
  1434. annotHistory = new PolygonMeasureAnnotHistory();
  1435. annotHistory.PDFDoc = cPDFDocument;
  1436. int index = -1;
  1437. if (point.Equals(new Point(-1, -1)))
  1438. {
  1439. index = item.PageIndex;
  1440. rect = new Rect(
  1441. (item.ClientRect.left + 25),
  1442. (item.ClientRect.top + 25),
  1443. item.ClientRect.width(),
  1444. item.ClientRect.height()
  1445. );
  1446. }
  1447. else
  1448. {
  1449. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1450. CRect cRect = item.ClientRect;
  1451. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1452. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1453. rect = new Rect(
  1454. (pdfPoint.X - cRect.width() / 2),
  1455. (pdfPoint.Y - cRect.height() / 2),
  1456. cRect.width(),
  1457. cRect.height()
  1458. );
  1459. }
  1460. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1461. PolygonMeasureParam newPolygonParam = new PolygonMeasureParam();
  1462. item.CopyTo(newPolygonParam);
  1463. if(newPolygonParam.SavePoints != null && newPolygonParam.SavePoints.Count>0)
  1464. {
  1465. float offsetX = setRect.left - item.ClientRect.left;
  1466. float offsetY = setRect.top - item.ClientRect.top;
  1467. List<CPoint> arrangeList = new List<CPoint>();
  1468. foreach (CPoint addPoint in newPolygonParam.SavePoints)
  1469. {
  1470. arrangeList.Add(new CPoint(addPoint.x + offsetX, addPoint.y + offsetY));
  1471. }
  1472. newPolygonParam.SavePoints = arrangeList;
  1473. newPolygonParam.ClientRect = setRect;
  1474. }
  1475. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1476. if (cPDFPage == null)
  1477. return;
  1478. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(newPolygonParam.CurrentType);
  1479. CreateDefaultAnnot(cPDFAnnotation, newPolygonParam.CurrentType, newPolygonParam);
  1480. cPDFAnnotation.UpdateAp();
  1481. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1482. (annotHistory as PolygonMeasureAnnotHistory).CurrentParam = (PolygonMeasureParam)annotParam;
  1483. }
  1484. break;
  1485. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  1486. break;
  1487. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  1488. {
  1489. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1490. Rect rect;
  1491. annotHistory = new StampAnnotHistory();
  1492. annotHistory.PDFDoc = cPDFDocument;
  1493. int index = -1;
  1494. if (point.Equals(new Point(-1, -1)))
  1495. {
  1496. index = item.PageIndex;
  1497. rect = new Rect(
  1498. (item.ClientRect.left + 25),
  1499. (item.ClientRect.top + 25),
  1500. item.ClientRect.width(),
  1501. item.ClientRect.height()
  1502. );
  1503. }
  1504. else
  1505. {
  1506. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1507. CRect cRect = (item as StampParam).SourceRect;
  1508. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1509. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1510. rect = new Rect(
  1511. (pdfPoint.X - cRect.width() / 2),
  1512. (pdfPoint.Y - cRect.height() / 2),
  1513. cRect.width(),
  1514. cRect.height()
  1515. );
  1516. }
  1517. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1518. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1519. if (cPDFPage == null)
  1520. return;
  1521. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(item.CurrentType);
  1522. (cPDFAnnotation as CPDFStampAnnotation).SetSourceRect(setRect);
  1523. //cPDFAnnotation.SetRect(setRect);
  1524. CreateDefaultAnnot(cPDFAnnotation, item.CurrentType, item);
  1525. cPDFAnnotation.UpdateAp();
  1526. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1527. (annotHistory as StampAnnotHistory).CurrentParam = (StampParam)annotParam;
  1528. }
  1529. break;
  1530. case C_ANNOTATION_TYPE.C_ANNOTATION_CARET:
  1531. break;
  1532. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  1533. {
  1534. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1535. Rect rect;
  1536. annotHistory = new InkAnnotHistory();
  1537. annotHistory.PDFDoc = cPDFDocument;
  1538. int index = -1;
  1539. if (point.Equals(new Point(-1, -1)))
  1540. {
  1541. index = item.PageIndex;
  1542. rect = new Rect(
  1543. (item.ClientRect.left + 25),
  1544. (item.ClientRect.top + 25),
  1545. item.ClientRect.width(),
  1546. item.ClientRect.height()
  1547. );
  1548. }
  1549. else
  1550. {
  1551. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1552. CRect cRect = item.ClientRect;
  1553. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1554. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1555. rect = new Rect(
  1556. (pdfPoint.X - cRect.width() / 2),
  1557. (pdfPoint.Y - cRect.height() / 2),
  1558. cRect.width(),
  1559. cRect.height()
  1560. );
  1561. }
  1562. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1563. InkParam newInkParam = new InkParam();
  1564. item.CopyTo(newInkParam);
  1565. if (newInkParam.InkPath != null && newInkParam.InkPath.Count > 0)
  1566. {
  1567. float offsetX = setRect.left - item.ClientRect.left;
  1568. float offsetY = setRect.top - item.ClientRect.top;
  1569. List<List<CPoint>> arrangeList = new List<List<CPoint>>();
  1570. foreach (List<CPoint> inkNode in newInkParam.InkPath)
  1571. {
  1572. List<CPoint> inkPath = new List<CPoint>();
  1573. arrangeList.Add(inkPath);
  1574. foreach (CPoint addPoint in inkNode)
  1575. {
  1576. inkPath.Add(new CPoint(addPoint.x + offsetX, addPoint.y + offsetY));
  1577. }
  1578. }
  1579. newInkParam.InkPath = arrangeList;
  1580. newInkParam.ClientRect = setRect;
  1581. }
  1582. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1583. if (cPDFPage == null)
  1584. return;
  1585. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateAnnot(newInkParam.CurrentType);
  1586. CreateDefaultAnnot(cPDFAnnotation, newInkParam.CurrentType, newInkParam);
  1587. cPDFAnnotation.UpdateAp();
  1588. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, cPDFAnnotation);
  1589. (annotHistory as InkAnnotHistory).CurrentParam = (InkParam)annotParam;
  1590. }
  1591. break;
  1592. case C_ANNOTATION_TYPE.C_ANNOTATION_POPUP:
  1593. break;
  1594. case C_ANNOTATION_TYPE.C_ANNOTATION_FILEATTACHMENT:
  1595. break;
  1596. case C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET:
  1597. {
  1598. rightPressPoint = point;
  1599. bool result = PasteWidgetData(ref annotHistory, item as WidgetParm);
  1600. if (!result)
  1601. return;
  1602. }
  1603. break;
  1604. case C_ANNOTATION_TYPE.C_ANNOTATION_SCREEN:
  1605. break;
  1606. case C_ANNOTATION_TYPE.C_ANNOTATION_PRINTERMARK:
  1607. break;
  1608. case C_ANNOTATION_TYPE.C_ANNOTATION_TRAPNET:
  1609. break;
  1610. case C_ANNOTATION_TYPE.C_ANNOTATION_WATERMARK:
  1611. break;
  1612. case C_ANNOTATION_TYPE.C_ANNOTATION_3D:
  1613. break;
  1614. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  1615. break;
  1616. case C_ANNOTATION_TYPE.C_ANNOTATION_INTERCHANGE:
  1617. break;
  1618. default:
  1619. break;
  1620. }
  1621. PasteParam = annotHistory.CurrentParam;
  1622. PDFViewer.UndoManager.AddHistory(annotHistory);
  1623. }
  1624. }
  1625. private void DeleteAnnotData()
  1626. {
  1627. dynamic notifyData = null;
  1628. if (cacheHitTestAnnot != null)
  1629. {
  1630. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheHitTestAnnot.GetAnnotData().Annot);
  1631. if (annotHistory == null)
  1632. {
  1633. return;
  1634. }
  1635. AnnotParam annotParam = null;
  1636. if (cacheHitTestAnnot is BaseWidget)
  1637. {
  1638. annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1639. }
  1640. else
  1641. {
  1642. annotParam = ParamConverter.AnnotConverter(PDFViewer.GetDocument(), cacheHitTestAnnot.GetAnnotData().Annot);
  1643. }
  1644. if (annotParam != null)
  1645. {
  1646. notifyData = new ExpandoObject();
  1647. notifyData.Action = HistoryAction.Remove;
  1648. notifyData.PageIndex = annotParam.PageIndex;
  1649. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1650. notifyData.AnnotType = annotParam.CurrentType;
  1651. notifyData.CurrentParam = annotParam;
  1652. if (annotParam is WidgetParm)
  1653. {
  1654. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1655. }
  1656. annotHistory.CurrentParam = annotParam;
  1657. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1658. annotHistory.Action = HistoryAction.Remove;
  1659. PDFViewer.UndoManager.AddHistory(annotHistory);
  1660. List<C_ANNOTATION_TYPE> checkEditType = new List<C_ANNOTATION_TYPE>()
  1661. {
  1662. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  1663. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  1664. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON
  1665. };
  1666. if (checkEditType.Contains(cacheHitTestAnnot.CurrentType))
  1667. {
  1668. //Need to clear the measurement selected edit box.
  1669. CleanEditAnnot();
  1670. }
  1671. cacheHitTestAnnot.RemoveClean(true);
  1672. cacheHitTestAnnot.GetAnnotData().Annot.RemoveAnnot();
  1673. cacheHitTestAnnot = null;
  1674. }
  1675. CleanSelectedRect();
  1676. }
  1677. if (cacheMoveWidget != null)
  1678. {
  1679. AnnotHistory annotHistory = ParamConverter.CreateHistory(cacheMoveWidget.GetAnnotData().Annot);
  1680. if (annotHistory == null)
  1681. {
  1682. return;
  1683. }
  1684. AnnotParam annotParam = ParamConverter.WidgetConverter(PDFViewer.GetDocument(), cacheMoveWidget.GetAnnotData().Annot);
  1685. if (annotParam != null)
  1686. {
  1687. notifyData = new ExpandoObject();
  1688. notifyData.Action = HistoryAction.Remove;
  1689. notifyData.PageIndex = annotParam.PageIndex;
  1690. notifyData.AnnotIndex = annotParam.AnnotIndex;
  1691. notifyData.AnnotType = annotParam.CurrentType;
  1692. notifyData.WidgetType = (annotParam as WidgetParm).WidgetType;
  1693. notifyData.CurrentParam = annotParam;
  1694. annotHistory.CurrentParam = annotParam;
  1695. annotHistory.PDFDoc = PDFViewer.GetDocument();
  1696. annotHistory.Action = HistoryAction.Remove;
  1697. PDFViewer.UndoManager.AddHistory(annotHistory);
  1698. cacheMoveWidget.GetAnnotData().Annot.RemoveAnnot();
  1699. cacheMoveWidget = null;
  1700. }
  1701. CleanSelectedRect();
  1702. }
  1703. if (notifyData != null)
  1704. {
  1705. AnnotChanged?.Invoke(this, notifyData);
  1706. }
  1707. }
  1708. private bool PasteWidgetData(ref AnnotHistory annotHistory, WidgetParm item)
  1709. {
  1710. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  1711. Point point = rightPressPoint;
  1712. rightPressPoint = new Point(-1, -1);
  1713. Rect rect;
  1714. int index = -1;
  1715. if (point.Equals(new Point(-1, -1)))
  1716. {
  1717. index = item.PageIndex;
  1718. rect = new Rect(
  1719. (item.ClientRect.left + 25),
  1720. (item.ClientRect.top + 25),
  1721. item.ClientRect.width(),
  1722. item.ClientRect.height()
  1723. );
  1724. }
  1725. else
  1726. {
  1727. PDFViewer.GetPointPageInfo(point, out index, out Rect paintRect, out var pageBound);
  1728. CRect cRect = item.ClientRect;
  1729. Point zoomPoint = new Point((point.X - pageBound.X) / currentZoom, (point.Y - pageBound.Y) / currentZoom);
  1730. Point pdfPoint = DpiHelper.StandardPointToPDFPoint(zoomPoint);
  1731. rect = new Rect(
  1732. (pdfPoint.X - cRect.width() / 2),
  1733. (pdfPoint.Y - cRect.height() / 2),
  1734. cRect.width(),
  1735. cRect.height()
  1736. );
  1737. }
  1738. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  1739. if (cPDFPage == null)
  1740. return false;
  1741. CPDFAnnotation cPDFAnnotation = cPDFPage.CreateWidget(item.WidgetType);
  1742. CreateDefaultWidget(cPDFAnnotation, item.WidgetType, item);
  1743. CRect setRect = DataConversionForWPF.RectConversionForCRect(rect);
  1744. cPDFAnnotation.SetRect(setRect);
  1745. (cPDFAnnotation as CPDFWidget).UpdateFormAp();
  1746. AnnotParam annotParam = ParamConverter.WidgetConverter(cPDFDocument, cPDFAnnotation);
  1747. switch (item.WidgetType)
  1748. {
  1749. case C_WIDGET_TYPE.WIDGET_NONE:
  1750. break;
  1751. case C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  1752. annotHistory = new PushButtonHistory();
  1753. (annotHistory as PushButtonHistory).CurrentParam = (PushButtonParam)annotParam;
  1754. annotHistory.PDFDoc = cPDFDocument;
  1755. break;
  1756. case C_WIDGET_TYPE.WIDGET_CHECKBOX:
  1757. annotHistory = new CheckBoxHistory();
  1758. (annotHistory as CheckBoxHistory).CurrentParam = (CheckBoxParam)annotParam;
  1759. annotHistory.PDFDoc = cPDFDocument;
  1760. break;
  1761. case C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  1762. annotHistory = new RadioButtonHistory();
  1763. (annotHistory as RadioButtonHistory).CurrentParam = (RadioButtonParam)annotParam;
  1764. annotHistory.PDFDoc = cPDFDocument;
  1765. break;
  1766. case C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  1767. annotHistory = new TextBoxHistory();
  1768. (annotHistory as TextBoxHistory).CurrentParam = (TextBoxParam)annotParam;
  1769. annotHistory.PDFDoc = cPDFDocument;
  1770. break;
  1771. case C_WIDGET_TYPE.WIDGET_COMBOBOX:
  1772. annotHistory = new ComboBoxHistory();
  1773. (annotHistory as ComboBoxHistory).CurrentParam = (ComboBoxParam)annotParam;
  1774. annotHistory.PDFDoc = cPDFDocument;
  1775. break;
  1776. case C_WIDGET_TYPE.WIDGET_LISTBOX:
  1777. annotHistory = new ListBoxHistory();
  1778. (annotHistory as ListBoxHistory).CurrentParam = (ListBoxParam)annotParam;
  1779. annotHistory.PDFDoc = cPDFDocument;
  1780. break;
  1781. case C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
  1782. annotHistory = new SignatureHistory();
  1783. (annotHistory as SignatureHistory).CurrentParam = (SignatureParam)annotParam;
  1784. annotHistory.PDFDoc = cPDFDocument;
  1785. break;
  1786. case C_WIDGET_TYPE.WIDGET_UNKNOWN:
  1787. break;
  1788. default:
  1789. break;
  1790. }
  1791. return true;
  1792. }
  1793. #endregion
  1794. }
  1795. }