CPDFViewerTool.WidgetTool.cs 39 KB

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