CPDFViewerTool.WidgetTool.cs 39 KB

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