using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using ComPDFKit.PDFAnnotation; using ComPDFKit.PDFAnnotation.Form; using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; namespace ComPDFKitDemo.WidgetForm { public partial class ComboBoxFormProperty : Window { public WidgetAttribEvent CurrentAttribEvent; private Dictionary optionsList; public ComboBoxFormProperty() { InitializeComponent(); } private void NavClick(object sender, MouseButtonEventArgs e) { foreach (TextBlock child in FormStack.Children) { child.Foreground = Brushes.Black; child.Background = Brushes.Transparent; } TextBlock clickBlock = sender as TextBlock; clickBlock.Background = new SolidColorBrush(Color.FromRgb(0xD1, 0xE2, 0xF2)); RegularPanel.Visibility = Visibility.Collapsed; AppearancePanel.Visibility = Visibility.Collapsed; OptionPanel.Visibility = Visibility.Collapsed; switch (clickBlock.Tag.ToString()) { case "Regular": RegularPanel.Visibility = Visibility.Visible; break; case "Appearance": AppearancePanel.Visibility = Visibility.Visible; break; case "Option": OptionPanel.Visibility = Visibility.Visible; break; default: break; } } public void SetFormData(WidgetAttribEvent annotEvent) { optionsList = null; if (annotEvent != null) { FontStyleBox.SelectedIndex = 0; bool hideOptions = true; foreach (AnnotAttrib attr in annotEvent.Attribs.Keys) { switch (attr) { case AnnotAttrib.FieldName: GroupNameText.Text = annotEvent.Attribs[attr].ToString(); break; case AnnotAttrib.Color: BorderColorPicker.SelectedColor = (Color)annotEvent.Attribs[attr]; break; case AnnotAttrib.FillColor: FillColorPicker.SelectedColor = (Color)annotEvent.Attribs[attr]; break; case AnnotAttrib.FontFamily: List fontFamilyList = new List() { "Helvetica", "Courier", "Times" }; string currentFont = annotEvent.Attribs[attr].ToString(); FontFamilyBox.SelectedIndex = -1; for (int i = 0; i < fontFamilyList.Count; i++) { if (fontFamilyList[i].ToLower().Contains(currentFont.ToLower())) { FontFamilyBox.SelectedIndex = i; } } break; case AnnotAttrib.FontSize: FontSizeBox.Text = annotEvent.Attribs[attr].ToString(); break; case AnnotAttrib.FontColor: { FontColorPicker.SelectedColor = (Color)annotEvent.Attribs[attr]; } break; case AnnotAttrib.FontStyle: { FontStyle currentStyle = (FontStyle)annotEvent.Attribs[attr]; if (currentStyle == FontStyles.Italic) { FontStyleBox.SelectedIndex = FontStyleBox.SelectedIndex + 1; } } break; case AnnotAttrib.FontWeight: { FontWeight currentWeight = (FontWeight)annotEvent.Attribs[attr]; if (currentWeight == FontWeights.Bold) { FontStyleBox.SelectedIndex = FontStyleBox.SelectedIndex + 1; } } break; case AnnotAttrib.Thickness: ThicknessText.SelectedIndex = Convert.ToInt32(annotEvent.Attribs[attr]) - 1; break; case AnnotAttrib.LineStyle: if ((C_BORDER_STYLE)annotEvent.Attribs[attr] == C_BORDER_STYLE.BS_DASHDED) { LineStyleBox.SelectedIndex = 1; } else { LineStyleBox.SelectedIndex = 0; } break; case AnnotAttrib.Locked: { LockBox.IsChecked = (bool)annotEvent.Attribs[attr] == true; EnableControls(!(LockBox.IsChecked == true)); } break; case AnnotAttrib.ReadOnly: { ReadOnlyBox.IsChecked = (bool)annotEvent.Attribs[attr] == true; } break; case AnnotAttrib.FormField: { FormField field = (FormField)annotEvent.Attribs[attr]; FormFieldBox.SelectedIndex = (int)field; } break; case AnnotAttrib.ListOptions: { optionsList = (Dictionary)annotEvent.Attribs[attr]; if (optionsList != null) { foreach (string key in optionsList.Keys) { ListBoxItem item = new ListBoxItem(); item.Content = key; item.Tag = optionsList[key]; OptionsListBox.Items.Add(item); } hideOptions = false; } } break; case AnnotAttrib.DefaultChoice: { List selectedIndexList = (List)annotEvent.Attribs[attr]; if (selectedIndexList != null) { foreach (int index in selectedIndexList) { if (OptionsListBox.Items.Count > index) { (OptionsListBox.Items[index] as ListBoxItem).IsSelected = true; } } hideOptions = false; } } break; default: break; } } OptinTab.Visibility = hideOptions == false ? Visibility.Visible : Visibility.Collapsed; if (hideOptions) { foreach (UIElement child in OptionPanel.Children) { child.Visibility = Visibility.Collapsed; } OptinTab.Visibility = Visibility.Collapsed; } } } private void OptionsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBoxItem selectItem = OptionsListBox.SelectedItem as ListBoxItem; if (selectItem != null) { OptionName.Text = selectItem.Content.ToString(); OptionValue.Text = selectItem.Tag.ToString(); } } private void GroupNameText_TextChanged(object sender, TextChangedEventArgs e) { if (GroupNameText.Text.Length > 0) { CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FieldName, GroupNameText.Text); CurrentAttribEvent?.UpdateAnnot(); } } private void BorderColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs e) { CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Color, e.NewValue); CurrentAttribEvent?.UpdateAnnot(); } private void FillColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs e) { CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FillColor, e.NewValue); CurrentAttribEvent?.UpdateAnnot(); } private void FontFamilyBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string fontName = (FontFamilyBox.SelectedItem as ComboBoxItem).Content.ToString(); CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontFamily, fontName); CurrentAttribEvent?.UpdateAnnot(); } private void FontSizeBox_TextChanged(object sender, TextChangedEventArgs e) { int currentSize = 12; if (int.TryParse(FontSizeBox.Text, out currentSize)) { CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontSize, currentSize); CurrentAttribEvent?.UpdateAnnot(); } } private void FontStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { switch (FontStyleBox.SelectedIndex) { case 0: CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal); CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal); break; case 1: CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal); CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold); break; case 2: CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic); CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal); break; case 3: CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic); CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold); break; } CurrentAttribEvent?.UpdateAnnot(); } private void FontColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs e) { CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontColor, e.NewValue); CurrentAttribEvent?.UpdateAnnot(); } private void OptionName_TextChanged(object sender, TextChangedEventArgs e) { string currentKey = OptionName.Text.Trim(); AddOptionBtn.IsEnabled = true; if (optionsList != null && optionsList.ContainsKey(currentKey)) { AddOptionBtn.IsEnabled = false; } if (currentKey == string.Empty) { AddOptionBtn.IsEnabled = false; } } private void OptionValue_TextChanged(object sender, TextChangedEventArgs e) { string currentKey = OptionName.Text.Trim(); string currentValue = OptionValue.Text.Trim(); if (currentKey == string.Empty || currentValue == string.Empty) { AddOptionBtn.IsEnabled = false; return; } AddOptionBtn.IsEnabled = true; if (optionsList != null) { if (optionsList.ContainsKey(currentKey)) { AddOptionBtn.IsEnabled = false; return; } if (optionsList.Values.Contains(currentValue)) { AddOptionBtn.IsEnabled = false; return; } } } private void Button_Click(object sender, RoutedEventArgs e) { string currentKey = OptionName.Text.Trim(); string currentValue = OptionValue.Text.Trim(); if (currentKey == string.Empty || currentValue == string.Empty) { return; } if (optionsList == null) { optionsList = new Dictionary(); } optionsList[currentKey] = currentValue; CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.ListOptions, optionsList); CurrentAttribEvent?.UpdateAnnot(); OptionsListBox.Items.Clear(); foreach (string key in optionsList.Keys) { ListBoxItem item = new ListBoxItem(); item.Content = key; item.Tag = optionsList[key]; OptionsListBox.Items.Add(item); } } private void ButtonDel_Click(object sender, RoutedEventArgs e) { if (OptionsListBox.SelectedIndex != -1) { OptionsListBox.Items.RemoveAt(OptionsListBox.SelectedIndex); Dictionary currentList = new Dictionary(); foreach (ListBoxItem item in OptionsListBox.Items) { currentList[item.Content.ToString()] = item.Tag.ToString(); } CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.ListOptions, currentList); CurrentAttribEvent?.UpdateAnnot(); optionsList = currentList; } } private void ThicknessText_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (ThicknessText.SelectedIndex >= 0 && ThicknessText.SelectedIndex < 3) { CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Thickness, ThicknessText.SelectedIndex + 1); CurrentAttribEvent?.UpdateAnnot(); } } private void LineStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, LineStyleBox.SelectedIndex == 0 ? C_BORDER_STYLE.BS_SOLID : C_BORDER_STYLE.BS_DASHDED); CurrentAttribEvent?.UpdateAnnot(); } public void EnableControls(bool isEnable) { GroupNameText.IsEnabled = isEnable; FormFieldBox.IsEnabled = isEnable; ReadOnlyBox.IsEnabled = isEnable; BorderColorPicker.IsEnabled = isEnable; ThicknessText.IsEnabled = isEnable; FillColorPicker.IsEnabled = isEnable; LineStyleBox.IsEnabled = isEnable; FontFamilyBox.IsEnabled = isEnable; FontSizeBox.IsEnabled = isEnable; FontStyleBox.IsEnabled = isEnable; FontColorPicker.IsEnabled = isEnable; OptionName.IsEnabled = isEnable; OptionValue.IsEnabled = isEnable; AddOptionBtn.IsEnabled = isEnable; OptionsListBox.IsEnabled = isEnable; OptionDelBtn.IsEnabled = isEnable; } private void LockBox_Checked(object sender, RoutedEventArgs e) { EnableControls(!(LockBox.IsChecked == true)); CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Locked, (LockBox.IsChecked == true)); CurrentAttribEvent?.UpdateAnnot(); } private void ReadOnlyBox_Checked(object sender, RoutedEventArgs e) { CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.ReadOnly, (ReadOnlyBox.IsChecked == true)); CurrentAttribEvent?.UpdateAnnot(); } private void FormFieldBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (FormFieldBox.SelectedIndex != -1) { CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FormField, (FormField)FormFieldBox.SelectedIndex); CurrentAttribEvent?.UpdateAnnot(); } } } }