CPDFViewerTool.WidgetTool.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFDocument.Action;
  5. using ComPDFKit.PDFPage;
  6. using ComPDFKit.Tool.DrawTool;
  7. using ComPDFKit.Viewer.Layer;
  8. using ComPDFKitViewer;
  9. using ComPDFKitViewer.BaseObject;
  10. using ComPDFKitViewer.Widget;
  11. using ComPDFKitViewer.Layer;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Controls.Primitives;
  21. using System.Windows.Documents;
  22. using System.Windows.Media;
  23. using System.Xml.Linq;
  24. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  25. using ComPDFKit.Tool.UndoManger;
  26. using ComPDFKit.Tool.Help;
  27. using ComPDFKitViewer.Helper;
  28. namespace ComPDFKit.Tool
  29. {
  30. public class WidgetClickArgs: EventArgs
  31. {
  32. public bool Handled { get; set; }
  33. public BaseWidget Widget { get; set; }
  34. public UIElement UI { get; set; }
  35. }
  36. public partial class CPDFViewerTool
  37. {
  38. public static DependencyProperty PopupAttachDataProperty = DependencyProperty.Register("PopupAttachData", typeof(BaseAnnot), typeof(CPDFViewerTool));
  39. public event EventHandler<WidgetClickArgs> WidgetActionHandler;
  40. public event EventHandler<CPDFAnnotation> WidgetCreatedHandler;
  41. private CustomizeLayer formPopLayer=null;
  42. // Inner default pop-up control
  43. private bool isInternalPopup;
  44. public bool IsAnnotReadOnly(BaseAnnot checkAnnot)
  45. {
  46. AnnotData annotData = checkAnnot?.GetAnnotData();
  47. if (annotData != null && annotData.Annot?.IsValid()==true)
  48. {
  49. return annotData.Annot.GetIsReadOnly();
  50. }
  51. return false;
  52. }
  53. public bool ShowFormHitPop(BaseWidget hitForm)
  54. {
  55. List<C_WIDGET_TYPE> formTypeList = new List<C_WIDGET_TYPE>()
  56. {
  57. C_WIDGET_TYPE.WIDGET_TEXTFIELD,
  58. C_WIDGET_TYPE.WIDGET_LISTBOX,
  59. C_WIDGET_TYPE.WIDGET_COMBOBOX
  60. };
  61. if(hitForm != null && formTypeList.Contains(hitForm.GetFormType()))
  62. {
  63. UIElement newUI = null;
  64. if (IsAnnotReadOnly(hitForm))
  65. {
  66. return false;
  67. }
  68. switch(hitForm.GetFormType())
  69. {
  70. case C_WIDGET_TYPE.WIDGET_TEXTFIELD:
  71. newUI=BuildPopTextUI(hitForm);
  72. break;
  73. case C_WIDGET_TYPE.WIDGET_LISTBOX:
  74. newUI=BuildPopListBoxUI(hitForm);
  75. break;
  76. case C_WIDGET_TYPE.WIDGET_COMBOBOX:
  77. newUI=BuildPopComboBoxUI(hitForm);
  78. break;
  79. default:
  80. break;
  81. }
  82. if (newUI!=null)
  83. {
  84. ShowFormHitPop(newUI,hitForm);
  85. isInternalPopup = true;
  86. }
  87. }
  88. return false;
  89. }
  90. public bool ShowFormHitPop(UIElement customui,BaseWidget hitForm)
  91. {
  92. if (customui!= null && hitForm!=null)
  93. {
  94. isInternalPopup = false;
  95. customui.SetValue(PopupAttachDataProperty, hitForm);
  96. HideWidgetHitPop();
  97. if(formPopLayer==null)
  98. {
  99. formPopLayer= new CustomizeLayer();
  100. }
  101. int selectedRectViewIndex = PDFViewer.GetMaxViewIndex();
  102. formPopLayer.Children.Clear();
  103. formPopLayer.Children.Add(customui);
  104. formPopLayer.Arrange();
  105. PDFViewer.InsertView(selectedRectViewIndex, formPopLayer);
  106. }
  107. return false;
  108. }
  109. /// <summary>
  110. /// Remove Form pop-up control
  111. /// </summary>
  112. public void HideWidgetHitPop()
  113. {
  114. PDFViewer?.RemoveView(formPopLayer);
  115. }
  116. private string GetFontName(string pdfFontName)
  117. {
  118. string fontName;
  119. switch (GetFontType(pdfFontName))
  120. {
  121. case FontType.Courier:
  122. fontName = "Courier New";
  123. break;
  124. case FontType.Helvetica:
  125. fontName = "Arial";
  126. break;
  127. case FontType.Times_Roman:
  128. fontName = "Times New Roman";
  129. break;
  130. default:
  131. fontName = "Arial";
  132. break;
  133. }
  134. return fontName;
  135. }
  136. private void SetFormRotateTransform(FrameworkElement formui,AnnotData annotData)
  137. {
  138. RotateTransform rotateTrans = new RotateTransform();
  139. rotateTrans.Angle = -90 * annotData.Rotation;
  140. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  141. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  142. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  143. formui.Width = rotateRect.Width;
  144. formui.Height = rotateRect.Height;
  145. formui.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  146. formui.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  147. rotateTrans.Angle = 90 * annotData.Rotation;
  148. rotateTrans.CenterX = rotateRect.Width / 2;
  149. rotateTrans.CenterY = rotateRect.Height / 2;
  150. formui.RenderTransform = rotateTrans;
  151. }
  152. protected UIElement BuildPopTextUI(BaseWidget textForm)
  153. {
  154. try
  155. {
  156. if (textForm != null && textForm.GetFormType() == C_WIDGET_TYPE.WIDGET_TEXTFIELD)
  157. {
  158. AnnotData annotData = textForm.GetAnnotData();
  159. CPDFTextWidget textWidget = annotData.Annot as CPDFTextWidget;
  160. if (textWidget == null)
  161. {
  162. return null;
  163. }
  164. TextBox textui = new TextBox();
  165. CTextAttribute textAttribute = textWidget.GetTextAttribute();
  166. byte transparency = textWidget.GetTransparency();
  167. textui.FontSize = textAttribute.FontSize* annotData.CurrentZoom/72D*96D;
  168. Color textColor = Color.FromArgb(
  169. transparency,
  170. textAttribute.FontColor[0],
  171. textAttribute.FontColor[1],
  172. textAttribute.FontColor[2]);
  173. Color borderColor = Colors.Transparent;
  174. Color backgroundColor = Colors.White;
  175. byte[] colorArray = new byte[3];
  176. if (textWidget.GetWidgetBorderRGBColor(ref colorArray))
  177. {
  178. borderColor = Color.FromRgb(colorArray[0], colorArray[1], colorArray[2]);
  179. }
  180. if (textWidget.GetWidgetBgRGBColor(ref colorArray))
  181. {
  182. backgroundColor = Color.FromRgb(colorArray[0], colorArray[1], colorArray[2]);
  183. }
  184. textui.Foreground = new SolidColorBrush(textColor);
  185. textui.BorderBrush = new SolidColorBrush(borderColor);
  186. textui.Background = new SolidColorBrush(backgroundColor);
  187. textui.BorderThickness = new Thickness(textWidget.GetBorderWidth()*annotData.CurrentZoom);
  188. textui.Text = textWidget.Text;
  189. textui.FontFamily = new FontFamily(GetFontName(textAttribute.FontName));
  190. textui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
  191. textui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
  192. if (textWidget.IsMultiLine)
  193. {
  194. textui.AcceptsReturn = true;
  195. textui.TextWrapping = TextWrapping.Wrap;
  196. }
  197. else
  198. {
  199. textui.VerticalContentAlignment = VerticalAlignment.Center;
  200. }
  201. switch (textWidget.Alignment)
  202. {
  203. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  204. textui.TextAlignment = TextAlignment.Left;
  205. break;
  206. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  207. textui.TextAlignment = TextAlignment.Right;
  208. break;
  209. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  210. textui.TextAlignment = TextAlignment.Center;
  211. break;
  212. default:
  213. break;
  214. }
  215. SetFormRotateTransform(textui, annotData);
  216. textui.Loaded += (object sender, RoutedEventArgs e) =>
  217. {
  218. textui.Focus();
  219. textui.CaretIndex = textui.Text.Length;
  220. };
  221. CPDFViewer viewer = GetCPDFViewer();
  222. textui.LostFocus += (object sender, RoutedEventArgs e) =>
  223. {
  224. WidgetClickArgs eventparam = new WidgetClickArgs();
  225. BaseWidget currentForm = textui.GetValue(PopupAttachDataProperty) as BaseWidget;
  226. eventparam.Widget = currentForm;
  227. eventparam.UI = textui;
  228. WidgetActionHandler?.Invoke(this, eventparam);
  229. if (currentForm != null && eventparam.Handled == false)
  230. {
  231. AnnotData formData = currentForm.GetAnnotData();
  232. CPDFTextWidget updateWidget = formData.Annot as CPDFTextWidget;
  233. if (updateWidget!=null && updateWidget.Text!=textui.Text)
  234. {
  235. CPDFDocument doc= viewer.GetDocument();
  236. TextBoxHistory textHistory=new TextBoxHistory();
  237. textHistory.Action= HistoryAction.Update;
  238. textHistory.PDFDoc = doc;
  239. textHistory.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(doc, formData.PageIndex, formData.Annot);
  240. updateWidget.SetText(textui.Text);
  241. textHistory.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(doc, formData.PageIndex, formData.Annot);
  242. updateWidget.UpdateFormAp();
  243. viewer?.UpdateAnnotFrame();
  244. viewer?.UndoManager.AddHistory(textHistory);
  245. }
  246. }
  247. };
  248. return textui;
  249. }
  250. }
  251. catch (Exception ex)
  252. {
  253. }
  254. return null;
  255. }
  256. protected UIElement BuildPopListBoxUI(BaseWidget listboxForm)
  257. {
  258. try
  259. {
  260. if (listboxForm != null && listboxForm.GetFormType() == C_WIDGET_TYPE.WIDGET_LISTBOX)
  261. {
  262. AnnotData annotData = listboxForm.GetAnnotData();
  263. ListBox listui = new ListBox();
  264. CPDFListBoxWidget listWidget = annotData.Annot as CPDFListBoxWidget;
  265. if (listWidget == null)
  266. {
  267. return null;
  268. }
  269. CTextAttribute textAttribute = listWidget.GetTextAttribute();
  270. byte transparency = listWidget.GetTransparency();
  271. listui.FontSize = textAttribute.FontSize * annotData.CurrentZoom;
  272. Color textColor = Color.FromArgb(
  273. transparency,
  274. textAttribute.FontColor[0],
  275. textAttribute.FontColor[1],
  276. textAttribute.FontColor[2]);
  277. Color borderColor = Colors.Transparent;
  278. Color backgroundColor = Colors.White;
  279. byte[] colorArray = new byte[3];
  280. if (listWidget.GetWidgetBorderRGBColor(ref colorArray))
  281. {
  282. borderColor = Color.FromRgb(colorArray[0], colorArray[1], colorArray[2]);
  283. }
  284. if (listWidget.GetWidgetBgRGBColor(ref colorArray))
  285. {
  286. backgroundColor = Color.FromRgb(colorArray[0], colorArray[1], colorArray[2]);
  287. }
  288. listui.Foreground = new SolidColorBrush(textColor);
  289. listui.BorderBrush = new SolidColorBrush(borderColor);
  290. listui.Background = new SolidColorBrush(backgroundColor);
  291. listui.BorderThickness = new Thickness(listWidget.GetBorderWidth() * annotData.CurrentZoom);
  292. listui.FontFamily = new FontFamily(GetFontName(textAttribute.FontName));
  293. listui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
  294. listui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
  295. CWidgetItem selectItem = listWidget.GetSelectedItem();
  296. CWidgetItem[] listItems = listWidget.LoadWidgetItems();
  297. if (listItems != null)
  298. {
  299. foreach (CWidgetItem item in listItems)
  300. {
  301. ListBoxItem addItem = new ListBoxItem()
  302. {
  303. FontSize = listui.FontSize,
  304. Content = item.Text
  305. };
  306. listui.Items.Add(addItem);
  307. if (selectItem == null)
  308. {
  309. continue;
  310. }
  311. if (selectItem.Value == item.Value && selectItem.Text == item.Text)
  312. {
  313. listui.SelectedItem = addItem;
  314. }
  315. }
  316. }
  317. SetFormRotateTransform(listui, annotData);
  318. CPDFViewer viewer = GetCPDFViewer();
  319. listui.SelectionChanged += (object sender, SelectionChangedEventArgs e) =>
  320. {
  321. WidgetClickArgs eventparam = new WidgetClickArgs();
  322. BaseWidget currentForm = listui.GetValue(PopupAttachDataProperty) as BaseWidget;
  323. eventparam.Widget = currentForm;
  324. eventparam.UI = listui;
  325. WidgetActionHandler?.Invoke(this, eventparam);
  326. if (currentForm != null && eventparam.Handled == false)
  327. {
  328. AnnotData formData = currentForm.GetAnnotData();
  329. CPDFListBoxWidget updateWidget = formData.Annot as CPDFListBoxWidget;
  330. if (updateWidget != null)
  331. {
  332. int selectIndex = -1;
  333. if (listItems != null && listItems.Length > 0)
  334. {
  335. for (int i = 0; i < listItems.Length; i++)
  336. {
  337. CWidgetItem item = listItems[i];
  338. if (selectItem != null && selectItem.Text == item.Text && selectItem.Value == item.Value)
  339. {
  340. selectIndex = i;
  341. break;
  342. }
  343. }
  344. }
  345. if (selectIndex != listui.SelectedIndex)
  346. {
  347. CPDFDocument doc = viewer.GetDocument();
  348. ListBoxHistory listboxHistory = new ListBoxHistory();
  349. listboxHistory.Action = ComPDFKitViewer.Helper.HistoryAction.Update;
  350. listboxHistory.PDFDoc = doc;
  351. listboxHistory.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(doc, formData.PageIndex, formData.Annot);
  352. updateWidget.SelectItem(listui.SelectedIndex);
  353. listboxHistory.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(doc, formData.PageIndex, formData.Annot);
  354. updateWidget?.UpdateFormAp();
  355. viewer?.UpdateAnnotFrame();
  356. viewer?.UndoManager.AddHistory(listboxHistory);
  357. }
  358. }
  359. }
  360. };
  361. return listui;
  362. }
  363. }
  364. catch (Exception ex)
  365. {
  366. }
  367. return null;
  368. }
  369. protected UIElement BuildPopComboBoxUI(BaseWidget comboboxForm)
  370. {
  371. try
  372. {
  373. if (comboboxForm != null && comboboxForm.GetFormType() == C_WIDGET_TYPE.WIDGET_COMBOBOX)
  374. {
  375. AnnotData annotData = comboboxForm.GetAnnotData();
  376. ComboBox comboboxui=new ComboBox();
  377. CPDFComboBoxWidget comboboxWidget=annotData.Annot as CPDFComboBoxWidget;
  378. if(comboboxWidget==null)
  379. {
  380. return null;
  381. }
  382. CWidgetItem[] comboboxItems = comboboxWidget.LoadWidgetItems();
  383. CWidgetItem selectItem= comboboxWidget.GetSelectedItem();
  384. CTextAttribute textAttribute = comboboxWidget.GetTextAttribute();
  385. byte transparency = comboboxWidget.GetTransparency();
  386. comboboxui.FontSize = textAttribute.FontSize*annotData.CurrentZoom/72D*96D;
  387. Color textColor = Color.FromArgb(
  388. transparency,
  389. textAttribute.FontColor[0],
  390. textAttribute.FontColor[1],
  391. textAttribute.FontColor[2]);
  392. Color borderColor = Colors.Transparent;
  393. Color backgroundColor = Colors.White;
  394. byte[] colorArray = new byte[3];
  395. if (comboboxWidget.GetWidgetBorderRGBColor(ref colorArray))
  396. {
  397. borderColor = Color.FromRgb(colorArray[0], colorArray[1], colorArray[2]);
  398. }
  399. if (comboboxWidget.GetWidgetBgRGBColor(ref colorArray))
  400. {
  401. backgroundColor = Color.FromRgb(colorArray[0], colorArray[1], colorArray[2]);
  402. }
  403. if (comboboxItems != null)
  404. {
  405. foreach (CWidgetItem item in comboboxItems)
  406. {
  407. ComboBoxItem comboItem = new ComboBoxItem();
  408. comboItem.FontSize = comboboxui.FontSize;
  409. comboItem.Content = item.Text;
  410. comboboxui.Items.Add(comboItem);
  411. if (selectItem == null)
  412. {
  413. continue;
  414. }
  415. if (selectItem.Value == item.Value && selectItem.Text == item.Text)
  416. {
  417. comboboxui.SelectedItem = comboItem;
  418. }
  419. }
  420. }
  421. comboboxui.Text = comboboxWidget.GetSelectedItem().Text;
  422. comboboxui.Foreground = new SolidColorBrush(textColor);
  423. comboboxui.BorderBrush = new SolidColorBrush(borderColor);
  424. comboboxui.Background = new SolidColorBrush(backgroundColor);
  425. comboboxui.BorderThickness = new Thickness(comboboxWidget.GetBorderWidth()*annotData.CurrentZoom);
  426. comboboxui.FontFamily = new FontFamily(GetFontName(textAttribute.FontName));
  427. comboboxui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
  428. comboboxui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
  429. SetFormRotateTransform(comboboxui, annotData);
  430. CPDFViewer viewer = GetCPDFViewer();
  431. comboboxui.SelectionChanged += (object sender, SelectionChangedEventArgs e) =>
  432. {
  433. WidgetClickArgs eventparam = new WidgetClickArgs();
  434. BaseWidget currentForm= comboboxui.GetValue(PopupAttachDataProperty) as BaseWidget;
  435. eventparam.Widget = currentForm;
  436. eventparam.UI = comboboxui;
  437. WidgetActionHandler?.Invoke(this, eventparam);
  438. if (currentForm != null && eventparam.Handled == false)
  439. {
  440. AnnotData formData = currentForm.GetAnnotData();
  441. CPDFComboBoxWidget updateWidget = formData.Annot as CPDFComboBoxWidget;
  442. if (updateWidget != null)
  443. {
  444. int selectIndex = -1;
  445. if (comboboxItems != null && comboboxItems.Length > 0)
  446. {
  447. for (int i = 0; i < comboboxItems.Length; i++)
  448. {
  449. CWidgetItem item = comboboxItems[i];
  450. if (selectItem != null && selectItem.Text == item.Text && selectItem.Value == item.Value)
  451. {
  452. selectIndex = i;
  453. break;
  454. }
  455. }
  456. }
  457. if (selectIndex != comboboxui.SelectedIndex)
  458. {
  459. CPDFDocument doc = viewer.GetDocument();
  460. ComboBoxHistory comboboxHistory = new ComboBoxHistory();
  461. comboboxHistory.Action = ComPDFKitViewer.Helper.HistoryAction.Update;
  462. comboboxHistory.PDFDoc = doc;
  463. comboboxHistory.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(doc, formData.PageIndex, formData.Annot);
  464. updateWidget.SelectItem(comboboxui.SelectedIndex);
  465. comboboxHistory.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(doc, formData.PageIndex, formData.Annot);
  466. updateWidget?.UpdateFormAp();
  467. viewer?.UpdateAnnotFrame();
  468. viewer?.UndoManager.AddHistory(comboboxHistory);
  469. }
  470. }
  471. }
  472. };
  473. return comboboxui;
  474. }
  475. }
  476. catch(Exception ex)
  477. {
  478. }
  479. return null;
  480. }
  481. private void UpdateTextUI(TextBox textui,BaseWidget hitform)
  482. {
  483. if(textui!=null && hitform!=null)
  484. {
  485. AnnotData annotData = hitform.GetAnnotData();
  486. CPDFTextWidget textWidget = annotData.Annot as CPDFTextWidget;
  487. if (textWidget == null)
  488. {
  489. return;
  490. }
  491. CTextAttribute textAttribute = textWidget.GetTextAttribute();
  492. textui.FontSize = textAttribute.FontSize * annotData.CurrentZoom;
  493. SetFormRotateTransform(textui, annotData);
  494. textui.SetValue(PopupAttachDataProperty, hitform);
  495. formPopLayer?.Arrange();
  496. }
  497. }
  498. private void UpdateListBoxUI(ListBox listboxui,BaseWidget hitform)
  499. {
  500. if(listboxui!=null &&hitform!=null)
  501. {
  502. AnnotData annotData = hitform.GetAnnotData();
  503. CPDFListBoxWidget listWidget=annotData.Annot as CPDFListBoxWidget;
  504. if(listWidget == null)
  505. {
  506. return;
  507. }
  508. CTextAttribute textAttribute = listWidget.GetTextAttribute();
  509. listboxui.FontSize = textAttribute.FontSize * annotData.CurrentZoom;
  510. listboxui.BorderThickness = new Thickness(listWidget.GetBorderWidth() * annotData.CurrentZoom);
  511. foreach(ListBoxItem item in listboxui.Items)
  512. {
  513. item.FontSize = listboxui.FontSize;
  514. }
  515. SetFormRotateTransform(listboxui, annotData);
  516. listboxui.SetValue(PopupAttachDataProperty, hitform);
  517. formPopLayer?.Arrange();
  518. }
  519. }
  520. private void UpdateComboboxUI(ComboBox comboboxui,BaseWidget hitform)
  521. {
  522. if (comboboxui != null && hitform != null)
  523. {
  524. AnnotData annotData = hitform.GetAnnotData();
  525. CPDFComboBoxWidget comboboxWidget = annotData.Annot as CPDFComboBoxWidget;
  526. if (comboboxWidget == null)
  527. {
  528. return;
  529. }
  530. CTextAttribute textAttribute = comboboxWidget.GetTextAttribute();
  531. comboboxui.FontSize = textAttribute.FontSize * annotData.CurrentZoom;
  532. comboboxui.BorderThickness = new Thickness(comboboxWidget.GetBorderWidth() * annotData.CurrentZoom);
  533. foreach (ComboBoxItem item in comboboxui.Items)
  534. {
  535. item.FontSize = comboboxui.FontSize;
  536. }
  537. SetFormRotateTransform(comboboxui, annotData);
  538. comboboxui.SetValue(PopupAttachDataProperty, hitform);
  539. formPopLayer?.Arrange();
  540. }
  541. }
  542. protected void UpdateFormHitPop()
  543. {
  544. if(formPopLayer==null || formPopLayer.Children.Count==0 || !isInternalPopup)
  545. {
  546. return;
  547. }
  548. FrameworkElement popui = formPopLayer.Children[0] as FrameworkElement;
  549. if(popui==null)
  550. {
  551. return;
  552. }
  553. try
  554. {
  555. BaseWidget hitForm = popui.GetValue(PopupAttachDataProperty) as BaseWidget;
  556. if(hitForm==null)
  557. {
  558. return;
  559. }
  560. AnnotLayer annotLayer = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag()) as AnnotLayer;
  561. BaseAnnot baseAnnot = hitForm;
  562. annotLayer.GetUpdate(ref baseAnnot);
  563. hitForm = baseAnnot as BaseWidget;
  564. if (hitForm.GetFormType()==C_WIDGET_TYPE.WIDGET_TEXTFIELD && (popui is TextBox))
  565. {
  566. UpdateTextUI((TextBox)popui, hitForm);
  567. }
  568. if (hitForm.GetFormType() == C_WIDGET_TYPE.WIDGET_LISTBOX && (popui is ListBox))
  569. {
  570. UpdateListBoxUI((ListBox)popui,hitForm);
  571. }
  572. if (hitForm.GetFormType() == C_WIDGET_TYPE.WIDGET_COMBOBOX && (popui is ComboBox))
  573. {
  574. UpdateComboboxUI((ComboBox)popui, hitForm);
  575. }
  576. }
  577. catch(Exception ex)
  578. {
  579. }
  580. }
  581. protected void FormClickProcess()
  582. {
  583. BaseWidget currentForm = PDFViewer?.AnnotHitTest() as BaseWidget;
  584. if (currentForm != null)
  585. {
  586. List<C_WIDGET_TYPE> formTypeList = new List<C_WIDGET_TYPE>()
  587. {
  588. C_WIDGET_TYPE.WIDGET_CHECKBOX,
  589. C_WIDGET_TYPE.WIDGET_RADIOBUTTON,
  590. C_WIDGET_TYPE.WIDGET_PUSHBUTTON
  591. };
  592. if(formTypeList.Contains(currentForm.GetFormType()))
  593. {
  594. WidgetClickArgs eventparam = new WidgetClickArgs();
  595. eventparam.Widget = currentForm;
  596. WidgetActionHandler?.Invoke(this, eventparam);
  597. if (eventparam.Handled == false)
  598. {
  599. switch (currentForm.GetFormType())
  600. {
  601. case C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
  602. FormRadioButtonClick(currentForm);
  603. break;
  604. case C_WIDGET_TYPE.WIDGET_CHECKBOX:
  605. FormCheckBoxClick(currentForm);
  606. break;
  607. case C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
  608. FormPushButtonClick(currentForm);
  609. break;
  610. default:
  611. break;
  612. }
  613. }
  614. }
  615. }
  616. }
  617. private void FormRadioButtonClick(BaseWidget clickForm)
  618. {
  619. if(clickForm!=null && clickForm.GetFormType()==C_WIDGET_TYPE.WIDGET_RADIOBUTTON)
  620. {
  621. AnnotData formData = clickForm.GetAnnotData();
  622. CPDFRadioButtonWidget updateWidget = formData.Annot as CPDFRadioButtonWidget;
  623. CPDFViewer viewer = GetCPDFViewer();
  624. CPDFDocument doc = viewer?.GetDocument();
  625. if (viewer != null && doc != null)
  626. {
  627. if (updateWidget.IsChecked() == false)
  628. {
  629. GroupHistory historyGroup=new GroupHistory();
  630. for (int i = 0; i < doc.PageCount; i++)
  631. {
  632. SetFormButtonChecked(i, doc, updateWidget.GetFieldName(), false, historyGroup);
  633. }
  634. RadioButtonHistory history=new RadioButtonHistory();
  635. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(doc, updateWidget.Page.PageIndex, updateWidget);
  636. history.PDFDoc= doc;
  637. history.Action=HistoryAction.Update;
  638. updateWidget.SetChecked(true);
  639. updateWidget.UpdateFormAp();
  640. viewer.UpdateAnnotFrame();
  641. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(doc, updateWidget.Page.PageIndex, updateWidget);
  642. historyGroup?.Histories.Add(history);
  643. viewer?.UndoManager?.AddHistory(historyGroup);
  644. }
  645. }
  646. }
  647. }
  648. private void FormCheckBoxClick(BaseWidget clickForm)
  649. {
  650. if (clickForm != null && clickForm.GetFormType() == C_WIDGET_TYPE.WIDGET_CHECKBOX)
  651. {
  652. AnnotData formData = clickForm.GetAnnotData();
  653. CPDFCheckBoxWidget updateWidget = formData.Annot as CPDFCheckBoxWidget;
  654. bool isCheck = !updateWidget.IsChecked();
  655. CPDFViewer viewer = GetCPDFViewer();
  656. CPDFDocument doc = viewer?.GetDocument();
  657. if (viewer != null && doc != null)
  658. {
  659. GroupHistory historyGroup = new GroupHistory();
  660. for (int i = 0; i < doc.PageCount; i++)
  661. {
  662. SetFormButtonChecked(i, doc, updateWidget.GetFieldName(), isCheck, historyGroup);
  663. }
  664. if(historyGroup.Histories.Count > 0)
  665. {
  666. viewer?.UndoManager?.AddHistory(historyGroup);
  667. }
  668. updateWidget.UpdateFormAp();
  669. viewer.UpdateAnnotFrame();
  670. }
  671. }
  672. }
  673. private void FormPushButtonClick(BaseWidget clickForm)
  674. {
  675. if (clickForm != null && clickForm.GetFormType() == C_WIDGET_TYPE.WIDGET_PUSHBUTTON)
  676. {
  677. AnnotData formData = clickForm.GetAnnotData();
  678. CPDFPushButtonWidget updateWidget = formData.Annot as CPDFPushButtonWidget;
  679. CPDFViewer viewer = GetCPDFViewer();
  680. CPDFDocument doc = viewer?.GetDocument();
  681. if (viewer != null && doc != null)
  682. {
  683. CPDFAction action = updateWidget.GetButtonAction();
  684. PDFActionHandler(action, doc, viewer);
  685. }
  686. }
  687. }
  688. internal void SetFormButtonChecked(int pageIndex, CPDFDocument currentDoc, string fieldName, bool isCheck,GroupHistory historyGroup)
  689. {
  690. if (pageIndex < 0 || currentDoc==null || pageIndex>=currentDoc.PageCount)
  691. {
  692. return;
  693. }
  694. CPDFPage docPage = currentDoc.PageAtIndex(pageIndex, false);
  695. if (docPage == null)
  696. {
  697. return;
  698. }
  699. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  700. if (docAnnots != null && docAnnots.Count > 0)
  701. {
  702. foreach (CPDFAnnotation annotCore in docAnnots)
  703. {
  704. if (annotCore == null || annotCore.Type != C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  705. {
  706. continue;
  707. }
  708. CPDFWidget widget = (CPDFWidget)annotCore;
  709. if (widget != null)
  710. {
  711. if (widget.WidgetType == C_WIDGET_TYPE.WIDGET_RADIOBUTTON && widget.GetFieldName() == fieldName)
  712. {
  713. RadioButtonHistory history = new RadioButtonHistory();
  714. history.PDFDoc = currentDoc;
  715. history.Action = HistoryAction.Update;
  716. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(currentDoc, pageIndex, widget);
  717. widget.ResetForm();
  718. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(currentDoc, pageIndex, widget);
  719. historyGroup?.Histories.Add(history);
  720. }
  721. if (widget.WidgetType == C_WIDGET_TYPE.WIDGET_CHECKBOX && widget.GetFieldName() == fieldName)
  722. {
  723. CheckBoxHistory history = new CheckBoxHistory();
  724. history.PDFDoc = currentDoc;
  725. history.Action = HistoryAction.Update;
  726. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(currentDoc, pageIndex, widget);
  727. (widget as CPDFCheckBoxWidget)?.SetChecked(isCheck);
  728. widget.UpdateFormAp();
  729. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(currentDoc, pageIndex, widget);
  730. historyGroup?.Histories.Add(history);
  731. }
  732. }
  733. }
  734. }
  735. }
  736. internal void PDFActionHandler(CPDFAction action,CPDFDocument currentDoc,CPDFViewer viewer)
  737. {
  738. if (action != null && currentDoc!=null)
  739. {
  740. try
  741. {
  742. switch (action.ActionType)
  743. {
  744. case C_ACTION_TYPE.ACTION_TYPE_NAMED:
  745. {
  746. CPDFNamedAction namedAction = action as CPDFNamedAction;
  747. string name = namedAction.GetName();
  748. int pageIndex = -1;
  749. if (name.ToLower() == "firstpage")
  750. {
  751. pageIndex = 0;
  752. }
  753. if (name.ToLower() == "lastpage")
  754. {
  755. pageIndex = currentDoc.PageCount - 1;
  756. }
  757. if (name.ToLower() == "nextpage" && viewer.CurrentRenderFrame != null)
  758. {
  759. pageIndex = Math.Min(viewer.CurrentRenderFrame.PageIndex + 1, currentDoc.PageCount - 1);
  760. }
  761. if (name.ToLower() == "prevpage" && viewer.CurrentRenderFrame != null)
  762. {
  763. pageIndex = Math.Max(viewer.CurrentRenderFrame.PageIndex - 1, 0);
  764. }
  765. if (pageIndex != -1)
  766. {
  767. viewer.GoToPage(pageIndex, new Point(0, 0));
  768. }
  769. }
  770. break;
  771. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  772. {
  773. CPDFGoToAction gotoAction = action as CPDFGoToAction;
  774. CPDFDestination dest = gotoAction.GetDestination(currentDoc);
  775. if (dest != null)
  776. {
  777. viewer.GoToPage(dest.PageIndex, new Point(0, 0));
  778. }
  779. }
  780. break;
  781. case C_ACTION_TYPE.ACTION_TYPE_GOTOR:
  782. {
  783. CPDFGoToRAction gotorAction = action as CPDFGoToRAction;
  784. CPDFDestination dest = gotorAction.GetDestination(currentDoc);
  785. if (dest != null)
  786. {
  787. viewer.GoToPage(dest.PageIndex, new Point(0, 0));
  788. }
  789. }
  790. break;
  791. case C_ACTION_TYPE.ACTION_TYPE_URI:
  792. {
  793. CPDFUriAction uriAction = action as CPDFUriAction;
  794. string uri = uriAction.GetUri();
  795. if (!string.IsNullOrEmpty(uri))
  796. {
  797. Process.Start(uri);
  798. }
  799. }
  800. break;
  801. default:
  802. break;
  803. }
  804. }
  805. catch(Exception ex)
  806. {
  807. }
  808. }
  809. }
  810. internal void InvokeWidgetCreated(CPDFAnnotation annot)
  811. {
  812. if(annot!=null && annot.IsValid())
  813. {
  814. WidgetCreatedHandler?.Invoke(this, annot);
  815. }
  816. }
  817. }
  818. }