CPDFViewerTool.WidgetTool.cs 39 KB

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