CPDFViewerTool.Command.cs 91 KB

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