123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using System;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- namespace Compdfkit_Tools.Annotation.PDFAnnotationUI
- {
- public partial class CPDFLinkUI : UserControl, INotifyPropertyChanged
- {
- bool OpenPDF = false;
- int totalPage = 0;
- int LinkPage = 0;
- private LinkAnnotArgs LinkAnnot;
- private AnnotAttribEvent annotAttribEvent;
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- if (this.PropertyChanged != null)
- this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- private bool _drawLink;
- public bool DrawLink
- {
- get
- {
- return _drawLink;
- }
- set
- {
- _drawLink = value;
- InputEnable = _drawLink;
- }
- }
- private string _pagePromptIndex;
- public string PagePromptText
- {
- get
- {
- return _pagePromptIndex;
- }
- set
- {
- _pagePromptIndex = value;
- OnPropertyChanged();
- }
- }
- private int _selectedIndex = 0;
- public int SelectedIndex
- {
- get { return _selectedIndex; }
- set
- {
- _selectedIndex = value;
- CheckingItem(_selectedIndex);
- OnPropertyChanged();
- }
- }
- public bool InputEnable
- {
- get
- {
- if (annotAttribEvent != null)
- {
- return true;
- }
- return DrawLink;
- }
- set
- {
- OnPropertyChanged();
- }
- }
- public CPDFLinkUI()
- {
- InitializeComponent();
- DataContext = this;
- }
- public void SetPresentAnnotAttrib(AnnotAttribEvent AttribEvent, int PageCount)
- {
- annotAttribEvent = AttribEvent;
- UrlText.Text = "";
- PageText.Text = "";
- EmailText.Text = "";
- SaveBtn.IsEnabled = true;
- totalPage = PageCount;
- PagePromptText = Helper.LanguageHelper.PropertyPanelManager.GetString("Holder_Jump") + totalPage;
- if (AttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx))
- {
- int pageNum = (int)AttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1;
- if (pageNum > 0 && pageNum <= totalPage)
- {
- PageText.Text = pageNum.ToString();
- SelectedIndex = 1;
- }
- }
- if (AttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
- {
- string linkUrl = (string)AttribEvent.Attribs[AnnotAttrib.LinkUri];
- if (!string.IsNullOrEmpty(linkUrl))
- {
- if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
- {
- EmailText.Text = linkUrl.ToLower().TrimStart("mailto:".ToCharArray());
- SelectedIndex = 2;
- }
- else
- {
- UrlText.Text = linkUrl;
- SelectedIndex = 0;
- }
- }
- }
- }
- public void InitLinkAnnotArgs(LinkAnnotArgs linkAnnotArgs, int PageCount)
- {
- LinkAnnot = linkAnnotArgs;
- InputEnable = true;
- LinkAnnot.LinkDrawFinished += LinkAnnot_LinkDrawFinished;
- totalPage = PageCount;
- PagePromptText = Helper.LanguageHelper.PropertyPanelManager.GetString("Holder_Jump") + totalPage;
- }
- private void LinkAnnot_LinkDrawFinished(object sender, bool e)
- {
- DrawLink = e;
- UrlText.Text = "";
- PageText.Text = "";
- EmailText.Text = "";
- }
- private void Save_Click(object sender, RoutedEventArgs e)
- {
- if (LinkAnnot != null)
- {
- switch (SelectedIndex)
- {
- case 0:
- LinkAnnot.LinkType = LINK_TYPE.URI;
- string urlPath = UrlText.Text.Trim().ToLower();
- if (urlPath.StartsWith("http://") || urlPath.StartsWith("https://"))
- {
- LinkAnnot.URI = urlPath;
- }
- else
- {
- LinkAnnot.URI = "http://" + urlPath;
- }
- break;
- case 1:
- LinkAnnot.LinkType = LINK_TYPE.GOTO;
- LinkAnnot.DestIndex = LinkPage - 1;
- break;
- case 2:
- LinkAnnot.LinkType = LINK_TYPE.URI;
- LinkAnnot.URI = "mailto:" + EmailText.Text.Trim();
- break;
- default:
- break;
- }
- LinkAnnot.InvokeLinkSaveCalled(null, null);
- }
- else
- {
- switch (SelectedIndex)
- {
- case 0:
- annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
- string urlPath = UrlText.Text.Trim().ToLower();
- if (urlPath.StartsWith("http://") || urlPath.StartsWith("https://"))
- {
- annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkUri, urlPath);
- }
- else
- {
- annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkUri, "http://" + UrlText.Text.Trim().ToLower());
- }
- break;
- case 1:
- annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
- annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkDestIndx, LinkPage - 1);
- break;
- case 2:
- annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
- annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + EmailText.Text.Trim());
- break;
- default:
- break;
- }
- annotAttribEvent.UpdateAnnot();
- }
- DrawLink = false;
- SaveBtn.IsEnabled = false;
- }
- #region Data Verification
- private bool CheckPageNumVaild(out int pageNum, string text)
- {
- pageNum = -1;
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- if (text.Trim() != string.Empty)
- {
- if (!int.TryParse(text.Trim(), out pageNum))
- {
- return false;
- }
- }
- if (pageNum < 1 || pageNum > totalPage)
- {
- return false;
- }
- return true;
- }
- private bool CheckPageWebVaild(string text)
- {
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
- if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
- {
- return false;
- }
- string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
- if (matchText.Length != checkUrl.Length)
- {
- return false;
- }
- return true;
- }
- private bool CheckPageMailVaild(string text)
- {
- if (string.IsNullOrEmpty(text))
- {
- return false;
- }
- if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
- {
- return false;
- }
- return true;
- }
- private void UrlText_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (LinkAnnot != null)
- {
- if (CheckPageWebVaild((sender as TextBox).Text) && DrawLink)
- {
- SaveBtn.IsEnabled = true;
- }
- else
- {
- SaveBtn.IsEnabled = false;
- }
- }
- else
- {
- if (CheckPageWebVaild((sender as TextBox).Text))
- {
- SaveBtn.IsEnabled = true;
- }
- else
- {
- SaveBtn.IsEnabled = false;
- }
- }
- }
- private void PageText_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (LinkAnnot != null)
- {
- if (CheckPageNumVaild(out LinkPage, (sender as TextBox).Text) && DrawLink)
- {
- SaveBtn.IsEnabled = true;
- }
- else
- {
- SaveBtn.IsEnabled = false;
- }
- }
- else
- {
- if (CheckPageNumVaild(out LinkPage, (sender as TextBox).Text))
- {
- SaveBtn.IsEnabled = true;
- }
- else
- {
- SaveBtn.IsEnabled = false;
- }
- }
- }
- private void EmailText_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (LinkAnnot != null)
- {
- if (CheckPageMailVaild((sender as TextBox).Text) && DrawLink)
- {
- SaveBtn.IsEnabled = true;
- }
- else
- {
- SaveBtn.IsEnabled = false;
- }
- }
- else
- {
- if (CheckPageMailVaild((sender as TextBox).Text))
- {
- SaveBtn.IsEnabled = true;
- }
- else
- {
- SaveBtn.IsEnabled = false;
- }
- }
- }
- private void CheckingItem(int ItemIndex)
- {
- bool BtnIsEnabled = false;
- if (LinkAnnot != null)
- {
- switch (ItemIndex)
- {
- case 0:
- BtnIsEnabled = CheckPageWebVaild(UrlText.Text) && DrawLink;
- break;
- case 1:
- BtnIsEnabled = CheckPageNumVaild(out LinkPage, PageText.Text) && DrawLink;
- break;
- case 2:
- BtnIsEnabled = CheckPageMailVaild(EmailText.Text) && DrawLink;
- break;
- default:
- break;
- }
- }
- else
- {
- switch (ItemIndex)
- {
- case 0:
- BtnIsEnabled = CheckPageWebVaild(UrlText.Text);
- break;
- case 1:
- BtnIsEnabled = CheckPageNumVaild(out LinkPage, PageText.Text);
- break;
- case 2:
- BtnIsEnabled = CheckPageMailVaild(EmailText.Text);
- break;
- default:
- break;
- }
- }
- SaveBtn.IsEnabled = BtnIsEnabled;
- }
- #endregion
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- Binding Indexbinding = new Binding();
- Indexbinding.Source = this;
- Indexbinding.Path = new System.Windows.PropertyPath("SelectedIndex");
- Indexbinding.Mode = BindingMode.TwoWay;
- HeadTabControl.SetBinding(TabControl.SelectedIndexProperty, Indexbinding);
- }
- private void PART_BtnClear_Click(object sender, RoutedEventArgs e)
- {
- switch (SelectedIndex)
- {
- case 0:
- UrlText.Text = "";
- break;
- case 1:
- PageText.Text = "";
- break;
- case 2:
- EmailText.Text = "";
- break;
- default:
- break;
- }
- }
- private void UserControl_Unloaded(object sender, RoutedEventArgs e)
- {
- UrlText.Text = string.Empty;
- PageText.Text = string.Empty;
- EmailText.Text = string.Empty;
- }
- }
- }
|