123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFAnnotation.Form;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.Tool;
- using ComPDFKit.Tool.Help;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Media;
- using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
- namespace Compdfkit_Tools.PDFControl
- {
- public partial class ComboBoxProperty : UserControl
- {
- private ComboBoxParam widgetParam = null;
- private CPDFComboBoxWidget cPDFAnnotation = null;
- private PDFViewControl pdfViewerControl = null;
- private CPDFDocument cPDFDocument = null;
- private Dictionary<string, string> itemlists = null;
- public ObservableCollection<int> SizeList { get; set; } = new ObservableCollection<int>
- {
- 6,8,9,10,12,14,18,20,24,26,28,32,30,32,48,72
- };
- bool IsLoadedData = false;
- public ComboBoxProperty()
- {
- InitializeComponent();
- }
- #region Loaded
- public void SetProperty(AnnotParam annotParam, CPDFAnnotation annotation, CPDFDocument doc, PDFViewControl cPDFViewer)
- {
- widgetParam = (ComboBoxParam)annotParam;
- cPDFAnnotation = (CPDFComboBoxWidget)annotation;
- pdfViewerControl = cPDFViewer;
- cPDFDocument = doc;
- }
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- Binding SizeListbinding = new Binding();
- SizeListbinding.Source = this;
- SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
- FontSizeCombox.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
- FieldNameText.Text = widgetParam.FieldName;
- FormFieldCombox.SelectedIndex = (int)ParamConverter.ConverterWidgetFormFlags(widgetParam.Flags, widgetParam.IsHidden);
- BorderColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.LineColor));
- BackgroundColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.BgColor));
- TextColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.FontColor));
- SetFontName(widgetParam.FontName);
- SetFontStyle(widgetParam.IsItalic, widgetParam.IsBold);
- SetFontSize(widgetParam.FontSize);
- itemlists = widgetParam.OptionItems;
- if (itemlists != null)
- {
- foreach (string key in itemlists.Keys)
- {
- ListBoxItem item = new ListBoxItem();
- item.Content = key;
- item.Tag = itemlists[key];
- itemsListBox.Items.Add(item);
- }
- CheckListCount();
- }
- TopTabControl.SelectedIndex = 2;
- IsLoadedData = true;
- }
- private void UserControl_Unloaded(object sender, RoutedEventArgs e)
- {
- IsLoadedData = false;
- }
- private void SetFontSize(double size)
- {
- int index = SizeList.IndexOf((int)size);
- FontSizeCombox.SelectedIndex = index;
- }
- private void SetFontStyle(bool IsItalic, bool IsBold)
- {
- int index = 0;
- if (IsItalic && IsBold)
- {
- index = 3;
- }
- else if (IsItalic)
- {
- index = 2;
- }
- else if (IsBold)
- {
- index = 1;
- }
- FontStyleCombox.SelectedIndex = index;
- }
- private void SetFontName(string fontName)
- {
- int index = -1;
- List<string> fontFamilyList = new List<string>() { "Helvetica", "Courier", "Times" };
- for (int i = 0; i < fontFamilyList.Count; i++)
- {
- if (fontFamilyList[i].ToLower().Contains(fontName.ToLower())
- || fontName.ToLower().Contains(fontFamilyList[i].ToLower()))
- {
- index = i;
- }
- }
- FontCombox.SelectedIndex = index;
- }
- #endregion
- private void FieldNameText_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (IsLoadedData)
- {
- cPDFAnnotation.SetFieldName((sender as TextBox).Text);
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- }
- }
- private void FormFieldCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (IsLoadedData)
- {
- cPDFAnnotation.SetFlags(ParamConverter.GetFormFlags((ParamConverter.FormField)(sender as ComboBox).SelectedIndex, cPDFAnnotation));
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- }
- }
- private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
- {
- if (IsLoadedData)
- {
- byte[] Color = new byte[3];
- Color[0] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.R;
- Color[1] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.G;
- Color[2] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.B;
- cPDFAnnotation.SetWidgetBorderRGBColor(Color);
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- }
- }
- private void BackgroundColorPickerControl_ColorChanged(object sender, EventArgs e)
- {
- if (IsLoadedData)
- {
- byte[] Color = new byte[3];
- Color[0] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.R;
- Color[1] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.G;
- Color[2] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.B;
- cPDFAnnotation.SetWidgetBgRGBColor(Color);
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- }
- }
- private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
- {
- if (IsLoadedData)
- {
- byte[] Color = new byte[3];
- Color[0] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.R;
- Color[1] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.G;
- Color[2] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.B;
- CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
- cTextAttribute.FontColor = Color;
- cPDFAnnotation.SetTextAttribute(cTextAttribute);
- cPDFAnnotation.UpdateFormAp();
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- }
- }
- private void FontCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (IsLoadedData)
- {
- CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
- bool isBold = IsBold(cTextAttribute.FontName);
- bool isItalic = IsItalic(cTextAttribute.FontName);
- FontType fontType = GetFontType((sender as ComboBox).SelectedItem?.ToString());
- cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
- cPDFAnnotation.SetTextAttribute(cTextAttribute);
- cPDFAnnotation.UpdateFormAp();
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- }
- }
- private void FontStyleCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (IsLoadedData)
- {
- CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
- bool isItalic = false;
- bool isBold = false;
- switch ((sender as ComboBox).SelectedIndex)
- {
- case 0:
- break;
- case 1:
- isItalic = IsItalic(cTextAttribute.FontName);
- isBold = true;
- break;
- case 2:
- isItalic = true;
- isBold = IsBold(cTextAttribute.FontName);
- break;
- case 3:
- isItalic = true;
- isBold = true;
- break;
- default:
- break;
- }
- FontType fontType = GetFontType(cTextAttribute.FontName);
- cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
- cPDFAnnotation.SetTextAttribute(cTextAttribute);
- cPDFAnnotation.UpdateFormAp();
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- }
- }
- private void FontSizeCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (IsLoadedData)
- {
- CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
- cTextAttribute.FontSize = Convert.ToSingle((sender as ComboBox).SelectedItem);
- cPDFAnnotation.SetTextAttribute(cTextAttribute);
- cPDFAnnotation.UpdateFormAp();
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- }
- }
- private void txtItemInput_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (itemlists.ContainsKey(txtItemInput.Text.Trim()))
- {
- btnAddItem.IsEnabled = false;
- }
- else
- {
- if (!string.IsNullOrEmpty(txtItemInput.Text))
- btnAddItem.IsEnabled = true;
- }
- }
- private void btnAddItem_Click(object sender, RoutedEventArgs e)
- {
- itemlists.Add(txtItemInput.Text, txtItemInput.Text);
- ListBoxItem item = new ListBoxItem();
- item.Content = txtItemInput.Text;
- item.Tag = txtItemInput.Text;
- itemsListBox.Items.Add(item);
- UpdateListItems();
- txtItemInput.Text = "";
- txtItemInput.Focus();
- btnAddItem.IsEnabled = false;
- }
- private void UpdateListItems()
- {
- Dictionary<string, string> pairs = new Dictionary<string, string>();
- foreach (ListBoxItem item in itemsListBox.Items)
- {
- if (item.Content != null && item.Tag != null)
- {
- pairs.Add(item.Content.ToString(), item.Tag.ToString());
- }
- }
- int optionsCount = cPDFAnnotation.GetItemsCount();
- for (int i = 0; i < optionsCount; i++)
- {
- cPDFAnnotation.RemoveOptionItem(0);
- }
- int addIndex = 0;
- foreach (string key in pairs.Keys)
- {
- cPDFAnnotation.AddOptionItem(addIndex, pairs[key], key);
- addIndex++;
- }
- if (itemsListBox.SelectedIndex > -1)
- {
- cPDFAnnotation.SelectItem(itemsListBox.SelectedIndex);
- }
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- CheckListCount();
- }
- private void CheckListCount()
- {
- if (itemsListBox.Items.Count > 0)
- TipPanel.Visibility = Visibility.Visible;
- else
- TipPanel.Visibility = Visibility.Collapsed;
- }
- private void itemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (e.AddedItems.Count > 0)
- {
- btnDelete.IsEnabled = true;
- if (itemsListBox.SelectedIndex <= 0)
- {
- btnMoveUp.IsEnabled = false;
- }
- else
- {
- btnMoveUp.IsEnabled = true;
- }
- if (itemsListBox.SelectedIndex >= itemsListBox.Items.Count - 1)
- {
- btnMoveDown.IsEnabled = false;
- }
- else
- {
- btnMoveDown.IsEnabled = true;
- }
- txtItemInput.Text = (itemsListBox.SelectedItem as ListBoxItem).Content.ToString();
- txtItemInput.SelectAll();
- }
- if (itemsListBox.SelectedItems.Count <= 0)
- {
- btnDelete.IsEnabled = false;
- btnMoveDown.IsEnabled = false;
- btnMoveUp.IsEnabled = false;
- }
- if (itemsListBox.SelectedIndex >= 0)
- {
- cPDFAnnotation.SelectItem(itemsListBox.SelectedIndex);
- pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
- }
- }
- private void btnDelete_Click(object sender, RoutedEventArgs e)
- {
- if (itemsListBox.SelectedItems.Count > 0)
- {
- if ((itemsListBox.SelectedItem as ListBoxItem).Content != null)
- itemlists.Remove((itemsListBox.SelectedItem as ListBoxItem).Content.ToString());
- itemsListBox.Items.Remove(itemsListBox.SelectedItem as ListBoxItem);
- btnDelete.IsEnabled = false;
- UpdateListItems();
- txtItemInput.Text = "";
- }
- }
- private void btnMoveUp_Click(object sender, RoutedEventArgs e)
- {
- ListBoxItem newitem = new ListBoxItem();
- newitem.Content = (itemsListBox.SelectedItem as ListBoxItem).Content;
- newitem.Tag = (itemsListBox.SelectedItem as ListBoxItem).Tag;
- int index = itemsListBox.SelectedIndex;
- if (index - 1 >= 0)
- {
- itemsListBox.Items.Insert(index - 1, newitem);
- itemsListBox.Items.Remove(itemsListBox.SelectedItem);
- itemsListBox.SelectedIndex = index - 1;
- itemsListBox.Focus();
- UpdateListItems();
- }
- }
- private void btnMoveDown_Click(object sender, RoutedEventArgs e)
- {
- ListBoxItem newitem = new ListBoxItem();
- newitem.Content = (itemsListBox.SelectedItem as ListBoxItem).Content;
- newitem.Tag = (itemsListBox.SelectedItem as ListBoxItem).Tag;
- int index = itemsListBox.SelectedIndex;
- if (index + 1 <= itemsListBox.Items.Count)
- {
- itemsListBox.Items.Remove(itemsListBox.SelectedItem);
- itemsListBox.Items.Insert(index + 1, newitem);
- itemsListBox.SelectedIndex = index + 1;
- itemsListBox.Focus();
- UpdateListItems();
- }
- }
- }
- }
|