FormBarControl.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKit.PDFDocument.Action;
  4. using ComPDFKit.Tool;
  5. using ComPDFKit.Tool.SettingParam;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Runtime.CompilerServices;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Controls.Primitives;
  13. using System.Windows.Media;
  14. using System.Windows.Shapes;
  15. using System.Xml.Linq;
  16. using Compdfkit_Tools.Helper;
  17. namespace Compdfkit_Tools.PDFControl
  18. {
  19. public partial class CPDFFormBarControl : UserControl, INotifyPropertyChanged
  20. {
  21. private string textField = LanguageHelper.ToolBarManager.GetString("Button_TextField");
  22. private string checkBox = LanguageHelper.ToolBarManager.GetString("Button_Chb");
  23. private string radioButton = LanguageHelper.ToolBarManager.GetString("Button_Rdo");
  24. private string listBox = LanguageHelper.ToolBarManager.GetString("Button_ListBox");
  25. private string comboBox = LanguageHelper.ToolBarManager.GetString("Button_Cmb");
  26. private string pushButton = LanguageHelper.ToolBarManager.GetString("Button_Btn");
  27. private string signature = LanguageHelper.ToolBarManager.GetString("Button_Sig");
  28. private bool isFirstLoad = true;
  29. enum FromType
  30. {
  31. UnKnown = -1,
  32. Textbox,
  33. CheckBox,
  34. RadioButton,
  35. ListBox,
  36. ComboBox,
  37. Pushbutton,
  38. Signature,
  39. }
  40. #region Data
  41. public event EventHandler<C_WIDGET_TYPE> AnnotationPropertyChanged;
  42. private Dictionary<string, string> ButtonDict;
  43. public event PropertyChangedEventHandler PropertyChanged;
  44. protected void OnPropertyChanged([CallerMemberName] string name = null)
  45. {
  46. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  47. }
  48. private PDFViewControl pdfViewer;
  49. private FromPropertyControl fromPropertyControl = null;
  50. #endregion
  51. #region Create Default UI
  52. public CPDFFormBarControl()
  53. {
  54. ButtonDict = new Dictionary<string, string>
  55. {
  56. {textField,"M0.75 2.25H20.25V17.75H0.75V2.25ZM18.75 11.75V3.75H2.25V16.25H14.25L18.75 11.75ZM11.75 5.5V8H10.25V7H8.5V13H9.25V14.5H6.25V13H7V7H5.25V8H3.75V5.5H11.75Z"},
  57. {checkBox, "M18.75 1.75H2.25V18.25H18.75V1.75ZM17.25 3.25V16.75H3.75V3.25H17.25ZM16.032 7.52861L14.968 6.47139L9.538 11.935L7.03204 9.41291L5.96796 10.4701L9.53873 14.0641L16.032 7.52861Z"},
  58. {radioButton,"M10.5 1.25C5.66751 1.25 1.75 5.16751 1.75 10C1.75 14.8325 5.66751 18.75 10.5 18.75C15.3325 18.75 19.25 14.8325 19.25 10C19.25 5.16751 15.3325 1.25 10.5 1.25ZM10.5 2.75C14.5041 2.75 17.75 5.99594 17.75 10C17.75 14.0041 14.5041 17.25 10.5 17.25C6.49594 17.25 3.25 14.0041 3.25 10C3.25 5.99594 6.49594 2.75 10.5 2.75ZM10.5 13C12.1569 13 13.5 11.6569 13.5 10C13.5 8.34315 12.1569 7 10.5 7C8.84315 7 7.5 8.34315 7.5 10C7.5 11.6569 8.84315 13 10.5 13Z"},
  59. {listBox,"M20.25 2.25H0.75V17.75H20.25V2.25ZM18.75 10.75V16.25H2.25V10.75H18.75ZM18.75 9.25V3.75H2.25V9.25H18.75ZM6.5 5.5H3.5V7.5H6.5V5.5ZM3.5 12.5H6.5V14.5H3.5V12.5Z"},
  60. {comboBox,"M0.75 2.25H20.25V10.75H16.25V17.75H0.75V10.75V9.25V2.25ZM2.25 10.75H14.75V16.25H2.25V10.75ZM18.75 9.25H16.25H2.25V3.75H18.75V9.25ZM15.5 8L17.5 5.5H13.5L15.5 8Z"},
  61. {pushButton,"M20.5 3H0.5V17H20.5V3ZM10.0918 10.3271C10.0918 12.3794 8.91406 13.6626 7.04199 13.6626C5.15674 13.6626 3.98779 12.3794 3.98779 10.3271C3.98779 8.27051 5.17432 6.99609 7.04199 6.99609C8.90967 6.99609 10.0918 8.2749 10.0918 10.3271ZM5.3457 10.3271C5.3457 11.6719 5.99609 12.5376 7.04199 12.5376C8.07471 12.5376 8.73389 11.6719 8.73389 10.3271C8.73389 8.97803 8.07031 8.12109 7.04199 8.12109C6.00928 8.12109 5.3457 8.97803 5.3457 10.3271ZM12.6758 13.5V11.562L13.3042 10.8062L15.1455 13.5H16.7363L14.2622 9.93604L16.5737 7.15869H15.0972L12.7549 10.0063H12.6758V7.15869H11.3486V13.5H12.6758Z"},
  62. {signature, "M7.08813 17.5815C8.62427 17.5815 9.82397 16.6738 10.6047 15.1504H20.5V13.8428H11.1316C11.4363 12.8652 11.614 11.7227 11.6584 10.4658C12.0583 10.3833 12.4773 10.3325 12.8962 10.3325C13.0422 10.3325 13.1248 10.4023 13.1248 10.5039C13.1248 10.98 12.801 11.3862 12.801 11.9956C12.801 12.5669 13.22 12.9414 13.8103 12.9414C14.4691 12.9414 15.1903 12.455 15.808 11.9475L16.1336 11.6713L16.6994 11.1736C16.9478 10.9597 17.1384 10.8149 17.2444 10.8149C17.3279 10.8149 17.3739 10.9147 17.4151 11.0681L17.5009 11.4329L17.5572 11.6434C17.7076 12.1446 18.0105 12.6875 18.8821 12.6875C19.0167 12.6875 19.2501 12.6207 19.5822 12.4871L19.8911 12.3562L20.2483 12.1926L19.988 10.8914L19.6787 11.0317L19.4291 11.1369L19.2391 11.2071C19.1591 11.2334 19.1014 11.2466 19.0662 11.2466C18.9293 11.2466 18.8686 11.0858 18.8206 10.8544L18.739 10.4051C18.6308 9.83926 18.4286 9.19629 17.6252 9.19629C17.0135 9.19629 16.3006 9.71182 15.7085 10.2028L15.3988 10.4649L15.1211 10.7027C14.8619 10.9218 14.6627 11.0752 14.5593 11.0752C14.5022 11.0752 14.4768 11.0498 14.4768 10.98C14.4768 10.925 14.4923 10.8566 14.513 10.7785L14.5802 10.5192C14.6009 10.4256 14.6165 10.3262 14.6165 10.2246C14.6165 9.41846 14.0325 8.93604 13.0803 8.93604C12.5916 8.93604 12.1091 8.98047 11.6394 9.06934C11.4109 5.62891 9.68433 3.08984 7.24683 3.08984C5.39966 3.08984 4.10474 4.49902 4.10474 6.46045C4.10474 8.60596 5.46313 10.4023 6.94849 11.5703C6.23755 12.2876 5.69165 13.0747 5.35522 13.8428H0.5V15.1504H5.0061C4.99341 15.252 4.98706 15.3472 4.98706 15.4487C4.98706 16.75 5.72339 17.5815 7.08813 17.5815ZM7.99585 10.6689C6.79614 9.74219 5.50122 8.32031 5.50122 6.44775C5.50122 5.31152 6.21851 4.49268 7.25317 4.49268C8.94165 4.49268 10.1477 6.61914 10.262 9.44385C9.43042 9.74854 8.6687 10.1675 7.99585 10.6689ZM1.39766 12.9448L2.32073 12.0218L3.2438 12.9448L3.99599 12.1926L3.0761 11.2727L3.99599 10.3528L3.24379 9.60065L2.32073 10.5237L1.39666 9.60601L0.64764 10.355L1.5717 11.2727L0.648633 12.1958L1.39766 12.9448ZM9.6272 13.8428H6.95483C7.24048 13.354 7.64673 12.8462 8.14185 12.3765L8.77184 12.737L9.7795 11.8768C9.53409 11.7317 9.35213 11.6084 9.23364 11.5068C9.54468 11.3037 9.8811 11.1133 10.2302 10.9482C10.1477 12.0464 9.93823 13.0303 9.6272 13.8428ZM7.25317 16.1851C6.73267 16.1851 6.45972 15.8359 6.45972 15.3091C6.45972 15.2583 6.46606 15.2075 6.47241 15.1504H8.948C8.47192 15.8042 7.89429 16.1851 7.25317 16.1851Z"},
  63. };
  64. InitializeComponent();
  65. }
  66. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  67. {
  68. if (isFirstLoad)
  69. {
  70. foreach (KeyValuePair<string, string> data in ButtonDict)
  71. {
  72. string Path = data.Value;
  73. string name = data.Key;
  74. Geometry annotationGeometry = Geometry.Parse(Path);
  75. Path path = new Path
  76. {
  77. Width = 20,
  78. Height = 20,
  79. Data = annotationGeometry,
  80. Fill = new SolidColorBrush(Color.FromRgb(0x43, 0x47, 0x4D))
  81. };
  82. CreateButtonForPath(path, name);
  83. }
  84. isFirstLoad = false;
  85. }
  86. }
  87. private void CreateButtonForPath(Path path, String name)
  88. {
  89. StackPanel stackPanel = new StackPanel();
  90. TextBlock textBlock = new TextBlock();
  91. if (path != null)
  92. {
  93. stackPanel.Orientation = Orientation.Horizontal;
  94. stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
  95. stackPanel.Children.Add(path);
  96. }
  97. if (!string.IsNullOrEmpty(name))
  98. {
  99. textBlock.Text = name;
  100. textBlock.VerticalAlignment = VerticalAlignment.Center;
  101. textBlock.Margin = new Thickness(8, 0, 0, 0);
  102. textBlock.FontSize = 12;
  103. stackPanel.Children.Add(textBlock);
  104. }
  105. Style style = (Style)FindResource("RoundMarginToggleButtonStyle");
  106. ToggleButton button = new ToggleButton();
  107. button.BorderThickness = new Thickness(0);
  108. button.Padding = new Thickness(10, 5, 10, 5);
  109. button.Tag = name;
  110. button.ToolTip = name;
  111. button.Style = style;
  112. button.Content = stackPanel;
  113. button.Click -= FormBtn_Click;
  114. button.Click += FormBtn_Click;
  115. FormGrid.Children.Add(button);
  116. }
  117. #endregion
  118. #region Even Process
  119. public void InitWithPDFViewer(PDFViewControl pdfViewer, FromPropertyControl FromProperty)
  120. {
  121. this.pdfViewer = pdfViewer;
  122. fromPropertyControl = FromProperty;
  123. }
  124. public void CheckedButtonForName(string name)
  125. {
  126. foreach (UIElement child in FormGrid.Children)
  127. {
  128. if (child is ToggleButton toggle)
  129. {
  130. if (toggle.Tag.ToString() == name)
  131. {
  132. toggle.IsChecked = true;
  133. }
  134. else
  135. {
  136. toggle.IsChecked = false;
  137. }
  138. }
  139. }
  140. }
  141. public void ClearAllToolState()
  142. {
  143. foreach (UIElement child in FormGrid.Children)
  144. {
  145. if (child is ToggleButton toggle)
  146. {
  147. toggle.IsChecked = false;
  148. }
  149. }
  150. }
  151. private void ClearToolState(UIElement sender)
  152. {
  153. foreach (UIElement child in FormGrid.Children)
  154. {
  155. if (child is ToggleButton toggle && (child as ToggleButton) != (sender as ToggleButton))
  156. {
  157. toggle.IsChecked = false;
  158. }
  159. }
  160. }
  161. private void FormBtn_Click(object sender, RoutedEventArgs e)
  162. {
  163. ClearToolState(sender as ToggleButton);
  164. pdfViewer.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_NONE);
  165. fromPropertyControl.SetPropertyForType(null, null,null);
  166. if ((bool)(sender as ToggleButton).IsChecked)
  167. {
  168. switch (StringToType((sender as ToggleButton).Tag.ToString()))
  169. {
  170. case FromType.Textbox:
  171. CreateTextBox();
  172. return;
  173. case FromType.CheckBox:
  174. CreateCheckBox();
  175. return;
  176. case FromType.RadioButton:
  177. CreateRadioBtn();
  178. return;
  179. case FromType.ListBox:
  180. CreateListBox();
  181. return;
  182. case FromType.ComboBox:
  183. CreateComboBox();
  184. return;
  185. case FromType.Pushbutton:
  186. CreatePushBtn();
  187. return;
  188. case FromType.Signature:
  189. CreateSign();
  190. return;
  191. case FromType.UnKnown:
  192. default:
  193. break;
  194. }
  195. pdfViewer.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  196. }
  197. else
  198. {
  199. pdfViewer.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  200. }
  201. }
  202. private FromType StringToType(string type)
  203. {
  204. if (type == textField) return FromType.Textbox;
  205. if (type == checkBox) return FromType.CheckBox;
  206. if (type == radioButton) return FromType.RadioButton;
  207. if (type == listBox) return FromType.ListBox;
  208. if (type == comboBox) return FromType.ComboBox;
  209. if (type == pushButton) return FromType.Pushbutton;
  210. if (type == signature) return FromType.Signature;
  211. return FromType.UnKnown;
  212. }
  213. #endregion
  214. #region Create Form
  215. private string GetTime()
  216. {
  217. DateTime dateTime = DateTime.Now;
  218. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  219. }
  220. private void CreateTextBox()
  221. {
  222. pdfViewer.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  223. pdfViewer.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_TEXTFIELD);
  224. TextBoxParam textBoxParam = new TextBoxParam();
  225. textBoxParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET;
  226. textBoxParam.WidgetType = C_WIDGET_TYPE.WIDGET_TEXTFIELD;
  227. textBoxParam.LineWidth = 1;
  228. textBoxParam.FontName = "Helvetica";
  229. textBoxParam.LineColor =new byte[] {0,0,0 };
  230. textBoxParam.FontColor = new byte[] { 0, 0, 0 };
  231. textBoxParam.Transparency = 255;
  232. textBoxParam.HasLineColor = true;
  233. textBoxParam.FieldName = "Text"+ GetTime();
  234. pdfViewer.SetAnnotParam(textBoxParam);
  235. }
  236. private void CreateRadioBtn()
  237. {
  238. pdfViewer.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  239. pdfViewer.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_RADIOBUTTON);
  240. RadioButtonParam radioButtonParam = new RadioButtonParam();
  241. radioButtonParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET;
  242. radioButtonParam.WidgetType = C_WIDGET_TYPE.WIDGET_RADIOBUTTON;
  243. radioButtonParam.CheckStyle = C_CHECK_STYLE.CK_CIRCLE;
  244. radioButtonParam.BorderStyle = C_BORDER_STYLE.BS_SOLID;
  245. radioButtonParam.LineColor = new byte[] { 0, 0, 0 };
  246. radioButtonParam.FontColor = new byte[] { 0, 0, 0 };
  247. radioButtonParam.Transparency = 255;
  248. radioButtonParam.HasLineColor = true;
  249. radioButtonParam.LineWidth = 2;
  250. radioButtonParam.FieldName = "Radio button" + GetTime();
  251. pdfViewer.SetAnnotParam(radioButtonParam);
  252. }
  253. private void CreateCheckBox()
  254. {
  255. pdfViewer.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  256. pdfViewer.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_CHECKBOX);
  257. CheckBoxParam checkBoxParam = new CheckBoxParam();
  258. checkBoxParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET;
  259. checkBoxParam.WidgetType = C_WIDGET_TYPE.WIDGET_CHECKBOX;
  260. checkBoxParam.CheckStyle = C_CHECK_STYLE.CK_CHECK;
  261. checkBoxParam.BorderStyle = C_BORDER_STYLE.BS_SOLID;
  262. checkBoxParam.LineColor = new byte[] { 0, 0, 0 };
  263. checkBoxParam.FontColor = new byte[] { 0, 0, 0 };
  264. checkBoxParam.Transparency = 255;
  265. checkBoxParam.HasLineColor = true;
  266. checkBoxParam.LineWidth = 1;
  267. checkBoxParam.FieldName = "Checkbox" + GetTime();
  268. pdfViewer.SetAnnotParam(checkBoxParam);
  269. }
  270. private void CreateComboBox()
  271. {
  272. pdfViewer.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  273. pdfViewer.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_COMBOBOX);
  274. ComboBoxParam comboBoxParam = new ComboBoxParam();
  275. comboBoxParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET;
  276. comboBoxParam.WidgetType = C_WIDGET_TYPE.WIDGET_COMBOBOX;
  277. comboBoxParam.LineColor = new byte[] { 0, 0, 0 };
  278. comboBoxParam.FontColor = new byte[] { 0, 0, 0 };
  279. comboBoxParam.Transparency = 255;
  280. comboBoxParam.HasLineColor = true;
  281. comboBoxParam.LineWidth = 1;
  282. comboBoxParam.FieldName = "Combobox" + GetTime();
  283. pdfViewer.SetAnnotParam(comboBoxParam);
  284. }
  285. private void CreateListBox()
  286. {
  287. pdfViewer.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  288. pdfViewer.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_LISTBOX);
  289. ListBoxParam listBoxParam = new ListBoxParam();
  290. listBoxParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET;
  291. listBoxParam.WidgetType = C_WIDGET_TYPE.WIDGET_LISTBOX;
  292. listBoxParam.LineColor = new byte[] { 0, 0, 0 };
  293. listBoxParam.FontColor = new byte[] { 0, 0, 0 };
  294. listBoxParam.Transparency = 255;
  295. listBoxParam.HasLineColor = true;
  296. listBoxParam.LineWidth = 1;
  297. listBoxParam.FieldName = "List" + GetTime();
  298. pdfViewer.SetAnnotParam(listBoxParam);
  299. }
  300. private void CreatePushBtn()
  301. {
  302. pdfViewer.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  303. pdfViewer.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_PUSHBUTTON);
  304. PushButtonParam pushButtonParam = new PushButtonParam();
  305. pushButtonParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET;
  306. pushButtonParam.WidgetType = C_WIDGET_TYPE.WIDGET_PUSHBUTTON;
  307. pushButtonParam.Text = "Push Button";
  308. pushButtonParam.LineWidth = 1;
  309. pushButtonParam.HasLineColor = true;
  310. pushButtonParam.Action = C_ACTION_TYPE.ACTION_TYPE_URI;
  311. pushButtonParam.Uri = @"https://www.compdf.com";
  312. pushButtonParam.LineColor = new byte[] { 0, 0, 0 };
  313. pushButtonParam.FontColor = new byte[] { 0, 0, 0 };
  314. pushButtonParam.Transparency = 255;
  315. pushButtonParam.FieldName = "Button" + GetTime();
  316. pdfViewer.SetAnnotParam(pushButtonParam);
  317. }
  318. private void CreateSign()
  319. {
  320. pdfViewer.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.WidgetEdit);
  321. pdfViewer.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS);
  322. SignatureParam signatureParam = new SignatureParam();
  323. signatureParam.CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET;
  324. signatureParam.WidgetType = C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS;
  325. signatureParam.LineWidth = 1;
  326. signatureParam.HasLineColor = true;
  327. signatureParam.LineColor = new byte[] { 0, 0, 0 };
  328. signatureParam.FontColor = new byte[] { 0, 0, 0 };
  329. signatureParam.Transparency = 255;
  330. signatureParam.FieldName= "Signature" + GetTime();
  331. pdfViewer.SetAnnotParam(signatureParam);
  332. }
  333. #endregion
  334. }
  335. }