CPDFViewerTool.Command.cs 76 KB

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